我在我的 android 应用程序中尝试 volley 库
这是我的日志
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)
String error = new String(volleyError.networkResponse.data, HTTP.UTF_8);
我不知道怎么回事,有人知道吗?