android.os.NetworkOnMainThreadException

时间:2014-11-10 04:11:52

标签: android android-asynctask

我正在尝试将url设置为我的通知的setLargeIcon,但在执行此操作时我得到了android.os.NetWorkOnMainThreadException错误,我看到一些帖子提到使用AsyncTask,但我不知道如何将其实现到我的代码中。

@Override
public void onReceive(final Context context, Intent intent) {
    Log.d(TAG, " START");
    try {
        if (intent == null)
        {
            Log.d(TAG, "Receiver intent null");
        }
        else
        {
                Log.d(TAG,intent.toString());
                String action = intent.getAction();
                Log.d(TAG, "got action " + action );
                String channel = intent.getExtras().getString("com.parse.Channel");
                JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

                Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
                Iterator itr = json.keys();
                while (itr.hasNext()) {
                    String key = (String) itr.next();

                    Log.d(TAG, "..."+key+ "=>" +json.getString(key));
                    if (key.equals("customdata"))
                    {
                        Log.d(TAG,"1.0");
                        msg=json.getString(key);    
                        Log.d(TAG,msg.toString());
                    }
                    Log.d(TAG,"1.1");
                    if(key.equals("image_url"))
                    {
                        msg1=json.getString(key);       
                        Log.d("msg1",msg1.toString());
                    }
                }

                Bitmap bitmap = getBitmapFromURL(msg1);


    }           
    } catch (JSONException e) {
        Log.d(TAG, "JSONException: " + e.getMessage());
    }        
}
public Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

2 个答案:

答案 0 :(得分:2)

您可以将AsyncTask用作:

class LoadBitmaps extends AsyncTask<String, Void, Void> {


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        // do something  // show some progress of loading images
    }

    @Override
    protected Void doInBackground(String... str) {

        try {
            URL url = new URL(str[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);

        } catch (IOException e) {
            e.printStackTrace();

        }
        return null;
    }


    @Override
    protected void onPostExecute(Void v) {
        // do something
    }


}

答案 1 :(得分:0)

发生此异常是因为您在主题中从网络获取数据。将其移至AsyncTask's onBackgroundThread,以便获取数据的过程将在background thread。示例(使用Thread):

Thread thread = new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            getBitmapFromURL(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

thread.start();

如果您不确定是使用Thread还是AsyncTask,请查看此处:

Asynctask vs Thread in android