Ankit Nagevadiya Ankit Nagevadiya - 5 months ago 29
JSON Question

Value Bundle of type java.lang.String cannot be converted to JSONArray

Below is my response from server

[
{
"updates":[
{
"id":6,
"name":"Food Express",
"photo":"http:\/\/www.abc.org\/development\/data\/restaurant\/1451480096.jpg",
"updates":"testing updatessss"
}
],
"count":0
},
{
"updates":[
{
"id":96,
"name":"Madras cafe",
"photo":"http:\/\/www.abc.org\/development\/img\/res-bg.jpg",
"updates":"asdfasdfsafasdfsa"
},
{
"id":96,
"name":"Madras cafe",
"photo":"http:\/\/www.abc.org\/development\/img\/res-bg.jpg",
"updates":"vcxvzxvzx"
}
],
"count":4
}
]


Here is JSON response I Parse in the android but causes the error..

Here is my android code

JSONArray jsonarray = new JSONArray(response);
Log.e("Hello","Hi"+jsonarray);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonObjectmain = jsonarray .getJSONObject(i);
JSONArray array = jsonObjectmain.getJSONArray("updates");
for (int j = 0; j < array.length(); j++) {
JSONObject jsonObject = array.getJSONObject(j);
String id = jsonObject.getString("id").toString();
String name = jsonObject.getString("name").toString();
String photo = jsonObject.getString("photo").toString();
String updates = jsonObject.getString("updates").toString();

}
}


I want to put this thing to the pushnotification over onMessage() method but i am not able to do so.

Here is my stacktrace...

07-02 13:08:53.553: W/System.err(19037): org.json.JSONException: Value Bundle of type java.lang.String cannot be converted to JSONArray
07-02 13:08:53.553: W/System.err(19037): at org.json.JSON.typeMismatch(JSON.java:111)
07-02 13:08:53.553: W/System.err(19037): at org.json.JSONArray.<init>(JSONArray.java:96)
07-02 13:08:53.553: W/System.err(19037): at org.json.JSONArray.<init>(JSONArray.java:108)
07-02 13:08:53.553: W/System.err(19037): at com.abc.ff.GCMIntentService.parseList(GCMIntentService.java:117)
07-02 13:08:53.553: W/System.err(19037): at com.abc.ff.GCMIntentService.onMessage(GCMIntentService.java:63)
07-02 13:08:53.553: W/System.err(19037): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
07-02 13:08:53.553: W/System.err(19037): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
07-02 13:08:53.553: W/System.err(19037): at android.os.Handler.dispatchMessage(Handler.java:102)
07-02 13:08:53.553: W/System.err(19037): at android.os.Looper.loop(Looper.java:149)
07-02 13:08:53.553: W/System.err(19037): at android.os.HandlerThread.run(HandlerThread.java:61)


Here is my
onMessage()
method...

@Override
protected void onMessage(Context context, Intent data) {

String message;

message = data.getExtras().getString("updates");

Log.e("",""+data.getExtras().toString());

Intent intent = new Intent(this, MyActivity.class);
parseList(data.getExtras().toString());

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentTitle("Title")
.setContentText(message).setContentIntent(pIntent)
.getNotification();

notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(R.string.app_name, notification);

{

PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
final PowerManager.WakeLock mWakelock = pm.newWakeLock(
PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
mWakelock.acquire();
notification.defaults|=Notification.DEFAULT_VIBRATE;


Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
mWakelock.release();
}
};
timer.schedule(task, 5000);
}

}

Answer

I think you're receiving a Bundle in your GCMIntentService which has a JsonObject and not a JsonArray. You should convert a Bundle to JsonObject like this :

JSONObject json = new JSONObject();
Set<String> keys = bundle.keySet();
for (String key : keys) {
   try {
       json.put(key, bundle.get(key)); 
   } catch (JSONException e) {
       e.printStackTrace();
   }
}

Then you can do your normal processing like this :

JSONArray jsonarray = new JSONArray(json.getString("response"));
Log.e("Hello","Hi"+jsonarray);
for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject jsonObjectmain = jsonarray .getJSONObject(i);
    JSONArray array = jsonObjectmain.getJSONArray("updates");
    for (int j = 0; j < array.length(); j++) {
         JSONObject jsonObject = array.getJSONObject(j);
         String id = jsonObject.getString("id").toString();
         String name = jsonObject.getString("name").toString();
         String photo = jsonObject.getString("photo").toString();
         String updates = jsonObject.getString("updates").toString();

    }      
}

UPDATE

change the argument type of your parseList method from String to Bundle, so your parseList method should be like this:

parseList(Bundle bundle)

and replace parseList(data.getExtras().toString()); with parseList(data.getExtras());

Now in your parseList(Bundle bundle) method, you can convert the bundle to json as described above.

Comments