android - OkhttpUtil回调解析成功,怎么传出来?
迷茫
迷茫 2017-04-17 17:46:45
0
4
474

代码如下,先会去执行returnUrl,等到第二次调用getHeadUrl该方法才会去到onResponse返回数据。


public static String getHeadUrl(long userid) {
    final String url = ServiceResponseURL.GetLogoUrl(userid);
    OkHttpUtils.get().url(url).build().execute(new StringCallback() {
        @Override
        public void onError(Call call, Exception e, int id) {
            returnUrl = null;
        }
        @Override
        public void onResponse(String response, int id) {
            JSONObject all = JSON.parseObject(response);
            returnUrl = all.getString("imageUrl");
        }
    });
    return returnUrl;
}
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(4)
巴扎黑
    public static void getHeadUrl(long userid,StringCallback callback) {
        final String url = ServiceResponseURL.GetLogoUrl(userid);
        OkHttpUtils.get().url(url).build().execute(callback);
    }

Let Callback丢到外面去。 或者直接接收 Response handle it

小葫芦

If you want to send it out, just use synchronization.

There is no callback parameter in execute(). The return result is generated directly after the .execute() method ends.

 Response response = OkHttpUtils
    .get()//
    .url(url)//
    .tag(this)//
    .build()//
    .execute();

Take a good look at the documentation github

PHPzhong

The answer has been answered upstairs. There are about three methods:

  1. Use the synchronous blocking method to get the return value directly.

  2. Convert asynchronous to synchronous, send the message in the callback, and process it in another thread.

  3. Asynchronous and direct processing, directly handled in the callback.
    It depends on what you do. .

左手右手慢动作

= =~Obviously you made a return before the callback function was executed... It is recommended to write the new StringCallback outside, and then hand over the processing of headerUrl to Callback. . Or just write it like this and call setHeaderUrl(headerUrl) to pass out the headerUrl~~

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template