我需要通过onResponse
调用中的okhttp
方法获取字符串,但我无法更改onResponse
内的值。我的代码在
public class OkHttpTest {
private static final String TAG = "OkHttpTest";
OkHttpClient mClient = new OkHttpClient();
public String getMarkets(String url) throws IOException {
public static String mString;
Request mRequest = new Request.Builder()
.url(url)
.build();
Call mCall = mClient.newCall(mRequest);
mCall.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.d(TAG, "onFailure: call failed");
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
Log.d(TAG, "onResponse: ok");
mString = response.body().string(); // cannot access
} else return;
}
});
return mString; //
}
}
我这样打电话
String result = test.getMarkets(url);
有什么方法可以让我的工作吗?