2

我正在NetworkOnMainThreadException执行两个网络请求AsyncTask。在第一个请求中,我得到了Response XML,在第二个请求中,我下载了Image InputStream要在BitmapFactory.decodeStream. 请在下面找到异常:

NetworkOnMainThreadException

07-10 08:20:49.961: W/System.err(8570): android.os.NetworkOnMainThreadException
07-10 08:20:49.961: W/System.err(8570):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-10 08:20:49.961: W/System.err(8570):     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
07-10 08:20:49.971: W/System.err(8570):     at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
07-10 08:20:49.971: W/System.err(8570):     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
07-10 08:20:49.971: W/System.err(8570):     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
07-10 08:20:49.971: W/System.err(8570):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
07-10 08:20:49.981: W/System.err(8570):     at java.io.BufferedInputStream.read(BufferedInputStream.java:304)

异步任务

public class FetchDataTask extends AsyncTask {

    @Override
    protected Object doInBackground(Object... object) {
        String url = (String)object[0];
        TASK_TYPE type = (TASK_TYPE)object[1];
        Object[] objects = new Object[4];
        switch(type)
        {
        case FETCH_NOTICES:
            publishProgress("Fetching notices ...");

             // Request # 1 to get the JSON XML

            String data = apiRequestGET(url);
            ArrayList<Notices> notices = extractUserFriendsDataFromXML(data);
            Notices notice = notices.get(0);

            publishProgress("Fetching done.");
            objects[0] = type;
            objects[1] = new ArrayList<Notices>(notices.subList(1, notices.size()));
            objects[2] = notice.getUniversityTitle();
            try {

             // Request # 2 to get the InputStream

                objects[3] = (InputStream) new URL(notice.getUniversityLogo()).getContent();
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            return objects; 
        }

        return null;
    }

    private String apiRequestGET(String requestString)
    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet(requestString);

        String responseText = null;
        try 
        {
            HttpResponse response = httpClient.execute(httpGet, localContext);
            System.out.println("Some Response" +response);
            HttpEntity entity = response.getEntity();
            responseText = EntityUtils.toString(entity);
        }
        catch (Exception e1) 
        {
            return e1.getLocalizedMessage();
        }
        return responseText;
    }

}

谁能告诉我我做错了什么?两个网络请求doInBackground都不能做?

提前致谢!

编辑完整堆栈跟踪

07-10 08:52:18.672: W/System.err(9213): android.os.NetworkOnMainThreadException
07-10 08:52:18.682: W/System.err(9213):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-10 08:52:18.682: W/System.err(9213):     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
07-10 08:52:18.682: W/System.err(9213):     at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
07-10 08:52:18.692: W/System.err(9213):     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
07-10 08:52:18.692: W/System.err(9213):     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
07-10 08:52:18.692: W/System.err(9213):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
07-10 08:52:18.702: W/System.err(9213):     at java.io.BufferedInputStream.read(BufferedInputStream.java:304)
07-10 08:52:18.702: W/System.err(9213):     at libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:45)
07-10 08:52:18.702: W/System.err(9213):     at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:168)
07-10 08:52:18.702: W/System.err(9213):     at java.io.BufferedInputStream.read(BufferedInputStream.java:309)
07-10 08:52:18.712: W/System.err(9213):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
07-10 08:52:18.712: W/System.err(9213):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
07-10 08:52:18.712: W/System.err(9213):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:601)
07-10 08:52:18.722: W/System.err(9213):     at com.alpha.noticeslist.NoticeListActivity.dataFetched(NoticeListActivity.java:145)
07-10 08:52:18.722: W/System.err(9213):     at com.alpha.web.FetchDataTask.onPostExecute(FetchDataTask.java:124)
07-10 08:52:18.732: W/System.err(9213):     at android.os.AsyncTask.finish(AsyncTask.java:631)
07-10 08:52:18.732: W/System.err(9213):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-10 08:52:18.732: W/System.err(9213):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-10 08:52:18.732: W/System.err(9213):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 08:52:18.742: W/System.err(9213):     at android.os.Looper.loop(Looper.java:137)
07-10 08:52:18.742: W/System.err(9213):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-10 08:52:18.752: W/System.err(9213):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 08:52:18.752: W/System.err(9213):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 08:52:18.752: W/System.err(9213):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 08:52:18.752: W/System.err(9213):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-10 08:52:18.762: W/System.err(9213):     at dalvik.system.NativeStart.main(Native Method)

当从onPostExecutein which调用的函数InputStream被传递为:

public void dataFetched(InputStream inputstream)
    {       
            Bitmap bitmap = BitmapFactory.decodeStream(inputstream);
            logo.setImageBitmap(bitmap);
    }

后执行

@Override
    protected void onPostExecute(Object result){
        statusDialog.dismiss();
        Object[] objects = (Object[])result;
        TASK_TYPE type = (TASK_TYPE)objects[0];
        switch(type)
        {
        case FETCH_NOTICES:
        ((NoticeListActivity)callerActivity).dataFetched((InputStream)objects[3]);
        break;
        }
    }
4

4 回答 4

2

我认为您收到了错误,因为尽管您在InputStream后台线程中获得了对decodeStreamonPostExecute

移动呼叫:

Bitmap bitmap = BitmapFactory.decodeStream(inputstream);

进入doInBackground并传递bitmap后面而不是输入流

于 2014-07-10T09:46:32.123 回答
0

确保您正在调用myFetchDataTask.execute(). 您是否直接致电doInBackground(),如果是这样,它将不起作用,如果不是,请发布您如何执行您的AsyncTask.

希望能帮助到你!

于 2014-07-10T08:41:11.333 回答
0

将以下代码用于异步任务:

private class FetchDataTask  extends AsyncTask <String, Void, Void>

{
Object[] objects = new Object[4];
protected void onPreExecute() {
}
@Override
protected Void doInBackground(String... paramsObj) {
    String url = (String)object[0];
    TASK_TYPE type = (TASK_TYPE)object[1];

    switch(type)
    {
    case FETCH_NOTICES:
        publishProgress("Fetching notices ...");

        // Request # 1 to get the JSON XML

        String data = apiRequestGET(url);
        ArrayList<Notices> notices = extractUserFriendsDataFromXML(data);
        Notices notice = notices.get(0);

        publishProgress("Fetching done.");
        objects[0] = type;
        objects[1] = new ArrayList<Notices>(notices.subList(1, notices.size()));
        objects[2] = notice.getUniversityTitle();
        try {

            // Request # 2 to get the InputStream

            objects[3] = (InputStream) new URL(notice.getUniversityLogo()).getContent();
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

    return null;
}

protected void onPostExecute(Void unused) 
{
    // Do action with objects
}

}

在调用方法中,使用:

new FetchDataTask().execute();
于 2014-07-10T08:46:17.180 回答
0

你的班级应该是:

class FetchDataTask extends AsyncTask<Object, String, Void> 
    {

        @Override
        protected Void doInBackground(Object... params) {
            //code here

            publishProgress("");

            return null;
        }

        @Override
        protected void onProgressUpdate(String... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
        }
    }

你可以像这样执行new FetchDataTask.execute([your parameter]);

于 2014-07-10T08:50:14.830 回答