textview android中的显示json数组不起作用

问题描述 投票:-1回答:1

我的JSON数组不起作用,我也不知道为什么。在日志中,它仅显示以下内容:[],而没有其他显示。我不会在文本视图中使用它。

这是我的代码:

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);

}

java php android arrays json
1个回答
0
投票

我尝试了此ID为ID的代码:

inreate:

uAccount("3");

使用方法

  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);

}

© www.soinside.com 2019 - 2024. All rights reserved.