我有一个片段,我开始线程。在这个线程中,我得到一个对象,然后我想将对象传递给主线程。我应该为此做些什么?如何将对象从Android中的其他线程传递回主线程?
public class IFragment extends Fragment {
private void getRecentlyTag(){
new Thread(){
@Override
public void run() {
HttpURLConnection urlConnection = null;
try {
URL url = new URL(Constants.API_URL);
urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.connect();
String response = Tools.streamToString(urlConnection
.getInputStream());
JSONObject jsonObj = (JSONObject) new JSONTokener(response)
.nextValue();
}catch(Exception exc){
exc.printStackTrace();
}finally {
if(urlConnection!=null){
try{
urlConnection.disconnect();
}catch(Exception e){
e.printStackTrace();
}
}
}
// mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0));
}
}.start();
}}
我需要将jsonObj传递回主线程?
http://stackoverflow.com/questions/11140285/how-to-use-runonuithread –