【问题标题】:display json array in textview android not working在textview android中显示json数组不起作用
【发布时间】:2020-07-21 04:50:17
【问题描述】:

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

}

【问题讨论】:

  • 请通过断点显示你的response变量内部数据
  • 在 logcat 中?在我的日志猫响应中只显示:{“uaccount”:[]}

标签: java php android arrays json


【解决方案1】:

我用 id 尝试了这段代码:

在onreate:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-22
    • 2016-02-22
    • 2023-03-18
    • 2016-12-02
    • 2013-04-26
    • 2015-11-18
    • 2012-08-10
    • 2021-10-17
    相关资源
    最近更新 更多