代码如下,先会去执行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;
}
巴扎黑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
高洛峰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
PHPz2017-04-17 17:48:45
The answer has been answered upstairs. There are about three methods:
Use the synchronous blocking method to get the return value directly.
Convert asynchronous to synchronous, send the message in the callback, and process it in another thread.
Asynchronous and direct processing, directly handled in the callback.
It depends on what you do. .
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~~