我正在使用这个库: 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);
}
});
}
}