【发布时间】:2019-01-16 00:45:27
【问题描述】:
我正在尝试用 Java 中的 OkHttp 发出 Get 请求。我使用异步线程成功实现了它,但我“卡”在 onResponse 函数中。我的意思是,除了这个函数,我不能在其他任何地方使用请求的结果。我该怎么做才能使这段代码工作?
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://raw.github.com/square/okhttp/master/README.md")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String responseData = response.body().string();
LoginInscription.this.runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tv = findViewById(R.id.txtString);
tv.setText(responseData);
}
});
}
});
TextView tv = findViewById(R.id.txtString);
String res = tv.getText().toString();
System.out.println(res);
谢谢...
【问题讨论】: