2

我正在尝试使用以下代码来获取 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
4

1 回答 1

0

这里发生的事情很常见,您正试图在应用程序的主线程上执行一些长时间运行的操作。

这意味着,主线程负责更新 UI,以强制执行流畅的体验,如果 android 检测到您正在执行一些将阻塞的长时间运行操作,它将强制关闭应用程序,让您知道这很糟糕。

有许多替代解决方案,例如 AsynTask 或 IntentService。

IntentService 是在您向其发送意图时运行的服务:

public class NetworkHandler extends IntentService {
    @Override
    protected void onHandleIntent(Intent intent) {
         //Do your request here
    }
}

要激活 IntentService,只需执行以下操作:

Intent intent = new Intent(mContext, NetworkHandler.class);
startService(intent);
于 2013-04-22T21:52:39.857 回答