0

I have a fragment in which I start the thread. In this thread I get a object and after that I want to pass the object to the main thread. What shall I do for this?

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();
}}

I need to pass jsonObj back to the main thread?

Bogdan Emil Mariesan
  • 5,529
  • 2
  • 33
  • 57
Delphian
  • 1,650
  • 3
  • 15
  • 31

2 Answers2

0

you can try using the Join method in Thread. Other way you can do this also in a multi-threading program is to Synchronized the Object with you want to share between Threads. by synchronizing the object , you must then first allow other thread that will finished manupulating the Object to have access first, and you will now later assign the object back to the main thread. in this case if the current Thread handling the Object is not yet through with the Object any attempt by other thread will result into a wait. But when the Current Thread is through handling the object, Other thread can now have access to it

Joseph Peter
  • 133
  • 7
  • But if I'ii use mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0, jsonObj.toString())); and then get the object use msg.getData().getString()? It will be right? – Delphian Sep 21 '16 at 11:32
-1

Use interface to send object as call back.

 private void getRecentlyTag(final OnResponseListener listener){

  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();
        if(listener!=null){
          listener.onResponseReceived(jsonObj);
        }

    }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();
} }

interface OnResponseListener{
 void onResponseReceived(JSONObject obj);
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Ganesh Kanna
  • 2,269
  • 1
  • 19
  • 29