android - 从应用程序类更改 Activity UI

标签 android user-interface android-activity android-context

我扩展了 Application 类,以便在 android 中创建类似单例的对象。

在这个对象中,我的所有 HTTP 都与我的服务器一起工作,所有其他 Activity 都可以访问它并调用 GET、POST 等方法。

代码:

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

    }

}

这是我从 Activity 调用方法的方式:

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

同样 - 我想访问调用该方法以放置 REQUEST ACCEPTED 消息的 Activity 的 UI。

也许传递上下文?有什么想法吗?

最佳答案

我会创建一个监听器:

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

    }

}

然后,在您的 Activity 中:

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

});

关于android - 从应用程序类更改 Activity UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30441524/

相关文章:

android - 如何连接到android中的特定Wifi连接?

iphone - 如何在 iPhone 上显示加载边框?

java - Netbeans GUI 的运行按钮呈灰色

python - TK 图像未出现

java - Intent .CATEGORY_APP_CALCULATOR : ActivityNotFoundException

android - 如何从 Android 主屏幕小部件启动 Activity

android - ViewPager - 从 Activity 中更新 fragment

java - Http交互打印,不消耗

android - 如何将表格布局垂直分成相等的部分?

android - 在构造函数中仅注入(inject)某些参数