在Android中显示JSON数组在TextView中不起作用。

huangapple 未分类评论63阅读模式
英文:

display json array in textview android not working

问题

以下是翻译好的代码部分:

private void account(){
    // 从服务器获取数据
    String url = "我的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());

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

                        txt_City.setText("" + city);
                        txt_Town.append(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);
}
英文:

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 = &quot;my url&quot;;

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null,
  new Response.Listener&lt;JSONObject&gt;() {
    @Override
    public void onResponse(JSONObject response) {

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

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

          String city = employee.getString(&quot;city&quot;);
          String town = employee.getString(&quot;town&quot;);

          txt_City.setText(&quot;&quot;+city);
          txt_Town.append(town);
          //Log.i(&quot;co&quot;, city + town);
          
        }
      } catch (JSONException e) {
        Log.i(&quot;console.logs e:&quot;, e.toString());

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

    error.printStackTrace();
  }
});

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

}

答案1

得分: 0

我尝试过这段带有ID的代码:

在onCreate方法中:

uAccount("3");

在方法中:

private void uAccount(final String id){
    // 从服务器获取
    String 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);
}
英文:

i tried this code with id :

in onreate:

uAccount(&quot;3&quot;);

in method

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

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null,
  new Response.Listener&lt;JSONObject&gt;() {
    @Override
    public void onResponse(JSONObject response) {

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

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

          String city = employee.getString(&quot;city&quot;);
          String town = employee.getString(&quot;town&quot;);

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

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

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

    error.printStackTrace();
  }
}){
  @Override
  protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
    Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
    params.put(&quot;id&quot;,id);

    return params;
  }
};

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

}

huangapple
  • 本文由 发表于 2020年4月9日 01:22:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/61106394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定