我正在尝试使用以下代码来获取 http 请求。网址是:
字符串 url = " http://vlm1.uta.edu/~zhangzhong/questions.json ";
调用时InputStream source = retrieveStream(url)
,它给了我一个错误。顺便说一句,我已经添加了<uses-permission android:name="android.permission.INTERNET"/>
private InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
}
catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
以下是 LogCat:
04-22 16:36:37.042: D/dalvikvm(3840): 调试器已分离;对象注册表有 427
entries
04-22 16:36:37.042: I/dalvikvm(3840): ignoring registerObject request in thread=1
04-22 16:36:37.042: I/dalvikvm(3840): ignoring registerObject request in thread=1
04-22 16:36:37.042: D/AndroidRuntime(3840): Shutting down VM
04-22 16:36:37.042: W/dalvikvm(3840): threadid=1: thread exiting with uncaught exception (group=0x41760930)
04-22 16:36:37.072: E/AndroidRuntime(3840): FATAL EXCEPTION: main
04-22 16:36:37.072: E/AndroidRuntime(3840): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flashcard/com.example.flashcard.MainActivity}: android.os.NetworkOnMainThreadException
04-22 16:36:37.072: E/AndroidRuntime(3840): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-22 16:36:37.072: E/AndroidRuntime(3840): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-22 16:36:37.072: E/AndroidRuntime(3840): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-22 16:36:37.072: E/AndroidRuntime(3840): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-22 16:36:37.072: E/AndroidRuntime(3840): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 16:36:37.072: E/AndroidRuntime(3840): at android.os.Looper.loop(Looper.java:137)
04-22 16:36:37.072: E/AndroidRuntime(3840): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-22 16:36:37.072: E/AndroidRuntime(3840): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 16:36:37.072: E/AndroidRuntime(3840): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 16:36:37.072: E/AndroidRuntime(3840): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-22 16:36:37.072: E/AndroidRuntime(3840): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-22 16:36:37.072: E/AndroidRuntime(3840): at dalvik.system.NativeStart.main(Native Method)
04-22 16:36:37.072: E/AndroidRuntime(3840): Caused by: android.os.NetworkOnMainThreadException
04-22 16:36:37.072: E/AndroidRuntime(3840): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-22 16:36:37.072: E/AndroidRuntime(3840): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-22 16:36:37.072: E/AndroidRuntime(3840): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-22 16:36:37.072: E/AndroidRuntime(3840): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-22 16:36:37.072: E/AndroidRuntime(3840): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-22 16:36:37.072: E/AndroidRuntime(3840): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-22 16:36:37.072: E/AndroidRuntime(3840): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-22 16:36:37.072: E/AndroidRuntime(3840): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-22 16:36:37.072: E/AndroidRuntime(3840): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-22 16:36:37.072: E/AndroidRuntime(3840): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-22 16:36:37.072: E/AndroidRuntime(3840): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-22 16:36:37.072: E/AndroidRuntime(3840): at com.example.flashcard.MainActivity.retrieveStream(MainActivity.java:63)
04-22 16:36:37.072: E/AndroidRuntime(3840): at com.example.flashcard.MainActivity.onCreate(MainActivity.java:37)
04-22 16:36:37.072: E/AndroidRuntime(3840): at android.app.Activity.performCreate(Activity.java:5104)
04-22 16:36:37.072: E/AndroidRuntime(3840): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-22 16:36:37.072: E/AndroidRuntime(3840): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-22 16:36:37.072: E/AndroidRuntime(3840): ... 11 more
04-22 16:36:37.092: I/Process(3840): Sending signal. PID: 3840 SIG: 9
04-22 16:37:37.727: D/AndroidRuntime(3957): Shutting down VM
04-22 16:37:43.443: D/AndroidRuntime(4077): Shutting down VM
04-22 16:37:43.443: W/dalvikvm(4077): threadid=1: thread exiting with uncaught exception (group=0x41760930)
04-22 16:37:43.443: E/AndroidRuntime(4077): FATAL EXCEPTION: main
04-22 16:37:43.443: E/AndroidRuntime(4077): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flashcard/com.example.flashcard.MainActivity}: android.os.NetworkOnMainThreadException
04-22 16:37:43.443: E/AndroidRuntime(4077): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-22 16:37:43.443: E/AndroidRuntime(4077): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-22 16:37:43.443: E/AndroidRuntime(4077): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-22 16:37:43.443: E/AndroidRuntime(4077): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-22 16:37:43.443: E/AndroidRuntime(4077): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 16:37:43.443: E/AndroidRuntime(4077): at android.os.Looper.loop(Looper.java:137)
04-22 16:37:43.443: E/AndroidRuntime(4077): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-22 16:37:43.443: E/AndroidRuntime(4077): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 16:37:43.443: E/AndroidRuntime(4077): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 16:37:43.443: E/AndroidRuntime(4077): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-22 16:37:43.443: E/AndroidRuntime(4077): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-22 16:37:43.443: E/AndroidRuntime(4077): at dalvik.system.NativeStart.main(Native Method)
04-22 16:37:43.443: E/AndroidRuntime(4077): Caused by: android.os.NetworkOnMainThreadException
04-22 16:37:43.443: E/AndroidRuntime(4077): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-22 16:37:43.443: E/AndroidRuntime(4077): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-22 16:37:43.443: E/AndroidRuntime(4077): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-22 16:37:43.443: E/AndroidRuntime(4077): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-22 16:37:43.443: E/AndroidRuntime(4077): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-22 16:37:43.443: E/AndroidRuntime(4077): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-22 16:37:43.443: E/AndroidRuntime(4077): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-22 16:37:43.443: E/AndroidRuntime(4077): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-22 16:37:43.443: E/AndroidRuntime(4077): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-22 16:37:43.443: E/AndroidRuntime(4077): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-22 16:37:43.443: E/AndroidRuntime(4077): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-22 16:37:43.443: E/AndroidRuntime(4077): at com.example.flashcard.MainActivity.retrieveStream(MainActivity.java:63)
04-22 16:37:43.443: E/AndroidRuntime(4077): at com.example.flashcard.MainActivity.onCreate(MainActivity.java:37)
04-22 16:37:43.443: E/AndroidRuntime(4077): at android.app.Activity.performCreate(Activity.java:5104)
04-22 16:37:43.443: E/AndroidRuntime(4077): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-22 16:37:43.443: E/AndroidRuntime(4077): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-22 16:37:43.443: E/AndroidRuntime(4077): ... 11 more
04-22 16:37:44.865: I/Process(4077): Sending signal. PID: 4077 SIG: 9