search

Home  >  Q&A  >  body text

android - OkhttpUtil回调解析成功,怎么传出来?

代码如下,先会去执行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;
}
迷茫迷茫2773 days ago477

reply all(4)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 17:48:45

        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

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:48:45

    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

    reply
    0
  • PHPz

    PHPz2017-04-17 17:48:45

    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. .

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:48:45

    = =~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~~

    reply
    0
  • Cancelreply