问题描述
我在我的 android 应用程序中尝试了 volley 库
i try volley library in my android application
这是我的日志
10-31 14:30:09.277: E/AndroidRuntime(22916): java.lang.NullPointerException
10-31 14:30:09.277: E/AndroidRuntime(22916): at com.mypackage.api.Api$2.onErrorResponse(Api.java:269)
10-31 14:30:09.277: E/AndroidRuntime(22916): at com.android.volley.Request.deliverError(Request.java:517)
10-31 14:30:09.277: E/AndroidRuntime(22916): at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
10-31 14:30:09.277: E/AndroidRuntime(22916): at android.os.Handler.handleCallback(Handler.java:615)
10-31 14:30:09.277: E/AndroidRuntime(22916): at android.os.Handler.dispatchMessage(Handler.java:92)
10-31 14:30:09.277: E/AndroidRuntime(22916): at android.os.Looper.loop(Looper.java:137)
10-31 14:30:09.277: E/AndroidRuntime(22916): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-31 14:30:09.277: E/AndroidRuntime(22916): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 14:30:09.277: E/AndroidRuntime(22916): at java.lang.reflect.Method.invoke(Method.java:511)
10-31 14:30:09.277: E/AndroidRuntime(22916): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-31 14:30:09.277: E/AndroidRuntime(22916): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-31 14:30:09.277: E/AndroidRuntime(22916): at dalvik.system.NativeStart.main(Native Method)
这就是我如何使用截击
GetStringRequest req = new GetStringRequest(Request.Method.GET,URL_API,
new Response.Listener<String>() {
// handle success response
}, new Response.ErrorListener() {
//handle error response
@Override
public void onErrorResponse(VolleyError volleyError) {
try {
String error = new String(volleyError.networkResponse.data, HTTP.UTF_8);
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
有时我在这一行收到错误 nullPointerException (269)
sometimes i get error nullPointerException at this line (269)
String error = new String(volleyError.networkResponse.data, HTTP.UTF_8);
我不知道怎么回事,有人知道吗?
i don't know what is wrong, anyone know?
推荐答案
可能是 volleyError.networkResponse.data 为空.我不确定你想用这行代码得到什么,但与 Volley 一起工作并想看看 volleyError 中有什么.你可以试试这个:
Chances are that volleyError.networkResponse.data is empty. I am not sure what you are trying to get with this line of code, but working with Volley and wanting to see what is in volleyError. You could try this:
String error = volleyError.toString();
然后您可以检查此字符串是否有任何特定错误 [至少我是这样做的].VolleyErrors 可能是 API 定义的少数几个之一,例如超时错误、连接错误、服务器错误等.当然,如果您想根据特定错误触发其他操作,您可能需要进一步解析字符串.
You can then check this string for any specific errors [at least that's how I do it]. VolleyErrors could be one of the few defined by the API such as timeout error, connection error, server error, and so forth. Of-course, you might have to parse the string further if you want to fire other actions based on a specific error.
这篇关于Volley onErrorResponse 给出 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!