Android 如何使用OkHttp 从Callback 获取响应字符串?
Android how to get response string from Callback using OkHttp?
这是我的代码:
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
Callback callback = new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
}
};
okHttpClient.newCall(request).enqueue(callback);
String responseString;
在上面的代码中,我想将来自 onResponse() 方法的 response.body().string() 的值存储在变量 responseString 中,但是我无法访问它。
如果您将 responseString
声明移动为实例变量,那么您将能够在 Callback
的 onResponse
方法中为其赋值。
public class MyClass {
private String responseString;
// your class implementation
}
我已经修改了您发布的代码,并进行了以下必要的更改:
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
Callback callback = new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
responseString = response.body().string();
}
};
okHttpClient.newCall(request).enqueue(callback);
第一个答案很接近,但您不能在方法 onResponse 中分配 final 变量。解决方法是键入 final String[] responseString = new String[1];
并分配 responseString[0] = response.body().string();
我想你想做的是这样的:
public class MyActivity extends Activity implements Callback , View.OnClickListener {
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
findViewById(R.id.DoHttp).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId(() == R.id.DoHttp) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
okHttpClient.newCall(request).enqueue(this);
}
}
@Override
public void onFailure(Request request, IOException e) {
//do something to indicate error
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
parse(response.body().string());
}
}
private void parse(String response) {
//do something with response
}
}
在上面的 activity 中,我们实现了回调,然后当我们创建 okhttp 请求时,我们将我们自己的实例 (this) 传递给它,这样我们就可以让 oktthp 回调 class,我们本可以同样轻松地完成内部 class,但这减少了我们必须制作的 classes 的数量。我使用按钮单击来说明何时进行 Http 调用,但这可能是其他时间,例如,它可能发生在首次创建屏幕时(在 onCreate 中)。小心屏幕旋转。此外,这假设回调是在主线程上完成的,我认为它会是这样,但我并不肯定,因为我使用 okttp 的方式与你不同。如果它没有 return 主线程响应的结果,那么您可以调用 runOnUiThread() 并向它传递一个 Runnable 来完成更新视图的工作。
这是我的代码:
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
Callback callback = new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
}
};
okHttpClient.newCall(request).enqueue(callback);
String responseString;
在上面的代码中,我想将来自 onResponse() 方法的 response.body().string() 的值存储在变量 responseString 中,但是我无法访问它。
如果您将 responseString
声明移动为实例变量,那么您将能够在 Callback
的 onResponse
方法中为其赋值。
public class MyClass {
private String responseString;
// your class implementation
}
我已经修改了您发布的代码,并进行了以下必要的更改:
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
Callback callback = new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
responseString = response.body().string();
}
};
okHttpClient.newCall(request).enqueue(callback);
第一个答案很接近,但您不能在方法 onResponse 中分配 final 变量。解决方法是键入 final String[] responseString = new String[1];
并分配 responseString[0] = response.body().string();
我想你想做的是这样的:
public class MyActivity extends Activity implements Callback , View.OnClickListener {
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
findViewById(R.id.DoHttp).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId(() == R.id.DoHttp) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
okHttpClient.newCall(request).enqueue(this);
}
}
@Override
public void onFailure(Request request, IOException e) {
//do something to indicate error
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
parse(response.body().string());
}
}
private void parse(String response) {
//do something with response
}
}
在上面的 activity 中,我们实现了回调,然后当我们创建 okhttp 请求时,我们将我们自己的实例 (this) 传递给它,这样我们就可以让 oktthp 回调 class,我们本可以同样轻松地完成内部 class,但这减少了我们必须制作的 classes 的数量。我使用按钮单击来说明何时进行 Http 调用,但这可能是其他时间,例如,它可能发生在首次创建屏幕时(在 onCreate 中)。小心屏幕旋转。此外,这假设回调是在主线程上完成的,我认为它会是这样,但我并不肯定,因为我使用 okttp 的方式与你不同。如果它没有 return 主线程响应的结果,那么您可以调用 runOnUiThread() 并向它传递一个 Runnable 来完成更新视图的工作。