0

描述:在 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);
}

}

4

1 回答 1

1

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

根据文档:“响应主体是一次性值,只能使用一次”。

于 2015-04-22T02:48:34.897 回答