This是我的API,here is the POJO to the JSON 我想知道如何使用Volley存储JSON对象。
到目前为止,这是我的代码:
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();
}
//hidePDialog();
}
});
这会返回IllegalStateException
说This is not a JSON Array
,
这是有道理的,但我如何将其存储为Object
?
EDIT1 添加了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: at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106)
01-08 20:01:08.688 4764-4764/com.fossasia.loklak W/System.err: at com.fossasia.loklak.Activities.MainActivity$2.onErrorResponse(MainActivity.java:105)
01-08 20:01:08.688 4764-4764/com.fossasia.loklak W/System.err: at com.android.volley.Request.deliverError(Request.java:564)
01-08 20:01:08.688 4764-4764/com.fossasia.loklak W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
01-08 20:01:08.688 4764-4764/com.fossasia.loklak W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
01-08 20:01:08.688 4764-4764/com.fossasia.loklak W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01-08 20:01:08.689 4764-4764/com.fossasia.loklak W/System.err: at android.os.Looper.loop(Looper.java:148)
01-08 20:01:08.689 4764-4764/com.fossasia.loklak W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
01-08 20:01:08.689 4764-4764/com.fossasia.loklak W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-08 20:01:08.689 4764-4764/com.fossasia.loklak W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-08 20:01:08.689 4764-4764/com.fossasia.loklak W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:-1)
try{
JsonArray jArray = parser.parse(Tempx.getString("GSON_FEED","")).getAsJsonArray();
for(JsonElement obj : jArray )
{
MainPojo cse = gson.fromJson( obj , MainPojo.class);
TweetList.add(cse);
}
}catch(Throwable e) {
JsonElement obj = parser.parse(Tempx.getString("GSON_FEED","")).getAsJsonObject();
MainPojo cse = gson.fromJson( obj , MainPojo.class);
TweetList.add(cse);
}