新请求队列的排名错误

时间:2017-09-20 04:46:27

标签: java android rest api android-volley

当我应用了一个使用谷库的简单代码时,我使用了谷歌示例“https://developer.android.com/training/volley/simple.html#simple

                RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
            String url ="http://www.google.com";

            // Request a string response from the provided URL.
            StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            // Display the first 500 characters of the response string.
                            message.setText("Response is: "+ response.substring(0,500));
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    message.setText("That didn't work!");
                }
            });
                // Add the request to the RequestQueue.
                     queue.add(stringRequest);

我得到以下错误。

  

E / Volley:[77336] NetworkDispatcher.run:未处理的异常java.lang.NullPointerException:尝试从空对象引用上的字段'int java.lang.String.offset'读取                                                                       java.lang.NullPointerException:尝试从空对象引用上的字段'int java.lang.String.offset'读取                                                                           at java.lang.String.compareToIgnoreCase(String.java:560)                                                                           at java.lang.String $ CaseInsensitiveComparator.compare(String.java:77)                                                                           at java.lang.String $ CaseInsensitiveComparator.compare(String.java:66)                                                                           在java.util.TreeMap.find(TreeMap.java:277)                                                                           在java.util.TreeMap.putInternal(TreeMap.java:240)                                                                           在java.util.TreeMap.put(TreeMap.java:186)                                                                           在com.android.volley.NetworkResponse.toHeaderMap(NetworkResponse.java:138)                                                                           在com.android.volley.NetworkResponse。(NetworkResponse.java:0)                                                                           在com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:169)                                                                           在com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:113)

4 个答案:

答案 0 :(得分:1)

代码显示您遇到了NullPointer exception

  

这意味着您在此代码中使用的变量为null   运行时。

     

检查您的传递值是否为空。

请参阅this

答案 1 :(得分:1)

经过大量的工作后我找到了解决方案,我使用的是Github的凌空库,而不是在app文件的依赖项中使用“compile'c​​om.android.volley:volley:1.0.0'”。希望有人能解释一下为什么我不能使用Github的库。谢谢大家的帮助。

答案 2 :(得分:0)

请从响应中删除子字符串: -

message.setText("Response is: "+ response.substring(0,500));
更改后

   if(response.lemght>500){
        message.setText("Response is: "+ response.substring(0,500));
   }else{

        message.setText("Response is: "+ response);
   }

试试这个 在创建RequestQueue时,请传递上下文而不是getApplicationContext(): -

RequestQueue queue = Volley.newRequestQueue(this);//if you are in activity, if you are in fragment then **getActivity()**

答案 3 :(得分:0)

当您进行网络呼叫时,您可能会获得成功但有时不会获得数据。因此,请确保在访问数据之前进行空检查。

if(response!=null){
  message.setText("Response is: "+ response.substring(0,500));
}