没有json对象的值

时间:2018-01-24 05:13:19

标签: android json android-volley

我想在listview中加载来自服务器的userid和评论。我已经完成了ListView和适配器,但在阅读jsonarray时遇到错误。它没有显示结果的价值。

JSON数组

  

{“result”:[{“USER_ID”:“9876543210”,“Desc”:“非常好”}}}

Android代码

private void loadComments(){
        StringRequest stringRequest = new StringRequest(Request.Method.GET, LOAD_COMMENT_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("result");
                    Comments comments = new Comments();
                    for (int i = 0; jsonArray.length() > i; i++) {
                        final JSONObject j = jsonArray.getJSONObject(i);


                        String username = j.getString("USER_ID");
                        String comment = j.getString("Desc");

                        comments.setUsername(username);
                        comments.setComments(comment);
                        commentslist.add(comments);
                    }
                    ListViewAdapter adapter = new ListViewAdapter(commentslist, SingleImage.this);
                    listComments.setAdapter(adapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(SingleImage.this, error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

错误

W/System.err: org.json.JSONException: No value for Desc
W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
W/System.err:     at com.smiles.mychildren.SingleImage$3.onResponse(SingleImage.java:93)
W/System.err:     at com.smiles.mychildren.SingleImage$3.onResponse(SingleImage.java:81)
W/System.err:     at com.android.volley.request.StringRequest.deliverResponse(StringRequest.java:62)
W/System.err:     at com.android.volley.request.StringRequest.deliverResponse(StringRequest.java:31)
W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:154)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6077)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

6 个答案:

答案 0 :(得分:2)

请使用此

7

答案 1 :(得分:2)

只是改变  在线以下

private void loadComments(){
    StringRequest stringRequest = new StringRequest(Request.Method.GET, LOAD_COMMENT_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("result");
                Comments comments = new Comments();
                for (int i = 0; jsonArray.length() > i; i++) {
                    final JSONObject j = jsonArray.getJSONObject(i);
                    if(i==0) {
                         String username = j.getString("USER_ID");
                         comments.setUsername(username);
                    } else if(i==1) {
                         String comment = j.getString("Desc");
                         comments.setComments(comment);
                    } 
                }
                commentslist.add(comments);
                ListViewAdapter adapter = new ListViewAdapter(commentslist, SingleImage.this);
                listComments.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(SingleImage.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

 JSONObject jsonObject = new JSONObject();

注意 - 您的错误是因为您的json响应格式不正确,所以如果可能的话,请按以下格式更新您的respoinse。

 JSONObject jsonObject = new JSONObject(response);

答案 2 :(得分:1)

JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("result");

                for (int i = 0; jsonArray.length() > i; i++) {
                    JSONObject j = jsonArray.getJSONObject(i);
                    String username = j.getString("USER_ID");
                    String comment = j.getString("Desc");

答案 3 :(得分:1)

我希望它对您有用,请参阅下面的代码段:

    private void loadComments() {
 StringRequest stringRequest = new StringRequest(Request.Method.GET, LOAD_COMMENT_URL, new Response.Listener < String > () {
  @Override
  public void onResponse(String response) {
   try {
    JSONObject jsonObject = new JSONObject(response);
    JSONArray jaResult = null;
    try {
     jaResult = jsonObject.getJSONArray("result");
     Comments comments = new Comments();
     String username = "";
     String comment = "";
     for (int i = 0; jaResult.length() > i; i++) {
      final JSONObject joResult = jaResult.getJSONObject(i);
      if (i == 0 && joResult.has("USER_ID")) {
       username = joResult.getString("USER_ID");
      } else if (i == 1 && joResult.has("Desc")) {
       comment = joResult.getString("Desc");
      }
     }

     comments.setUsername(username);
     comments.setComments(comment);
     commentslist.add(comments);
    } catch (JSONException e) {
     e.printStackTrace();
    }

    ListViewAdapter adapter = new ListViewAdapter(commentslist, SingleImage.this);
    listComments.setAdapter(adapter);
   } catch (JSONException e) {
    e.printStackTrace();
   }
  }
 }, new Response.ErrorListener() {
  @Override
  public void onErrorResponse(VolleyError error) {
   Toast.makeText(SingleImage.this, error.getMessage(), Toast.LENGTH_SHORT).show();
  }
 });
 RequestQueue requestQueue = Volley.newRequestQueue(this);
 requestQueue.add(stringRequest);
}

答案 4 :(得分:0)

试试这个

try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray jsonArray = jsonObject.getJSONArray("result");

        for (int i = 0; jsonArray.length() > i; i++) {

            JSONObject j = jsonArray.getJSONObject(i);
            Comments comments = new Comments();

            if (j.has("USER_ID")) 
            {
                String username = j.getString("USER_ID");

            } else if (j.has("Desc")) 
            {
               String comment = j.getString("Desc");
            }


               comments.setUsername(username);
               comments.setComments(comment);
               commentslist.add(comments);
        }

      ListViewAdapter adapter = new ListViewAdapter(commentslist, SingleImage.this);
      listComments.setAdapter(adapter);

} catch (JSONException e) 
{
    e.printStackTrace();
}

答案 5 :(得分:0)

如你所知,只有两个对象,你可以不用for循环来做。

     private void loadComments(){
            StringRequest stringRequest = new StringRequest(Request.Method.GET, LOAD_COMMENT_URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray jsonArray = jsonObject.getJSONArray("result");
                        Comments comments = new Comments();

                            final JSONObject j = jsonArray.getJSONObject(0);
                            final JSONObject secondObject = jsonArray.getJSONObject(1);
                                if(j.has("USER_ID"))
                                {
                                 String username = j.getString("USER_ID");
                                 comments.setUsername(username);
                                }
                                if(secondObject.has("Desc"))
                                {
                                 String comment = secondObject.getString("Desc");
                                 comments.setComments(comment);
                                 }


                        commentslist.add(comments);
                        ListViewAdapter adapter = new ListViewAdapter(commentslist, SingleImage.this);
                        listComments.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(SingleImage.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        }