android中okhttp怎么处理异常
1个回答
展开全部
下面是获取到json字符串。 enqueue就是异步请求的意思。
OkHttpClient mHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url)
.build();
mHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
//加载失败
}
@Override
public void onResponse(Response response) throws IOException {
//成功获取数据
if (response.isSuccessful()){
String json = response.body().toString();
Log.d("mainai", "onResponse() returned: " + json);
}
}
});
OkHttpClient mHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url)
.build();
mHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
//加载失败
}
@Override
public void onResponse(Response response) throws IOException {
//成功获取数据
if (response.isSuccessful()){
String json = response.body().toString();
Log.d("mainai", "onResponse() returned: " + json);
}
}
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询