eclipse Android - .getInputStream() 不断崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17414101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - .getInputStream() keeps on crashing
提问by Merelda
I'm really new to Android, java and HTML. So I'm pretty much stuffed. I'm just trying to get data from a URL with an android application. I tried the following code in java and it works fine, so I really stuck with what is wrong with it. I tried googling and used different options, such as setting connection time
我对 Android、Java 和 HTML 真的很陌生。所以我很饱。我只是想从一个带有 android 应用程序的 URL 中获取数据。我在 java 中尝试了以下代码并且它工作正常,所以我真的坚持它有什么问题。我尝试使用谷歌搜索并使用了不同的选项,例如设置连接时间
URL url = new URL("http://www.google.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
try {
input = new InputStreamReader(conn.getInputStream());
in =new BufferedReader(input);
}
I tried a lot of different versions, such as adding a bunch of stuff like: urlConnection.setRequestProperty("Connection", "keep-alive"); urlConnection.setRequestProperty("ConnectionTimeout", "12000"); urlConnection.setRequestProperty("Content-Length", "" + request.length);
我尝试了很多不同的版本,比如添加一堆类似的东西: urlConnection.setRequestProperty("Connection", "keep-alive"); urlConnection.setRequestProperty("ConnectionTimeout", "12000"); urlConnection.setRequestProperty("Content-Length", "" + request.length);
I think the following post has a similar problem: IOException with URLConnection and getInputStream
我认为以下帖子有类似的问题: IOException with URLConnection and getInputStream
I didn't get an IOException (well at least I don't think so), but I got this (and a whole bunch of others)
我没有收到 IOException(好吧至少我不这么认为),但是我收到了这个(还有一大堆其他的)
07-01 23:18:33.870: E/AndroidRuntime(30713): java.lang.IllegalStateException: Could not execute method of the activity
07-01 23:18:33.870: E/AndroidRuntime(30713): java.lang.IllegalStateException: 无法执行活动的方法
And yes, I did add the permission to INTERNET and ACCESS_NETWORK_STATE
是的,我确实添加了对 INTERNET 和 ACCESS_NETWORK_STATE 的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE"/>
I'm using Eclipse and running on my Android HTC One X. Please help. Thanks in advance!
我正在使用 Eclipse 并在我的 Android HTC One X 上运行。请帮忙。提前致谢!
Here's the stack trace after I added the AsyncTask:
这是我添加 AsyncTask 后的堆栈跟踪:
07-02 00:02:34.775: W/IInputConnectionWrapper(3799): getExtractedText on inactive InputConnection
07-02 00:02:34.785: W/IInputConnectionWrapper(3799): getTextBeforeCursor on inactive InputConnection
07-02 00:02:34.785: W/IInputConnectionWrapper(3799): getSelectedText on inactive InputConnection
07-02 00:02:34.785: W/IInputConnectionWrapper(3799): getTextAfterCursor on inactive InputConnection
07-02 00:02:34.790: W/IInputConnectionWrapper(3799): getExtractedText on inactive InputConnection
07-02 00:02:34.795: W/IInputConnectionWrapper(3799): getTextBeforeCursor on inactive InputConnection
07-02 00:02:34.795: W/IInputConnectionWrapper(3799): getExtractedText on inactive InputConnection
07-02 00:02:34.795: W/IInputConnectionWrapper(3799): getTextBeforeCursor on inactive InputConnection
07-02 00:02:34.800: W/IInputConnectionWrapper(3799): getSelectedText on inactive InputConnection
07-02 00:02:34.800: W/IInputConnectionWrapper(3799): getTextAfterCursor on inactive InputConnection
07-02 00:02:34.800: W/IInputConnectionWrapper(3799): getExtractedText on inactive InputConnection
07-02 00:02:34.805: W/IInputConnectionWrapper(3799): getTextBeforeCursor on inactive InputConnection
07-02 00:02:34.805: W/IInputConnectionWrapper(3799): getSelectedText on inactive InputConnection
07-02 00:02:34.810: W/IInputConnectionWrapper(3799): getTextAfterCursor on inactive InputConnection
07-02 00:02:34.810: W/IInputConnectionWrapper(3799): beginBatchEdit on inactive InputConnection
07-02 00:02:34.815: W/IInputConnectionWrapper(3799): getExtractedText on inactive InputConnection
07-02 00:02:34.815: W/IInputConnectionWrapper(3799): getTextBeforeCursor on inactive InputConnection
07-02 00:02:34.820: W/IInputConnectionWrapper(3799): getSelectedText on inactive InputConnection
07-02 00:02:34.820: W/IInputConnectionWrapper(3799): getTextAfterCursor on inactive InputConnection
07-02 00:02:34.830: W/IInputConnectionWrapper(3799): getExtractedText on inactive InputConnection
07-02 00:02:34.830: W/IInputConnectionWrapper(3799): getTextBeforeCursor on inactive InputConnection
07-02 00:02:34.830: W/IInputConnectionWrapper(3799): getSelectedText on inactive InputConnection
07-02 00:02:34.835: W/IInputConnectionWrapper(3799): getTextAfterCursor on inactive InputConnection
07-02 00:02:34.845: W/IInputConnectionWrapper(3799): endBatchEdit on inactive InputConnection
07-02 00:02:34.845: W/IInputConnectionWrapper(3799): getExtractedText on inactive InputConnection
07-02 00:02:34.845: W/IInputConnectionWrapper(3799): getTextBeforeCursor on inactive InputConnection
07-02 00:02:34.850: W/IInputConnectionWrapper(3799): getSelectedText on inactive InputConnection
07-02 00:02:34.850: W/IInputConnectionWrapper(3799): getTextAfterCursor on inactive InputConnection
07-02 00:02:34.855: W/IInputConnectionWrapper(3799): beginBatchEdit on inactive InputConnection
07-02 00:02:34.855: W/IInputConnectionWrapper(3799): setComposingRegion on inactive InputConnection
07-02 00:02:34.855: W/IInputConnectionWrapper(3799): endBatchEdit on inactive InputConnection
07-02 00:02:34.865: W/IInputConnectionWrapper(3799): getExtractedText on inactive InputConnection
07-02 00:02:34.865: W/IInputConnectionWrapper(3799): getTextBeforeCursor on inactive InputConnection
07-02 00:02:34.870: W/IInputConnectionWrapper(3799): getSelectedText on inactive InputConnection
07-02 00:02:34.870: W/IInputConnectionWrapper(3799): getTextAfterCursor on inactive InputConnection
07-02 00:05:06.105: W/LoadedApk(5867): pakageInfo is null! try again
07-02 00:05:06.105: W/LoadedApk(5867): packageInfo still null!
07-02 00:05:06.110: D/LoadedApk(5867): dalvik.system.VMStack.getThreadStackTrace(Native Method)
07-02 00:05:06.110: D/LoadedApk(5867): java.lang.Thread.getStackTrace(Thread.java:599)
07-02 00:05:06.110: D/LoadedApk(5867): android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:388)
07-02 00:05:06.110: D/LoadedApk(5867): android.app.LoadedApk.getClassLoader(LoadedApk.java:330)
07-02 00:05:06.110: D/LoadedApk(5867): android.app.LoadedApk.makeApplication(LoadedApk.java:535)
07-02 00:05:06.110: D/LoadedApk(5867): android.app.ActivityThread.handleBindApplication(ActivityThread.java:4803)
07-02 00:05:06.110: D/LoadedApk(5867): android.app.ActivityThread.access00(ActivityThread.java:151)
07-02 00:05:06.110: D/LoadedApk(5867): android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
07-02 00:05:06.110: D/LoadedApk(5867): android.os.Handler.dispatchMessage(Handler.java:99)
07-02 00:05:06.110: D/LoadedApk(5867): android.os.Looper.loop(Looper.java:155)
07-02 00:05:06.110: D/LoadedApk(5867): android.app.ActivityThread.main(ActivityThread.java:5493)
07-02 00:05:06.110: D/LoadedApk(5867): java.lang.reflect.Method.invokeNative(Native Method)
07-02 00:05:06.110: D/LoadedApk(5867): java.lang.reflect.Method.invoke(Method.java:511)
07-02 00:05:06.110: D/LoadedApk(5867): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
07-02 00:05:06.110: D/LoadedApk(5867): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
07-02 00:05:06.110: D/LoadedApk(5867): dalvik.system.NativeStart.main(Native Method)
07-02 00:05:09.565: I/dalvikvm-heap(6053): Grow heap (frag case) to 5.448MB for 1705652-byte allocation
07-02 00:05:09.810: I/dalvikvm-heap(6053): Grow heap (frag case) to 8.340MB for 3034580-byte allocation
07-02 00:05:09.905: I/MediaPlayer(6053): setLPAflag() in
07-02 00:05:09.905: I/MediaPlayer(6053): mContext is null, can't getMirrorDisplayStatus!!!
07-02 00:05:09.905: I/MediaPlayer(6053): setLPAflag() out
07-02 00:05:09.910: W/MediaPlayer(6053): info/warning (1, 902)
07-02 00:05:09.915: D/MediaPlayer(6053): [DLNA]contentType = 902
07-02 00:05:09.915: D/MediaPlayer(6053): doStart() in
07-02 00:05:09.915: D/MediaPlayer(6053): getIntParameter = 902
07-02 00:05:10.190: D/MediaPlayer(6053): Mediaplayer receives message, message type: 200
07-02 00:05:10.190: I/MediaPlayer(6053): Info (1,902)
07-02 00:05:10.190: D/MediaPlayer(6053): Mediaplayer receives message, message type: 5
07-02 00:05:10.190: D/MediaPlayer(6053): Mediaplayer receives message, message type: 1
07-02 00:05:10.295: E/(6053): file /data/data/com.nvidia.NvCPLSvc/files/driverlist.txt: not found!
07-02 00:05:10.295: I/(6053): Attempting to load EGL implementation /system/lib//egl/libEGL_tegra_impl
07-02 00:05:10.485: I/(6053): Loaded EGL implementation /system/lib//egl/libEGL_tegra_impl
07-02 00:05:10.595: I/(6053): Loading GLESv2 implementation /system/lib//egl/libGLESv2_tegra_impl
07-02 00:05:11.080: W/IInputConnectionWrapper(6053): getExtractedText on inactive InputConnection
07-02 00:05:11.140: W/IInputConnectionWrapper(6053): getTextBeforeCursor on inactive InputConnection
07-02 00:05:11.430: I/dalvikvm-heap(6053): Grow heap (frag case) to 10.851MB for 2731536-byte allocation
07-02 00:05:11.830: I/MediaPlayer(6053): setLPAflag() in
07-02 00:05:11.830: I/MediaPlayer(6053): mContext is null, can't getMirrorDisplayStatus!!!
07-02 00:05:11.830: I/MediaPlayer(6053): setLPAflag() out
07-02 00:05:11.845: W/MediaPlayer(6053): info/warning (1, 902)
07-02 00:05:11.870: W/IInputConnectionWrapper(6053): getSelectedText on inactive InputConnection
07-02 00:05:11.875: D/MediaPlayer(6053): Mediaplayer receives message, message type: 200
07-02 00:05:11.875: I/MediaPlayer(6053): Info (1,902)
07-02 00:05:11.875: D/MediaPlayer(6053): Mediaplayer receives message, message type: 5
07-02 00:05:11.875: D/MediaPlayer(6053): Mediaplayer receives message, message type: 1
07-02 00:05:12.025: W/IInputConnectionWrapper(6053): getTextAfterCursor on inactive InputConnection
07-02 00:05:12.600: D/MediaPlayer(6053): [DLNA]contentType = 902
07-02 00:05:12.600: D/MediaPlayer(6053): doStart() in
07-02 00:05:12.600: D/MediaPlayer(6053): getIntParameter = 902
07-02 00:05:12.645: W/IInputConnectionWrapper(6053): getExtractedText on inactive InputConnection
07-02 00:05:12.940: W/IInputConnectionWrapper(6053): getExtractedText on inactive InputConnection
07-02 00:05:12.945: W/IInputConnectionWrapper(6053): getTextBeforeCursor on inactive InputConnection
07-02 00:05:12.950: W/IInputConnectionWrapper(6053): getSelectedText on inactive InputConnection
07-02 00:05:12.955: W/IInputConnectionWrapper(6053): getTextAfterCursor on inactive InputConnection
07-02 00:05:12.960: W/IInputConnectionWrapper(6053): getExtractedText on inactive InputConnection
07-02 00:05:12.965: W/IInputConnectionWrapper(6053): getTextBeforeCursor on inactive InputConnection
07-02 00:05:12.980: W/IInputConnectionWrapper(6053): getExtractedText on inactive InputConnection
07-02 00:05:12.980: W/IInputConnectionWrapper(6053): getTextBeforeCursor on inactive InputConnection
07-02 00:05:12.985: W/IInputConnectionWrapper(6053): getSelectedText on inactive InputConnection
07-02 00:05:12.985: W/IInputConnectionWrapper(6053): getTextAfterCursor on inactive InputConnection
07-02 00:05:12.995: W/IInputConnectionWrapper(6053): getExtractedText on inactive InputConnection
07-02 00:05:13.000: W/IInputConnectionWrapper(6053): getTextBeforeCursor on inactive InputConnection
07-02 00:05:13.000: W/IInputConnectionWrapper(6053): getSelectedText on inactive InputConnection
07-02 00:05:13.005: W/IInputConnectionWrapper(6053): getTextAfterCursor on inactive InputConnection
07-02 00:05:13.005: W/IInputConnectionWrapper(6053): getExtractedText on inactive InputConnection
07-02 00:05:13.010: W/IInputConnectionWrapper(6053): getTextBeforeCursor on inactive InputConnection
07-02 00:05:13.010: W/IInputConnectionWrapper(6053): getSelectedText on inactive InputConnection
07-02 00:05:13.010: W/IInputConnectionWrapper(6053): getTextAfterCursor on inactive InputConnection
07-02 00:05:13.015: W/IInputConnectionWrapper(6053): getExtractedText on inactive InputConnection
07-02 00:05:13.015: W/IInputConnectionWrapper(6053): getTextBeforeCursor on inactive InputConnection
07-02 00:05:13.015: W/IInputConnectionWrapper(6053): getSelectedText on inactive InputConnection
07-02 00:05:13.015: W/IInputConnectionWrapper(6053): getTextAfterCursor on inactive InputConnection
07-02 00:05:13.015: W/IInputConnectionWrapper(6053): beginBatchEdit on inactive InputConnection
07-02 00:05:13.020: W/IInputConnectionWrapper(6053): endBatchEdit on inactive InputConnection
07-02 00:05:13.020: W/IInputConnectionWrapper(6053): getExtractedText on inactive InputConnection
07-02 00:05:13.020: W/IInputConnectionWrapper(6053): getTextBeforeCursor on inactive InputConnection
07-02 00:05:13.020: W/IInputConnectionWrapper(6053): getSelectedText on inactive InputConnection
07-02 00:05:13.020: W/IInputConnectionWrapper(6053): getTextAfterCursor on inactive InputConnection
07-02 00:05:13.025: W/IInputConnectionWrapper(6053): beginBatchEdit on inactive InputConnection
07-02 00:05:13.025: W/IInputConnectionWrapper(6053): setComposingRegion on inactive InputConnection
07-02 00:05:13.025: W/IInputConnectionWrapper(6053): endBatchEdit on inactive InputConnection
07-02 00:05:13.025: W/IInputConnectionWrapper(6053): getExtractedText on inactive InputConnection
07-02 00:05:13.025: W/IInputConnectionWrapper(6053): getTextBeforeCursor on inactive InputConnection
07-02 00:05:13.025: W/IInputConnectionWrapper(6053): getSelectedText on inactive InputConnection
07-02 00:05:13.025: W/IInputConnectionWrapper(6053): getTextAfterCursor on inactive InputConnection
07-02 00:05:13.165: E/SpannableStringBuilder(6053): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-02 00:05:13.165: E/SpannableStringBuilder(6053): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-02 00:05:13.295: D/MediaPlayer(6053): Mediaplayer receives message, message type: 2
07-02 00:05:16.440: W/System.err(6053): java.io.FileNotFoundException: http://www.google.com/
07-02 00:05:16.445: W/System.err(6053): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
07-02 00:05:16.445: W/System.err(6053): at aiti.mit.edu.sa.wu.calculator.MyAsynTask.doInBackground(ActivityHi.java:139)
07-02 00:05:16.445: W/System.err(6053): at aiti.mit.edu.sa.wu.calculator.MyAsynTask.doInBackground(ActivityHi.java:1)
07-02 00:05:16.445: W/System.err(6053): at android.os.AsyncTask.call(AsyncTask.java:287)
07-02 00:05:16.445: W/System.err(6053): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-02 00:05:16.445: W/System.err(6053): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-02 00:05:16.445: W/System.err(6053): at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
07-02 00:05:16.445: W/System.err(6053): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-02 00:05:16.450: W/System.err(6053): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-02 00:05:16.450: W/System.err(6053): at java.lang.Thread.run(Thread.java:864)
07-02 00:05:16.450: W/dalvikvm(6053): threadid=12: thread exiting with uncaught exception (group=0x40c392d0)
07-02 00:05:16.460: E/AndroidRuntime(6053): FATAL EXCEPTION: AsyncTask #1
07-02 00:05:16.460: E/AndroidRuntime(6053): java.lang.RuntimeException: An error occured while executing doInBackground()
07-02 00:05:16.460: E/AndroidRuntime(6053): at android.os.AsyncTask.done(AsyncTask.java:299)
07-02 00:05:16.460: E/AndroidRuntime(6053): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-02 00:05:16.460: E/AndroidRuntime(6053): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-02 00:05:16.460: E/AndroidRuntime(6053): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-02 00:05:16.460: E/AndroidRuntime(6053): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-02 00:05:16.460: E/AndroidRuntime(6053): at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
07-02 00:05:16.460: E/AndroidRuntime(6053): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-02 00:05:16.460: E/AndroidRuntime(6053): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-02 00:05:16.460: E/AndroidRuntime(6053): at java.lang.Thread.run(Thread.java:864)
07-02 00:05:16.460: E/AndroidRuntime(6053): Caused by: java.lang.NullPointerException
07-02 00:05:16.460: E/AndroidRuntime(6053): at aiti.mit.edu.sa.wu.calculator.MyAsynTask.doInBackground(ActivityHi.java:153)
07-02 00:05:16.460: E/AndroidRuntime(6053): at aiti.mit.edu.sa.wu.calculator.MyAsynTask.doInBackground(ActivityHi.java:1)
07-02 00:05:16.460: E/AndroidRuntime(6053): at android.os.AsyncTask.call(AsyncTask.java:287)
07-02 00:05:16.460: E/AndroidRuntime(6053): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-02 00:05:16.460: E/AndroidRuntime(6053): ... 5 more
07-02 00:05:19.770: D/Process(6053): killProcess, pid=6053
07-02 00:05:19.800: D/Process(6053): dalvik.system.VMStack.getThreadStackTrace(Native Method)
07-02 00:05:19.800: D/Process(6053): java.lang.Thread.getStackTrace(Thread.java:599)
07-02 00:05:19.800: D/Process(6053): android.os.Process.killProcess(Process.java:944)
07-02 00:05:19.800: D/Process(6053): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108)
07-02 00:05:19.800: D/Process(6053): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
07-02 00:05:19.800: D/Process(6053): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
07-02 00:05:20.290: I/dalvikvm-heap(6336): Grow heap (frag case) to 5.287MB for 1536016-byte allocation
07-02 00:05:20.375: I/dalvikvm-heap(6336): Grow heap (frag case) to 7.890MB for 2731536-byte allocation
07-02 00:05:20.590: I/MediaPlayer(6336): setLPAflag() in
07-02 00:05:20.590: I/MediaPlayer(6336): mContext is null, can't getMirrorDisplayStatus!!!
07-02 00:05:20.590: I/MediaPlayer(6336): setLPAflag() out
07-02 00:05:20.590: W/MediaPlayer(6336): info/warning (1, 902)
07-02 00:05:20.635: D/MediaPlayer(6336): Mediaplayer receives message, message type: 200
07-02 00:05:20.635: I/MediaPlayer(6336): Info (1,902)
07-02 00:05:20.635: D/MediaPlayer(6336): Mediaplayer receives message, message type: 5
07-02 00:05:20.635: D/MediaPlayer(6336): Mediaplayer receives message, message type: 1
07-02 00:05:20.685: E/(6336): file /data/data/com.nvidia.NvCPLSvc/files/driverlist.txt: not found!
07-02 00:05:20.685: I/(6336): Attempting to load EGL implementation /system/lib//egl/libEGL_tegra_impl
07-02 00:05:20.735: I/(6336): Loaded EGL implementation /system/lib//egl/libEGL_tegra_impl
07-02 00:05:20.820: I/(6336): Loading GLESv2 implementation /system/lib//egl/libGLESv2_tegra_impl
07-02 00:05:21.215: E/SpannableStringBuilder(6336): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-02 00:05:21.215: E/SpannableStringBuilder(6336): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-02 00:05:22.575: E/SpannableStringBuilder(6336): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-02 00:05:22.575: E/SpannableStringBuilder(6336): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
and this is AsyncTank...
这是 AsyncTank...
protected String doInBackground(String... arg0) {
try {
URL url = new URL("http://www.google.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
input = new InputStreamReader(conn.getInputStream());
in =new BufferedReader(input);
System.out.println("HELLO");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
conn.disconnect();
}
return null;
}
采纳答案by Martin
That AsyncTask you posted makes the problem obvious. Commenting out 153 is just hiding the bug.
您发布的 AsyncTask 使问题显而易见。注释掉 153 只是隐藏了错误。
You're declaring and assigning conn
inside the try block (HttpURLConnection conn = (HttpURLConnection) url.openConnection();
), and disconnecting in your finally
.
您在conn
try 块 ( HttpURLConnection conn = (HttpURLConnection) url.openConnection();
) 中声明和分配,并在finally
.
But that conn
you've connected to is not visible from the finally block. That means you probably have a member variable with the same name. You're trying to disconnect that instead.
但是conn
从 finally 块中看不到您已连接到的那个。这意味着您可能有一个同名的成员变量。相反,您正在尝试断开连接。
You're getting a NullPointerExceptionbecause the member is null.
你得到一个NullPointerException因为成员是null。
To start with, I'd suggest changing conn
to something else like conn2
to make the problem more obvious (you'll get a compile error on line 153).
首先,我建议更改conn
为其他内容,例如conn2
使问题更明显(您将在第 153 行收到编译错误)。
The correct solution is to move the declaration and assignment of conn
outside the try block so that it is visible to the finally block, and rejig your catch blocks as appropriate. Something like (I haven't tested this):
正确的解决方案是将conn
try 块之外的声明和赋值移动到 finally 块中,并根据需要重新调整 catch 块。类似的东西(我还没有测试过):
protected String doInBackground(String... arg0) {
try {
URL url = new URL("http://www.google.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
conn.setDoInput(true);
conn.setDoOutput(true);
input = new InputStreamReader(conn.getInputStream());
in =new BufferedReader(input);
System.out.println("HELLO");
}
finally {
conn.disconnect();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
回答by aehs29
I think your problem is that you are trying to do this on the Main UI Thread, you can't do this on ICS or Jellybean, you need to do this inside an AsyncTask, and that way you should be fine.
我认为您的问题是您试图在主 UI 线程上执行此操作,您不能在 ICS 或 Jellybean 上执行此操作,您需要在AsyncTask 中执行此操作,这样您应该没问题。
回答by DigCamara
Seems that if you take a look at the docsback at developer.android.com you'd get your code to work.
似乎如果您查看developer.android.com上的文档,您的代码就可以工作了。
Their sample looks like this:
他们的样本是这样的:
URL url = new URL("http://www.google.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
finally {
urlConnection.disconnect();
}
It's similar to yours, but the log you posted is complaining that
它与您的相似,但是您发布的日志抱怨说
07-02 00:05:16.440: W/System.err(6053): java.io.FileNotFoundException: http://www.google.com/
which leads to this null pointer exception
这导致了这个空指针异常
07-02 00:05:16.460: E/AndroidRuntime(6053): Caused by: java.lang.NullPointerException
which I guess is on this line
我想在这条线上
conn.setDoInput(true);