【问题标题】:Call method from onSuccess to another class从 onSuccess 调用方法到另一个类
【发布时间】:2014-02-27 17:05:41
【问题描述】:

我是 java/android 开发新手,如果可以,请提供帮助。

我想要一个方法,它可以从 Web 服务(异步)获取数据,一旦完成(onSuccess 或 onFailure),它应该调用活动类的另一个方法。 假设我有 WebManager 类,这不是真正的方法,我还要添加更多内容以及 try/catch 等等:

    public class WebManager {

            public MyObject1 getObject1() {
                AsyncHttpClient client = new AsyncHttpClient();

                client.get(QUERY_URL,
                        new JsonHttpResponseHandler() {

                            @Override
                            public void onSuccess(JSONObject jsonObject) {

                                 //Call method on my Activity and pass jsonObject (in the other method I want to pass my custom object instead of json)
                            }

                            @Override
                            public void onFailure(int statusCode, Throwable throwable, JSONObject error) {

                                //Call method on my Activity and pass statusCode, throwable and error
                            }
                        });
            }

            public MyObject2 getObject2() {
                AsyncHttpClient client = new AsyncHttpClient();
                // The same as above
            }
        }

现在在我的 MainActivity 中,我想调用 getObject1() ,当下载数据时,我想调用方法:

WebManager wm = new WebManager();
//... 


protected void onObject1DataDownload(JSONObject jsonObject) {
    //Do something with the data
}

protected void onObject2DataDownload(CUSTOMOBJECT obj) {
    //Do something with the data
}

我怎样才能实现这样的目标? 提前致谢

【问题讨论】:

    标签: java android


    【解决方案1】:
    1. 创建接口

      public interface ResponseListener {
      void onObject1DataDownload(JSONObject jsonObject);
      }
      
    2. 将WebManager中的getObject1()方法改为参数化方法,并调用回调方法。

      public class WebManager {
      public MyObject1 getObject1(ResponseListener listener) {
          AsyncHttpClient client = new AsyncHttpClient();
      
          client.get(QUERY_URL,
                  new JsonHttpResponseHandler() {
      
                      @Override
                      public void onSuccess(JSONObject jsonObject) {
      
                           //here's the change
                           listener.onObject1DataDownload(jsonObject);
                      }
      
                      @Override
                      public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
      
                          //Call method on my Activity and pass statusCode, throwable and error
                      }
                  });
      }
      
    3. 在你的活动中实现接口

      public class MainActivity implements ResponseListener{
      
      public void someMethod() {
       WebManager wm = new WebManager(); 
       //pass the activity as the listener
       wm.getObject1(this);
      
      }
      protected void onObject1DataDownload(JSONObject jsonObject) { 
      //this method will be called }
      
      protected void onObject2DataDownload(CUSTOMOBJECT obj) { //Do something with the data }
      
      }
      

    同样你可以为 onFailure 方法注册一个回调。

    【讨论】:

    • 谢谢,这就是我要找的。还有一个问题,如果我想这样调用我的方法怎么办:wm.getObject1(this, public void onSuccess(JSONObject jsonObject) {//My code here});
    • Ohkie 所以你要做的就是传递一个方法作为参数。您可以使用反射来做到这一点。但不建议这样做,而是我们使用接口。看看en.wikipedia.org/wiki/Strategy_patternen.wikipedia.org/wiki/Command_pattern
    • 感谢我使用 intercase,它对我来说效果很好。
    【解决方案2】:

    更好的方法可能是使用处理程序和消息。

    http://developer.android.com/reference/android/os/Handler.html

    或者至少不要忘记通过以下方式在 UI 线程中运行 UI 相关代码:

        runOnUiThread(new Runnable() {
    
            @Override
            public void run() {
                // code
    
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 2011-09-09
      相关资源
      最近更新 更多