我有一個片段,我開始線程。在這個線程中,我得到一個對象,然後我想將對象傳遞給主線程。我應該爲此做些什麼?如何將對象從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 –