httpurlconnection.getinputstream 空指针异常
httpurlconnection.getinputstream NullPointerException
我在使用 HttpUrlConnection 时遇到问题。当我将它与 url= https://whosebug.com/ it works very well. As soon as I use other code for example: url= http://192.168.78.1/index.php 一起使用时,它不起作用并导致 NullPointerException 。你能给我解释一下为什么会这样吗?感谢您的帮助。
@Override
protected String doInBackground(Void... params) {
try {
URL url=new URL("http://whosebug.com");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
connection.disconnect();
return stringBuilder.toString();
} catch (MalformedURLException e) {
return null;
} catch (ProtocolException e) {
return null;
} catch (IOException e) {
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
TextView tv=(TextView)context.findViewById(R.id.textView);
tv.setText(s.toString());
}
这是来自 LogCat 的 StackTrace:
1335-1335/com.example.administrator.http E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.administrator.http.Asysnc.onPostExecute(Asysnc.java:57)
at com.example.administrator.http.Asysnc.onPostExecute(Asysnc.java:15)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access0(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
在doInBackground 方法中,你return null,当异常被捕获时。在这种情况下,onPostExecute 将在您调用 s.toString().
的行上失败
您可以:
更新您的 onPostExecute 方法,以便它可以处理空结果,例如:
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s==null) {
s="";
}
TextView tv=(TextView)context.findViewById(R.id.textView);
tv.setText(s.toString());
}
或者当您在 doInBackground 中捕获异常时,您可以 return 其他内容,例如:
catch (IOException e) {
e.printStackTrace();
return "IO exception caught!";
}
您在 void onPostExecute(String s)
方法中获得了 NullPointerException
。发生这种情况是因为 String doInBackground(Void... params)
mehtod 返回了 null
。它返回 null
,因为,每次出现 Exception
时,您在此方法中得到一个 Exception
,并且您的代码 returns null
。 Exception
可能由于不同的原因而发生 - 例如:
- 连接超时
- 不正确URL
- 无法访问服务器或端点
- 请求或响应中的格式错误的数据
- 互联网连接中断
- input/output错误
- 等等...
我认为,您遇到此错误是因为 Android 设备无法访问您的 url http://192.168.78.1/index.php。也许它在不同的网络中,这个地址在网络中不可用,IP 不正确或类似的东西,但这只是我的猜测。我无权访问您的环境,因此无法对其进行调试。
不幸的是,AsyncTask
没有为您提供任何错误处理机制。您可以在 void onPostExecute(String s)
方法中执行 null 检查或尝试执行其他类似的 "tricks" 以防止出现此类情况,但在我看来,它们只是解决方法。如果你想拥有干净和健壮的代码,你应该摆脱 AsyncTask
并使用例如重新实现这个解决方案。 RxJava,它对错误处理和其他有用的特性有很好的支持。你应该知道服务器无法访问的情况,因为它发生在现实生活中。
我在使用 HttpUrlConnection 时遇到问题。当我将它与 url= https://whosebug.com/ it works very well. As soon as I use other code for example: url= http://192.168.78.1/index.php 一起使用时,它不起作用并导致 NullPointerException 。你能给我解释一下为什么会这样吗?感谢您的帮助。
@Override
protected String doInBackground(Void... params) {
try {
URL url=new URL("http://whosebug.com");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
connection.disconnect();
return stringBuilder.toString();
} catch (MalformedURLException e) {
return null;
} catch (ProtocolException e) {
return null;
} catch (IOException e) {
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
TextView tv=(TextView)context.findViewById(R.id.textView);
tv.setText(s.toString());
}
这是来自 LogCat 的 StackTrace:
1335-1335/com.example.administrator.http E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.administrator.http.Asysnc.onPostExecute(Asysnc.java:57)
at com.example.administrator.http.Asysnc.onPostExecute(Asysnc.java:15)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access0(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
在doInBackground 方法中,你return null,当异常被捕获时。在这种情况下,onPostExecute 将在您调用 s.toString().
的行上失败您可以: 更新您的 onPostExecute 方法,以便它可以处理空结果,例如:
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s==null) {
s="";
}
TextView tv=(TextView)context.findViewById(R.id.textView);
tv.setText(s.toString());
}
或者当您在 doInBackground 中捕获异常时,您可以 return 其他内容,例如:
catch (IOException e) {
e.printStackTrace();
return "IO exception caught!";
}
您在 void onPostExecute(String s)
方法中获得了 NullPointerException
。发生这种情况是因为 String doInBackground(Void... params)
mehtod 返回了 null
。它返回 null
,因为,每次出现 Exception
时,您在此方法中得到一个 Exception
,并且您的代码 returns null
。 Exception
可能由于不同的原因而发生 - 例如:
- 连接超时
- 不正确URL
- 无法访问服务器或端点
- 请求或响应中的格式错误的数据
- 互联网连接中断
- input/output错误
- 等等...
我认为,您遇到此错误是因为 Android 设备无法访问您的 url http://192.168.78.1/index.php。也许它在不同的网络中,这个地址在网络中不可用,IP 不正确或类似的东西,但这只是我的猜测。我无权访问您的环境,因此无法对其进行调试。
不幸的是,AsyncTask
没有为您提供任何错误处理机制。您可以在 void onPostExecute(String s)
方法中执行 null 检查或尝试执行其他类似的 "tricks" 以防止出现此类情况,但在我看来,它们只是解决方法。如果你想拥有干净和健壮的代码,你应该摆脱 AsyncTask
并使用例如重新实现这个解决方案。 RxJava,它对错误处理和其他有用的特性有很好的支持。你应该知道服务器无法访问的情况,因为它发生在现实生活中。