Brayden Brekke
Brayden Brekke

Reputation: 3

Android SharedPreference JSON not being accessed

I am using volley to send a http request to yahoo's weather API like this:

        public Object requestData(String url) {
        final RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        JsonObjectRequest objectRequest = new JsonObjectRequest(
                Request.Method.GET,
                url,
                null,
                new Response.Listener < JSONObject > () {
                    @Override
                    public void onResponse(JSONObject response) {
                     String responseList = response.toString();
                   cityList.edit().putString(String.valueOf(i), responseList);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );
        requestQueue.add(objectRequest);
        return null;
    }

As you can see, I am putting the response to this inside a sharedpreference I declared as "citylist" :

      cityList = getSharedPreferences(String.valueOf(i), MODE_PRIVATE);

I then access this response inside the sharedpreference like this:

       String mResponse = cityList.getString(String.valueOf(i), "");
                Log.d("log", mResponse);

                try {
                    JSONObject jsonObj = new JSONObject(mResponse);
                    JSONObject response = jsonObj.getJSONObject("query");
                    createdCity.temperature = response.optJSONObject("results").optJSONObject("channel").optJSONObject("item").optJSONObject("condition").getInt("temp");
                    createdCity.conditions = response.optJSONObject("results").optJSONObject("channel").optJSONObject("item").optJSONObject("condition").getString("text");
                    createdCity.town = response.optJSONObject("results").optJSONObject("channel").optJSONObject("location").getString("city");
                    createdCity.country = response.optJSONObject("results").optJSONObject("channel").optJSONObject("location").getString("country");
                    createdCity.state = response.optJSONObject("results").optJSONObject("channel").optJSONObject("location").getString("region");
                   Log.d("log", String.valueOf(createdCity.town));
                    cities.add(createdCity);
                    addMenuItemInNavMenuDrawer();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

The problem is that "log" isn't outputting anything. The only information I'm getting from the logcat is:

org.json.JSONException: End of input at character 0 of 

How can I go about fixing my issue? Is there a better way of doing this?

Upvotes: 0

Views: 42

Answers (1)

shiredude95
shiredude95

Reputation: 560

Are you committing after editing the preference? Based on your response, you might as well use apply() as it does things asynchronously.

If you are sure that the commit won't affect the main thread, you can use cityList.commit() or safely use cityList.apply(). But you need to do either of these two or else the changes are not written to the disk which is the reason you are not able to retrieve the data.

This link should help: https://developer.android.com/training/data-storage/shared-preferences#java

Upvotes: 1

Related Questions