0

在我的应用程序中,我正在打开一个连接以加载数据。在手机设备(galaxy 2)和平板电脑 p1000(带有 os 2.2 的旧 7" 平板电脑)中我没有问题,我可以获取和解析数据。但在另一台平板电脑(三星 7" plus - Honeycomb)中,应用程序崩溃了。logcat 说:

04-05 16:31:33.905: E/AndroidRuntime(4137): FATAL EXCEPTION: main
04-05 16:31:33.905: E/AndroidRuntime(4137): android.os.NetworkOnMainThreadException
04-05 16:31:33.905: E/AndroidRuntime(4137):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at java.net.InetAddress.getAllByName(InetAddress.java:249)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at cam.astro.mania.adapters.NewsAdapter.fetchImages(NewsAdapter.java:105)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at cam.astro.mania.adapters.NewsAdapter.setData(NewsAdapter.java:45)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at com.astro.mania.activities.NewsList.displayData(NewsList.java:359)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at com.astro.mania.activities.NewsList.access$4(NewsList.java:358)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at com.astro.mania.activities.NewsList$MyAsyncTask.onPostExecute(NewsList.java:199)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at com.astro.mania.activities.NewsList$MyAsyncTask.onPostExecute(NewsList.java:1)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at android.os.AsyncTask.finish(AsyncTask.java:590)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at android.os.AsyncTask.access$600(AsyncTask.java:149)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:603)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at android.os.Looper.loop(Looper.java:132)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at android.app.ActivityThread.main(ActivityThread.java:4123)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at java.lang.reflect.Method.invokeNative(Native Method)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at java.lang.reflect.Method.invoke(Method.java:491)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
04-05 16:31:33.905: E/AndroidRuntime(4137):     at dalvik.system.NativeStart.main(Native Method)

我的代码是:

/*-------------------------------
     * Downloading images from server
     * ------------------------------*/
    private Bitmap[] fetchImages(ArrayList<String> urlstr){
        InputStream is= null;
        Bitmap bm = null;
        Bitmap[] bmList = new Bitmap[urlstr.size()];

        try {
            for(int i=0; i<urlstr.size(); i++){
                HttpGet httpRequest = new HttpGet(urlstr.get(i));
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

                HttpEntity entity = response.getEntity();
                BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
                is = bufHttpEntity.getContent();
                bm = BitmapFactory.decodeStream(is); 
                bmList[i] = bm;
            }
        }catch ( MalformedURLException e ){
            Log.d( "RemoteImageHandler", "fetchImage passed invalid URL: " + urlstr );
        }catch ( IOException e ){
            Log.d( "RemoteImageHandler", "fetchImage IO exception: " + e );
        }finally{
            if(is!=null)try{
                is.close();
            }catch(IOException e){}
        }

        return bmList;
    }

Logcat指向HttpResponse response = (HttpResponse) httpclient.execute(httpRequest); 我不明白错误的含义是什么。问题是什么?

谢谢你的帮助。

4

3 回答 3

1

此错误表示您不允许在主线程中执行网络操作。

您需要在另一个线程中执行此操作并异步获取结果。

希望能帮助到你。

乔卡赫罗

于 2012-04-05T10:13:18.797 回答
0

开始一个新线程供您下载并在那里执行。通过消息通知您的主线程到 Handler或只是等待线程完成使用 join()

于 2012-04-05T10:18:35.497 回答
0

您可以使用此代码将图像加载到图像视图。

 ImageView ImageView =(ImageView)dialog.findViewById(R.id.ImageView01);
   InputStream is = null;
   try {
        is = (InputStream) new URL(ClueImgURL).getContent();
    } catch (MalformedURLException e) {
              // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
  Drawable d = Drawable.createFromStream(is, "src name");
  ImageView .setBackgroundDrawable(d);

这里的 ClueImgURL 是图像的 url。

于 2012-04-09T06:06:58.173 回答