gosulove gosulove - 2 months ago 10
Android Question

Android Volley JSON Request display empty data in TextView

I reading Android official docs and trying to display JSON data in

Textview
. I tried the following coding but when I click on the
Button
, nothing appears and also no error prompt. What's wrong with it? The coding is mainly from Android official docs.

TextView mTxtDisplay;

String url = "http://192.168.1.102/web_service/test.php/";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTxtDisplay = (TextView) findViewById(R.id.tv);

JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
mTxtDisplay.setText("Response: " + response.toString());
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub

}
});
}}


192.168.1.102/web_service/test.php

{
"1":{"name":"Superman","date":"20160909","location":"Mars"},
"2":{"name":"Xman","date":"20160909","location":"Galaxy"},
"3":{"name":"Batman","date":"20160909","location":"Earth"}
}

Answer

Add your Json Object to Request Queue at the end after Error Listener.

     JsonObjectRequest jsObjRequest = new JsonObjectRequest
        (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                mTxtDisplay.setText("Response: " + response.toString());
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub

            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsObjRequest);
Comments