简体   繁体   中英

display json array in textview android not working

My JSON array don't work and I don't know why. In logs, it only show this: [] and nothing else. I won't use that in text view.

This is my code:

private void account(){
// get from the server
    String url = "my url";

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null,
  new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {

      try {
        JSONArray jsonArray = response.getJSONArray("uaccount");
        Log.i("conso", jsonArray.toString());

        for (int i = 0; i < jsonArray.length(); i++) {
          JSONObject employee = jsonArray.getJSONObject(i);
          Log.i("con", employee.toString());
          //Log.i("con", name+ number+status+ age+ city+ town+ gender+weight +height);

          String city = employee.getString("city");
          String town = employee.getString("town");

          txt_City.setText(""+city);
          txt_Town.append(town);
          //Log.i("co", city + town);

        }
      } catch (JSONException e) {
        Log.i("console.logs e:", e.toString());

        e.printStackTrace();
      }
    }
  }, new Response.ErrorListener() {
  @Override
  public void onErrorResponse(VolleyError error) {
    Log.i("console.logs error:", error.toString());

    error.printStackTrace();
  }
});

requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(request);

}

i tried this code with id:

in onreate:

uAccount("3");

in method

  private void uAccount(final String id){
// get from server
    String url = "my url";

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null,
  new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {

      try {
        JSONArray jsonArray = response.getJSONArray("uaccount");
        Log.i("conso", jsonArray.toString());

        for (int i = 0; i < jsonArray.length(); i++) {
          JSONObject employee = jsonArray.getJSONObject(i);
          Log.i("con", employee.toString());
          //Log.i("con", name+ number+status+ age+ city+ town+ gender+weight +height);

          String city = employee.getString("city");
          String town = employee.getString("town");

          txt_City.setText(""+city);
          txt_Town.append(town);
          //Log.i("co", city + town);

        }
      } catch (JSONException e) {
        Log.i("console.logs e:", e.toString());

        e.printStackTrace();
      }
    }
  }, new Response.ErrorListener() {
  @Override
  public void onErrorResponse(VolleyError error) {
    Log.i("console.logs error:", error.toString());

    error.printStackTrace();
  }
}){
  @Override
  protected Map<String, String> getParams() throws AuthFailureError {
    Map<String, String> params = new HashMap<>();
    params.put("id",id);

    return params;
  }
};

requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(request);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM