Reputation: 6114
I am trying to pull a JSON array and load it into a RecyclerView. here is the code :
JsonArrayRequest TweetReq = new JsonArrayRequest(URL1,
new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response) {
Log.d("WORKING RESPONSE:::::", response.toString());
//hidePDialog();
SharedPreferences Tempx = getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
SharedPreferences.Editor edx = Tempx.edit();
edx.putString("GSON_FEED", response.toString());
edx.apply();
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray jArray = parser.parse(Tempx.getString("GSON_FEED","")).getAsJsonArray();
//ArrayList<MainPojo> uid = new ArrayList<MainPojo>();
for(JsonElement obj : jArray )
{
MainPojo cse = gson.fromJson( obj , MainPojo.class);
TweetList.add(cse);
}
mAdapter.notifyDataSetChanged();
// Parsing json
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
SharedPreferences Tempx = getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
try{Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray jArray = parser.parse(Tempx.getString("GSON_FEED","")).getAsJsonArray();
for(JsonElement obj : jArray )
{
MainPojo cse = gson.fromJson( obj , MainPojo.class);
TweetList.add(cse);
}
mAdapter.notifyDataSetChanged();
VolleyLog.d("EDUKNOW::::", "Error: " + error.getMessage());
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
As you can see, I am storing the result into a SharedPreferences and populating it!
However, when I execute it, all the time onErrorResponse()
is getting executed,
here is the logcat:
01-08 20:01:08.688 4764-4764/com.fossasia.loklak W/System.err:java.lang.IllegalStateException: This is not a JSON Array.
01-08 20:01:08.688 4764-4764/com.fossasia.loklak W/System.err: atcom.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106)
01-08 20:01:08.688 4764-4764/com.fossasia.loklak W/System.err: atcom.fossasia.loklak.Activities.MainActivity$2.onErrorResponse(MainActivity.java:105)
01-08 20:01:08.688 4764-4764/com.fossasia.loklak W/System.err: atcom.android.volley.Request.deliverError(Request.java:564)
01-08 20:01:08.688 4764-4764/com.fossasia.loklak W/System.err: atcom.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
01-08 20:01:08.688 4764-4764/com.fossasia.loklak W/System.err: atandroid.os.Handler.handleCallback(Handler.java:739)
01-08 20:01:08.688 4764-4764/com.fossasia.loklak W/System.err: atandroid.os.Handler.dispatchMessage(Handler.java:95)
01-08 20:01:08.689 4764-4764/com.fossasia.loklak W/System.err: atandroid.os.Looper.loop(Looper.java:148)
01-08 20:01:08.689 4764-4764/com.fossasia.loklak W/System.err: atandroid.app.ActivityThread.main(ActivityThread.java:5417)
01-08 20:01:08.689 4764-4764/com.fossasia.loklak W/System.err: atjava.lang.reflect.Method.invoke(Native Method)
01-08 20:01:08.689 4764-4764/com.fossasia.loklak W/System.err: atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-08 20:01:08.689 4764-4764/com.fossasia.loklak W/System.err: atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
he first error, This is not a JSON Array , I guess is because its trying to load from sharedPreferences which is empty initially.
And my question is : Is this due to any problem in the API or the way I've defined my POJO?
Upvotes: 0
Views: 618
Reputation: 1632
I think it the problem lies in your POJO implementation. I checked your query from http://loklak.org/api/search.json?q=apple
The response is not a JSON Array
, it's a JSON Object
. See the beginning of the response? it starts with a curly bracket {
, which marks the beginning of an object. I suggest you read a bit on the JSON documentation .
In order to get your JSON Array
, you need to check the structure of your JSON. I copy-pasted the JSON response to an Online JSON Formatter and I can see there are 7 objects there, with one of them containing the JSON Array
that you need. If you manage to get that JSON Object
as a JSON Array
, you'll be able to use it.
JSONArrayRequest
to a JSONObjectRequest
JSONArray
inside the JSONObject
(in this case, the key is "statuses"
, and the value is a JSONArray
)Good Luck
Upvotes: 1