JSONException,JSONObject异常错误

时间:2015-04-22 01:48:13

标签: java android json jsonobject jsonexception

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

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

    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);
}

}

1 个答案:

答案 0 :(得分:1)

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

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

相关问题