D
D
DenisVladimirovich2017-05-28 12:36:04
PHP
DenisVladimirovich, 2017-05-28 12:36:04

How to read json_encode in Java?

I am writing in php server side as usual and calling echo json_encode($a) wanted to read it into Java using Volley. But at that moment, when everything works well for everyone in the articles and in the video, echo json_encode ($ a) is not perceived inside the application for me. Below is the code in Java and PHP. What could be the bug?
here is a link to the page with the request app-container.ru/news-app/getPost.php?id=9488

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://app-container.ru/news-app/getPost.php",null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String image;
                try {
                    image = response.getString("image");
                    Toast.makeText(getApplicationContext(), image, Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Fail 2", error.toString());
                Toast.makeText(getApplicationContext(), "Error response", Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("id", id);
                return params;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(jsonObjectRequest);

if ($r->num_rows > 0) {
    $article=array();
    while($row = $r->fetch_assoc())
    {
        $article['id']=$row["id"];
        $article['title']=$row["title"];
        $imagejson = $row["images"];
        $image_obj = json_decode($imagejson);
        $article['image']=$image_obj->{"image_intro"};
    }
    echo json_encode($artice);
}
else{
    echo "0 results";
}

And I also wanted to point out. If you write echo "hello", then the application with this code will read

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
ugodrus, 2017-05-28
@ugodrus

It's hard to say without sample data. Do you actually read Json there? Try playing around with the json_encode options, maybe it's a matter of escaping some characters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question