return 值如何与异步 Http 客户端 loopj
How return value with Asynchronous Http Client loopj
我目前正在使用 loopj Android 异步 Http 客户端(http://loopj.com/android-async-http/)如果有人已经使用过这个库,这只是一个关于这个库的一般问题。
这段代码在我上面给出的示例页面上。有了这个,我们可以在 onSuccess 中从 TwitterAPI 接收数据,比如 tweetText。但是怎么可能在函数 onSuccess 之外使用 tweetText 的值呢?
我尝试了很多方法,例如全局变量或在 onSuccess 上更改类型,但我没有找到解决方案。我只想在另一个 class 或函数中使用 tweetText 的值...
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);
}
});
}
}
提前致谢。
扑克
尝试,
在单独的文件中定义静态变量class
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 SharedPreferences,我可以在这里找到很多教程
我目前正在使用 loopj Android 异步 Http 客户端(http://loopj.com/android-async-http/)如果有人已经使用过这个库,这只是一个关于这个库的一般问题。
这段代码在我上面给出的示例页面上。有了这个,我们可以在 onSuccess 中从 TwitterAPI 接收数据,比如 tweetText。但是怎么可能在函数 onSuccess 之外使用 tweetText 的值呢?
我尝试了很多方法,例如全局变量或在 onSuccess 上更改类型,但我没有找到解决方案。我只想在另一个 class 或函数中使用 tweetText 的值...
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);
}
});
}
}
提前致谢。 扑克
尝试,
在单独的文件中定义静态变量class
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 SharedPreferences,我可以在这里找到很多教程