3

I extended the Application class in order to create singleton-like object in android.

in this object I have all the HTTP work with my server, and all the other activities can access it and call methods to GET, POST etc.

Code:

public class HttpManagerInstance extends Application {
    private HttpClient httpClient;
    private HttpGet get;

    @Override
    public void onCreate() {
        httpClient = new DefaultHttpClient();
        get = new HttpGet("http://10.100.102.9:8000/users/");
        super.onCreate();

    }


    public Void getUsers() throws Exception {
        new executeRequest().execute(get);
        return null;
    }

    private class executeRequest extends AsyncTask<HttpRequest, Void, Integer> {

        @Override
        protected Integer doInBackground(HttpRequest... params) {
            // TODO Auto-generated method stub
            HttpRequest request = params[0];
            HttpResponse response;
            String result="";
            try {
                response = httpClient.execute((HttpUriRequest) request);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return responseCode;
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            switch (result) {
            case HttpStatus.SC_OK:
                // request was fine

                // Here I want to updated the GUI of the activity that called this method.
                break;
            }
        }

    }

}

This is how I call the method from the Activity:

HttpManagerInstance sampleApp = (HttpManagerInstance)getApplicationContext();
sampleApp.getUsers();

Again - I want to access the UI of the Activity that called the method to put an REQUEST ACCEPTED message.

Maybe pass a context? any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ofek Agmon
  • 5,040
  • 14
  • 57
  • 101

3 Answers3

4

I'd create a listener:

public class HttpManagerInstance extends Application {
    private HttpClient httpClient;
    private HttpGet get;

    public interface ResponseListener{
      public void onSuccess(Object data);
    }


    @Override
    public void onCreate() {
        httpClient = new DefaultHttpClient();
        get = new HttpGet("http://10.100.102.9:8000/users/");
        super.onCreate();

    }


    public Void getUsers(ResponseListener listener) throws Exception {
        new executeRequest(listener).execute(get);
        return null;
    }

    private class executeRequest extends AsyncTask<HttpRequest, Void, Integer> {

        private ResponseListener mListener;

        public executeRequest(ResponseListener listener){
         this.mListener = listener;
        }

        @Override
        protected Integer doInBackground(HttpRequest... params) {
            // TODO Auto-generated method stub
            HttpRequest request = params[0];
            HttpResponse response;
            String result="";
            try {
                response = httpClient.execute((HttpUriRequest) request);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return responseCode;
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            switch (result) {
            case HttpStatus.SC_OK:
                // request was fine

                // Here I want to updated the GUI of the activity that called this method.
                if(this.mListener != null) mListener.onSuccess(whatEverDataYouWant);
                break;
            }
        }

    }

}

Then, in your activity:

    HttpManagerInstance sampleApp = (HttpManagerInstance)getApplicationContext();
    sampleApp.getUsers(new ResponseListener(){
       public void onSuccess(Object data){
         //update your ui!
       }

});
mmark
  • 1,204
  • 12
  • 19
  • thanks for your answer. it looks really good, I will try it. BTW - I think that you had a small mistake - in the Activity code there is ResponseListener which is the Interface and in the AsyncTask you wrote RequestListener which doesn't exist. Thanks again! – Ofek Agmon May 25 '15 at 15:53
  • @OfekAgmon thanks for the correction! i just fixed it! Let me know how it went. You're welcome! – mmark May 25 '15 at 16:07
0

The short answer is you can't directly reference to the UI from another activity. My advice would be for you to set up a callback on your Application class and call in on executeRequest#onPostExecute then implement that callback on your Activity and update your UI from there.

If you need help to implement the callback check this question

Community
  • 1
  • 1
Carlos J
  • 2,965
  • 4
  • 17
  • 28
0

If you need to show message is good option the Dialog Class or the Toast Class, you can see more info are here: Dialogs: http://developer.android.com/guide/topics/ui/dialogs.html Toasts: http://developer.android.com/guide/topics/ui/notifiers/toasts.html

But if you want to access or modify a control in your actual activity, then use Runnable class, and context.runOnUiThread() method if you work inside AsyncTask. The real problem is that you can't change UI in a AsyncTask using declaration of the controls. You need to throw a Runnable process to communicate with activity!!. For example:

context.runOnUiThread(new Runnable() {
                public void run() {
                    //Declaration of variables
                    TextView MyTextView = (TextView) context.findViewById(R.id.txtvMyControl);

                    MyTextView.setText("My title");
                }
}

If I can helps you say me, good luck!