Answer the question
In order to leave comments, you need to log in
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";
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question