Android 如何使用异步Http客户端loopj返回值
我目前正在使用loopj Android异步Http客户端,如果有人已经使用过这个库,这只是关于这个库的一个一般性问题 这段代码在我上面给出的示例页面上。有了它,我们可以像tweetText一样从TwitterAPI接收成功的数据。但是,如何在函数onSuccess之外使用tweetText的值呢 我在onSuccess上尝试了很多方法,比如全局变量或更改类型,但没有找到解决方案。我只想在另一个类或函数中获取tweetText的值Android 如何使用异步Http客户端loopj返回值,android,asynchronous,Android,Asynchronous,我目前正在使用loopj Android异步Http客户端,如果有人已经使用过这个库,这只是关于这个库的一个一般性问题 这段代码在我上面给出的示例页面上。有了它,我们可以像tweetText一样从TwitterAPI接收成功的数据。但是,如何在函数onSuccess之外使用tweetText的值呢 我在onSuccess上尝试了很多方法,比如全局变量或更改类型,但没有找到解决方案。我只想在另一个类或函数中获取tweetText的值 class TwitterRestClientUsage {
class TwitterRestClientUsage {
public void getPublicTimeline() throws JSONException {
TwitterRestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// If the response is JSONObject instead of expected JSONArray
}
@Override
public void onSuccess(int statusCode, Header[] headers, 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);
}
});
}
}
提前谢谢你。
扑克试试看
在单独的类中定义静态变量
public class MyConstant
{
public static String tweetText;
}
然后,在onSuccess中,可以按如下方式分配值
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
// Pull out the first event on the public timeline
JSONObject firstEvent = timeline.get(0);
MyConstant.tweetText = firstEvent.getString("text");
// Do something with the response
System.out.println(MyConstant.tweetText);
}
或者使用Android SharedReferences,您可以在这里找到很多教程