The pit encountered by OkHttp

public PersterNetapi(){
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("http://www.weather.com.cn/data/cityinfo/101010100.html")
.build( );

Call call=okHttpClient.newCall(request);

call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
Gson gson = new Gson();
//When the response.body().string() is called, the data stream has been closed, and the call again indicates that it is closed, or no data is sent to the parsing Log.e
("aaaaaaaaaaaa", response.body().string()); //The json below this line is empty
String json= response.body().string().toString();
Mode  mode = gson.fromJson(json, Mode.class);

Mode.WeatherinfoBean weatherinfo = mode.getWeatherinfo();

Log.e(getClass().getCanonicalName(), weatherinfo.city);
Log.e(getClass().getCanonicalName(), weatherinfo.cityid);
Log.e(getClass().getCanonicalName(), weatherinfo.img1);
Log.e(getClass().getCanonicalName(), weatherinfo.img2);
Log.e(getClass().getCanonicalName(), weatherinfo.ptime);
Log.e(getClass().getCanonicalName(), weatherinfo.temp1);
Log.e(getClass().getCanonicalName(), weatherinfo.temp2);
Log.e(getClass().getCanonicalName(), weatherinfo.weather);
}
}
});


}




compile 'com.squareup.okhttp3:okhttp:3.3.0'
compile 'com.google.code.gson:gson:2.8.0'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325941419&siteId=291194637