gpt4 book ai didi

java - JSONException 、 JSONObject 异常错误

转载 作者:行者123 更新时间:2023-12-01 11:38:13 30 4
gpt4 key购买 nike

描述:在 OnRespose 方法中,我可以在日志中看到从 froecast.io 接收的数据,但是当我传递“response.body().string()”时,即来自 Forecast.io 的数据作为参数(字符串)传递给方法 getCurrentDetails 时方法正在接收 null 并且 JSONObject 正在抛出空指针异常。

尝试:通过多个catch block 处理异常。

    double latitude = -122.423;
double longitude = 37.8267;
String apiKey = "cac63237c81f5312b496ed8cce991b40";
String forecastURL = "https://api.forecast.io/forecast/"+apiKey+"/"+longitude+","+latitude+"";
if(isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(forecastURL).build();

Call call = client.newCall(request);

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

}

@Override
public void onResponse(Response response) throws IOException {
try {
Log.v(TAG, response.body().string());
if (response.isSuccessful()) {
mCurrentWeather = getCurrentDetails(response.body().string());

} else {
alertUserAboutProblem();
}
} catch (IOException e) {
Log.e(TAG, "Exception Caught: ", e);
}
catch(JSONException e){
Log.e(TAG,"Exception Caught: ", e);
}
}
});

Log.e(TAG, "Program is still running");
}
else{
Toast.makeText(MainActivity.this,"Network_Unavaliable",Toast.LENGTH_LONG).show();
}



}

private CurrentWeather getCurrentDetails(String string) throws JSONException {

------>>>>> JSONObject jsonObject = new JSONObject(string); String timezone = jsonObject.getString("timezone");

    JSONObject currently =jsonObject.getJSONObject("currently");

CurrentWeather currentWeather = new CurrentWeather();

currentWeather.setHumidity(currently.getDouble("humidity"));
currentWeather.setIcon(currently.getString("icon"));
currentWeather.setSummary(currently.getString("summary"));
currentWeather.setTemperature(currently.getDouble("temperature"));
currentWeather.setTime(currently.getLong("time"));
currentWeather.setPrecipIntensity(currently.getDouble("percipIntensity"));
currentWeather.setTimeZone(timezone);

Toast.makeText(MainActivity.this,String.valueOf(currently.getLong("time")),Toast.LENGTH_LONG).show();
// Log.d(TAG, String.valueOf(currentWeather.getTime()));
return currentWeather;
}

private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo coninfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if(coninfo!=null && coninfo.isConnected()){
isAvailable = true;
return isAvailable;
}

return false;
}

private void alertUserAboutProblem() {

AlertDialogFragment dialog =new AlertDialogFragment();
dialog.show(getFragmentManager(),TAG);
}

}

最佳答案

尝试将response.body().string()保存到字符串变量,然后将其传递给getCurrentDetails。

根据 documentation :“响应正文是一次性值,只能消耗一次”。

关于java - JSONException 、 JSONObject 异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29786310/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com