Monday, May 3, 2010

[android-developers] Re: Retrieving the Json data into a list view...

Your problems are right up on top of your log. It is not correctly
parsing your JSONObject. As I said earlier, you need to figure out
your parsing.

I took a different approach to writing my own adapter, although I
think I did that too, in some iteration or other. I create a generic
JSONArrayList which I can plug right into an ArrayAdapter. Also, it
looks like you might be doing a lot of copying. My JSONArrayList uses
an embedded JSONArray and does not do any copying. Maybe you'll spot a
tip or something. So, for what it's worth:

---- JSONArrayList.java ----

package com.eyebrowsoftware.gobeering;

import java.util.AbstractSequentialList;
import java.util.Collection;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;

import org.json.JSONArray;
import org.json.JSONException;

public class JSONArrayList<T> extends AbstractSequentialList<T> {

JSONArray mArray;

public JSONArrayList() {
super();
mArray = new JSONArray();
}

public JSONArrayList(String jss) {
super();
try {
mArray = new JSONArray(jss);
} catch (JSONException e) {
e.printStackTrace();
mArray = new JSONArray();
}
}

public JSONArrayList(JSONArray a) {
super();
mArray = (a == null) ? new JSONArray() : a;
}


public JSONArray getArray() {
return mArray;
}

public String toString() {
return mArray.toString();
}

/**
* Return a separator-separated string containing all the members in
the array
* @param separator - the separator to appear btw the parts of the
output string
* @return a new string with the array components separated by
separator
*/
public String join(String separator) {
try {
return mArray.join(separator);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

@Override
public void add(int location, T object) {
int len = mArray.length();
if(location < 0 || location > len)
throw new IndexOutOfBoundsException();
try {
for(int i = len; i > location; --i) {
mArray.put(i, mArray.getString(i-1));
}
mArray.put(location, object);
} catch (JSONException e) {
e.printStackTrace();
}
}

@Override
public boolean addAll(int arg0, Collection<? extends T> arg1) {
if(arg0 < 0)
throw new IndexOutOfBoundsException();
for(T o : arg1) {
try {
mArray.put(arg0++, o);
} catch (JSONException e) {
e.printStackTrace();
throw new IllegalArgumentException();
}
}
return true;
}

@SuppressWarnings("unchecked")
@Override
public T get(int location) {
if(location >= mArray.length())
throw new IndexOutOfBoundsException();
return (T) mArray.opt(location);
}

private class MyIterator<E> implements Iterator<E> {

int pos = -1;

@Override
public boolean hasNext() {
return pos + 1 < mArray.length();
}

@SuppressWarnings("unchecked")
@Override
public E next() {
if(pos + 1 >= mArray.length())
throw new NoSuchElementException();
return (E) mArray.opt(++pos);
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}

}
@Override
public Iterator<T> iterator() {
return new MyIterator<T>();
}

@Override
public int lastIndexOf(Object object) {
for(int i = mArray.length()-1; i >= 0; --i) {
if(mArray.optString(i).equals(object))
return i;
}
return -1;
}

@Override
public ListIterator<T> listIterator(int location) {
return new MyListIterator<T>(location);
}

@Override
public T remove(int location) {
throw new UnsupportedOperationException();
}

@Override
public boolean remove(Object object) {
throw new UnsupportedOperationException();
}

@SuppressWarnings("unchecked")
public T set(int location, T object) {
T r = (T) mArray.opt(location);
mArray.put(object);
return r;
}

class MyListIterator<E> implements ListIterator<E> {
int mPos = -1;

public MyListIterator() {
super();
}

public MyListIterator(int location) {
super();
if(location < 0)
location = 0;
mPos = location - 1;
}

@Override
public void add(E arg0) {
try {
for(int i = mArray.length(); i > mPos + 1; --i) {
mArray.put(i, mArray.getString(i-1));
}
mArray.put(++mPos, arg0);
} catch (JSONException e) {
e.printStackTrace();
}
}

@Override
public boolean hasNext() {
return mPos + 1 < mArray.length();
}

@Override
public boolean hasPrevious() {
return mPos >= 0;
}

@SuppressWarnings("unchecked")
@Override
public E next() {
if(mPos + 1 >= mArray.length())
throw new NoSuchElementException();
return (E) mArray.opt(++mPos);
}

@Override
public int nextIndex() {
return mPos + 1;
}

@SuppressWarnings("unchecked")
@Override
public E previous() {
if(mPos < 0)
throw new NoSuchElementException();
return (E) mArray.opt(mPos--);
}

@Override
public int previousIndex() {
return mPos;
}

@Override
public void remove() {
throw new UnsupportedOperationException();

}

@Override
public void set(E arg0) {
if(mPos == -1)
throw new IllegalStateException();
try {
mArray.put(mPos, arg0);
} catch (JSONException e) {
e.printStackTrace();
throw new IllegalArgumentException();
}
}
}
@Override
public int size() {
return mArray.length();
}
}


--------- End of JSONArrayList.java ---------

On May 3, 7:20 am, Rahul <rahulvarma.kalidindi0...@gmail.com> wrote:
> Here's the log errors i m getting...
>
> 04-30 13:09:46.656: INFO/ActivityManager(584): Starting activity:
> Intent { act=android.intent.action.MAIN
> cat=[android.intent.category.LAUNCHER] flg=0x10000000
> cmp=com.WorldToyota/.Alerts }
> 04-30 13:09:50.417: ERROR/JSON(924): There was an error parsing the
> JSON
> 04-30 13:09:50.417: ERROR/JSON(924): org.json.JSONException:
> JSONArray[0] is not a JSONObject.
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> org.json.JSONArray.getJSONObject(JSONArray.java:268)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> com.WorldToyota.AlertsAdd.retrieveJSONArray(AlertsAdd.java:30)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> com.WorldToyota.Alerts.onCreate(Alerts.java:20)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
> 1123)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
> 2364)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
> 2417)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> android.app.ActivityThread.access$2100(ActivityThread.java:116)
> 04-30 13:09:50.417: ERROR/JSON(924):     at android.app.ActivityThread
> $H.handleMessage(ActivityThread.java:1794)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> android.os.Handler.dispatchMessage(Handler.java:99)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> android.os.Looper.loop(Looper.java:123)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> android.app.ActivityThread.main(ActivityThread.java:4203)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> java.lang.reflect.Method.invokeNative(Native Method)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> java.lang.reflect.Method.invoke(Method.java:521)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> com.android.internal.os.ZygoteInit
> $MethodAndArgsCaller.run(ZygoteInit.java:791)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
> 04-30 13:09:50.417: ERROR/JSON(924):     at
> dalvik.system.NativeStart.main(Native Method)
> 04-30 13:09:50.688: ERROR/JSON(924): There was an error creating the
> JSONObject
> 04-30 13:09:50.688: ERROR/JSON(924): org.json.JSONException:
> JSONObject["suggestions"] not found.
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> org.json.JSONObject.get(JSONObject.java:287)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> org.json.JSONObject.getJSONArray(JSONObject.java:362)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> com.WorldToyota.AlertsAdd.retrieveJSONArray(AlertsAdd.java:41)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> com.WorldToyota.Alerts.onCreate(Alerts.java:20)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
> 1123)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
> 2364)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
> 2417)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> android.app.ActivityThread.access$2100(ActivityThread.java:116)
> 04-30 13:09:50.688: ERROR/JSON(924):     at android.app.ActivityThread
> $H.handleMessage(ActivityThread.java:1794)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> android.os.Handler.dispatchMessage(Handler.java:99)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> android.os.Looper.loop(Looper.java:123)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> android.app.ActivityThread.main(ActivityThread.java:4203)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> java.lang.reflect.Method.invokeNative(Native Method)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> java.lang.reflect.Method.invoke(Method.java:521)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> com.android.internal.os.ZygoteInit
> $MethodAndArgsCaller.run(ZygoteInit.java:791)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
> 04-30 13:09:50.688: ERROR/JSON(924):     at
> dalvik.system.NativeStart.main(Native Method)
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group athttp://groups.google.com/group/android-developers?hl=en

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment