处理程序线程 - NetworkOnMainThread异常(android.os.handler)

时间:2016-01-19 12:32:00

标签: android multithreading android-fragments android-handler android-handlerthread

这与上面发布的问题不重复。当HAndlerThread启动newThread并且我得到NetworkOnMainThread异常时,我认为这是一个奇怪的错误。解决正常的NetworkOnMainThread异常是一个全新的问题。请注意这一点。

首先,我正在练习HandlerThread。我已经使用Fragment来启动一个handletraread并向该线程发送一条消息。在HandlerThread的Handle消息中,我使用网络调用来下载图像。然后我使用MainThread的Handler将下载的图像发布到UI线程。但是当运行这个应用程序时,我得到一个NetworkOnMainThread Exception.Below是Fragment和Handler Thread的代码。

片段:

public class LoaderFragment extends Fragment {
Button loaderButton;
LoaderThread loadThread;
Handler loaderThreadHandler;
Handler mainHandler;
String[] queArray;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mainHandler = new Handler(Looper.getMainLooper());
    loadThread = new LoaderThread("LoaderThread",mainHandler);
     queArray = new String[]{"http://www.paynekillers.com/downloads/screens/17_screen05.jpg",
            "http://www.hdwallpapers.in/walls/jacob_frye_assassins_creed_syndicate-wide.jpg",
    "http://www.percivalconstantine.com/wp-content/uploads/2015/03/Tomb-Raider-Lara-Croft-Summit-2013.jpg",
    "http://orig00.deviantart.net/f904/f/2014/155/e/a/batman_arkham_knight_hd_wallpaper_1_by_rajivcr7-d7l19pt.jpg",
    "http://static5.gamespot.com/uploads/original/536/5360430/2753139-15774024117_9026e0a43c_o.jpg"};
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View loaderView = inflater.inflate(R.layout.loader_layout, container, false);
    this.loaderButton = (Button) loaderView.findViewById(R.id.proc_button);
    return loaderView;
}

@Override
public void onResume() {
    super.onResume();



    loaderButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            loadThread.start();
            loaderThreadHandler = loadThread.requestHandler();
            Message msg = Message.obtain(loaderThreadHandler, 1, queArray);
            msg.sendToTarget();
        }
    });
}

@Override
public void onDestroy() {
    super.onDestroy();
    loadThread.quit();
} }

处理程序线程:

public class LoaderThread extends HandlerThread implements Handler.Callback{
Handler imageHandler;
Handler loaderHandler;
Map<Integer,String> queMap;
HandlerActivity activity;


public LoaderThread(String name, Handler handler) {
    super(name);
    imageHandler= handler;
    Log.d("inside : ", name);
}

Handler requestHandler(){
    loaderHandler = new Handler(this.getLooper(),this);
    return loaderHandler;
}


@Override
public boolean handleMessage(Message msg) {
    String[] que = (String[]) msg.obj;
    queMap = new HashMap<Integer,String>();
    if(que != null){Log.d("",que[3]);}
    for (int i=0;i<=4; i++){
        String url = que[i];

        queMap.put(i,url);
        Log.d("1",url);
    }
    Log.d("THR : ", String.valueOf(Thread.currentThread()));
    Random rand = new Random();
   String link = queMap.get(rand.nextInt(5));

    loadImages(link);
    return true;
}

void loadImages(final String url){
    Log.d("inside Load :","Inside Load");
    try {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        final Bitmap image = BitmapFactory.decodeStream(connection.getInputStream());
        imageHandler.post(new Runnable() {
            @Override
            public void run() {
                activity.loadImage(image, url);
            }
        });

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

Activity的loadImage方法:

public void loadImage(Bitmap image, String url) {
    Bitmap img = image;
    String link = url;
    ft = fmg.beginTransaction();
    ft.add(R.id.frag_container,imgFrag,"ImageFragment");
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.show(imgFrag);
    ft.commit();
    imgFrag.loadImg(img,link);

}

当我使用log来检查我所在的线程(在HandlerThread中的handleMessage中)时,我得到一条日志消息,因为我在Main中。这怎么可能?我做错了吗?

3 个答案:

答案 0 :(得分:0)

LoaderFragment更改

mainHandler = new Handler(Looper.getMainLooper());
loadThread = new LoaderThread("LoaderThread",mainHandler);

loadThread = new LoaderThread("LoaderThread");

然后在LoaderThread

public LoaderThread(String name, Handler handler) {
     super(name);         
     Log.d("inside : ", name);
}
 @Override
 protected void onLooperPrepared() {
     imageHandler = new Handler(getLooper(), this);
  }

您正在收到错误,因为您正在将UI Thread Looper传递给LoaderThread,而您在requestHandler()中再次传递以进行网络访问。因此,您的网络操作会被UI Thread Looper队列化。

答案 1 :(得分:0)

我遇到了同样的问题。然后我在名为HandlerThread's的overriden方法中设置onLooperPrepared()处理程序,并且不再接收该异常,因此您应该尝试在LoaderThread类中执行类似的操作:< / p>

@Override
protected void onLooperPrepared() {
    super.onLooperPrepared();
    loaderHandler = new Handler(this.getLooper(),this);
}
根据{{​​3}},

onLooperPrepared引用:如果需要在Looper循环之前执行某些设置,则回调可以显式覆盖的方法。请告诉我这是否适合您。

答案 2 :(得分:-3)

对于Android中的任何网络操作,建议在AsyncTask中编写该代码。