0

我正在使用这个库: http: //loopj.com/android-async-http/来发出我所有的请求 && 目前让我的代码混乱的最大事情是我无法成功地从 onSuccess 中获取我的异步 Http 请求响应() 以便其他班级可以自由使用数据。到目前为止,我一直在我的响应处理程序 onSuccess(){} 的卷曲中解析我的所有请求响应

我在 Stack 上看到了这篇文章:How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

但还没有让它与这个库一起工作。

import com.loopj.android.http.*;

public class TwitterRestClient {
  private static final String BASE_URL = "http://api.twitter.com/1/";

  private static AsyncHttpClient client = new AsyncHttpClient();

  public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      client.get(getAbsoluteUrl(url), params, responseHandler);
  }

  public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      client.post(getAbsoluteUrl(url), params, responseHandler);
  }

  private static String getAbsoluteUrl(String relativeUrl) {
      return BASE_URL + relativeUrl;
  }
}




import org.json.*;
import com.loopj.android.http.*;

class TwitterRestClientUsage {
    public void getPublicTimeline() throws JSONException {
        TwitterRestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(JSONArray timeline) {
                // Pull out the first event on the public timeline
                JSONObject firstEvent = timeline.get(0);
                String tweetText = firstEvent.getString("text");

                // Do something with the response
                System.out.println(tweetText);
            }
        });
    }
}
4

2 回答 2

0

我不知道这是否有帮助,但我不得不面对同样的问题(我是 java 和 android 的新手)。使用了一个接口。http://www.gdgankara.org/2013/03/25/android-asynchronous-http-client-a-callback-based-http-client-library-for-android-and-android-smart-image-view/

于 2013-09-13T03:33:23.710 回答
0

尝试这个:

public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
                client.get(getAbsoluteUrl(url), params, responseHandler);
                String response = new String(responseHandler);
                Log.i("RESPONSE", response);
            }

于 2019-04-29T22:39:27.803 回答