簡體   English   中英

如何使用異步Http客戶端loopj返回值

[英]How return value with Asynchronous Http Client loopj

我目前正在使用loopj Android異步Http客戶端( http://loopj.com/android-async-http/ ),如果有人已經使用過這個庫,這只是關於此庫的一個普遍問題。

這段代碼在我上面給出的示例頁面上。 這樣,我們就可以像tweetText一樣從onSuccess中的TwitterAPI接收數據。 但是如何在onSuccess函數外部使用tweetText的值呢?

我在onSuccess上嘗試了很多類似全局變量或更改類型的方法,但沒有找到解決方案。 我只想要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);
            }
        });
    }
}

謝謝你提前。 撲克

試試看

  1. 在單獨的類中定義靜態變量

     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);
        }
  1. 否則使用Android SharedPreferences,您會在這里找到很多教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM