代码如下,先会去执行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;
}
Let
Callback
丢到外面去。 或者直接接收Response
handle itIf 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.
Take a good look at the documentation github
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. .
= =~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~~