在按着《第一行代码》这本书里面最后开发天气的那个app里面,在使用到用
HttpUtil.sendOkHttpRequest向服务器发送请求并将相应的数据回调到onResponse(),
但是我在调试之后发现每次都是在
HttpUtil.sendOkHttpRequest(address, new Callback(){ 之后就直接跳到最后了,跳过了
onRespons()和onFailure(),想请问一下有可能是什么情况导致的,谢谢了~~
private void queryFromServer(String address,final String type){
showProgressDialog();
HttpUtil.sendOkHttpRequest(address, new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseText = response.body().string();
boolean result =false;
if("province".equals(type)){
result = Utility.handleProvinceResponse(responseText);
}else if ("City".equals(type)){
result = Utility.handleCityResponse(responseText,selectedProvince.getId());
}else if ("county".equals(type)){
result = Utility.handleCountyResponse(responseText,selectedCity.getId());
}
if(result){
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
closeProgressDialog();
if("province".equals(type)){
queryProvinces();
}else if ("city".equals(type)){
queryCities();
}else if ("County".equals(type)){
queryCounties();
}
}
});
}
}
public class HttpUtil {
public static void sendOkHttpRequest(String address,okhttp3.Callback callback){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(address).build();
client.newCall(request).enqueue(callback);
}
}