Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 新请求队列的截击错误_Java_Android_Rest_Api_Android Volley - Fatal编程技术网

Java 新请求队列的截击错误

Java 新请求队列的截击错误,java,android,rest,api,android-volley,Java,Android,Rest,Api,Android Volley,当我应用了使用valley library的简单代码时,我使用了google示例“” RequestQueue queue=Volley.newRequestQueue(getApplicationContext()); 字符串url=”http://www.google.com"; //从提供的URL请求字符串响应。 StringRequest StringRequest=新的StringRequest(Request.Method.GET,url, 新的Response.Listener()

当我应用了使用valley library的简单代码时,我使用了google示例“”

RequestQueue queue=Volley.newRequestQueue(getApplicationContext());
字符串url=”http://www.google.com";
//从提供的URL请求字符串响应。
StringRequest StringRequest=新的StringRequest(Request.Method.GET,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//显示响应字符串的前500个字符。
message.setText(“响应为:”+Response.substring(0500));
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
message.setText(“那没用!”);
}
});
//将请求添加到RequestQueue。
添加(stringRequest);
我得到下面的错误

E/Volley:[77336]NetworkDispatcher.run:未处理的异常java.lang.NullPointerException:尝试从空对象引用上的字段“int java.lang.String.offset”读取 java.lang.NullPointerException:尝试读取空对象引用上的字段“int java.lang.String.offset” 位于java.lang.String.compareToIgnoreCase(String.java:560) 位于java.lang.String$CaseInsensitiveComparator.compare(String.java:77) 位于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)


代码表示您遇到了
空指针异常

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

检查传递的值是否不为null


请参见

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

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():-


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

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

我做了大量工作后找到了解决方案,我使用了Github的一个截取库,而不是在应用程序文件的依赖项中使用“compile'com.android.volley:volley:1.0.0”。希望有人能解释一下为什么我不能使用Github的库。谢谢大家的帮助

你能告诉我哪一行有错误吗(Logcat定点行)?实际上我不知道你所说的定点行是什么意思,这是我在应用请求时出现的错误。我没有使用包含数据的变量,我正在尝试测试那个简单的请求!应用更改后仍有相同的错误。有时getApplicationContext可能返回null,因此请尝试将其更改为this或getActivity()。如果仍然相同,则错误似乎不是来自“响应”.Ya我就是这么说的。我解决了你的空指针问题。。但是你没有得到响应数据,你正在尝试设置它。这就是为什么应用程序崩溃。你正在点击“”期待字符串响应吗?如何兄弟..尝试点击不同的api,它会给你字符串响应或json响应并尝试。或者如果可能的话,检查改装库。它与volley相同,但是新的。我已经从服务器应用了我自己的json响应,但仍然有相同的问题,我的google提供了这个示例!然后使用Json gRequest而不是StringRequest
RequestQueue queue = Volley.newRequestQueue(this);//if you are in activity, if you are in fragment then **getActivity()**
if(response!=null){
  message.setText("Response is: "+ response.substring(0,500));
}