我正在使用排球库。当我尝试向无效域名发送请求时,我的应用程序崩溃,所以我想知道后台发生了什么,我该如何解决这个问题? 演示代码可以帮助我。
final RequestQueue reQueue=Volley.newRequestQueue(this);
final String url="http://www.googkjle.com"; // invalid domain name
checkbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
StringRequest request=new StringRequest(com.android.volley.Request.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(
String response) {
showAlertDialog(MainActivity.this, "INTERNET ACCESS", "You have internet access"+response.substring(0,50));
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(
VolleyError error) {
// TODO Auto-generated method stub
int a=error.networkResponse.statusCode;
showAlertDialog(MainActivity.this, "Ohh. NO INTERNET ACCESS", "You do not have internet access");
System.out.println("status code : "+a);
}
});
reQueue.add(request);
}
});
}
public void showAlertDialog(Context context, String title,String message) {
AlertDialog alertDialog=new AlertDialog.Builder(context).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.show();
}
LogCat:
08-27 00:37:42.735: D/gralloc_goldfish(2637): Emulator without GPU emulation detected.
08-27 00:37:46.445: D/AndroidRuntime(2637): Shutting down VM
08-27 00:37:46.445: W/dalvikvm(2637): threadid=1: thread exiting with uncaught exception (group=0xb3a03ba8)
08-27 00:37:46.485: E/AndroidRuntime(2637): FATAL EXCEPTION: main
08-27 00:37:46.485: E/AndroidRuntime(2637): Process: com.exampfle.real, PID: 2637
08-27 00:37:46.485: E/AndroidRuntime(2637): java.lang.NullPointerException
08-27 00:37:46.485: E/AndroidRuntime(2637): at com.exampfle.real.MainActivity$1$2.onErrorResponse(MainActivity.java:78)
08-27 00:37:46.485: E/AndroidRuntime(2637): at com.android.volley.Request.deliverError(Request.java:563)
08-27 00:37:46.485: E/AndroidRuntime(2637): at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
08-27 00:37:46.485: E/AndroidRuntime(2637): at android.os.Handler.handleCallback(Handler.java:733)
08-27 00:37:46.485: E/AndroidRuntime(2637): at android.os.Handler.dispatchMessage(Handler.java:95)
08-27 00:37:46.485: E/AndroidRuntime(2637): at android.os.Looper.loop(Looper.java:136)
08-27 00:37:46.485: E/AndroidRuntime(2637): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-27 00:37:46.485: E/AndroidRuntime(2637): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 00:37:46.485: E/AndroidRuntime(2637): at java.lang.reflect.Method.invoke(Method.java:515)
08-27 00:37:46.485: E/AndroidRuntime(2637): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-27 00:37:46.485: E/AndroidRuntime(2637): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-27 00:37:46.485: E/AndroidRuntime(2637): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
编辑:我误读了行号...修复答案
如果您的MainClass上的第78行是:
int a=error.networkResponse.statusCode;
然后networkResponse可能为null,并导致NPE - 空指针异常
您可以通过以下方式解决此问题:
try{
int a=error.networkResponse.statusCode;
} catch (NullPointerException e){
// Dont know... perhaps just put a Toast to show... or
int a=-1;
}