-1

I'm trying to parse the JSON Array from http://inspirontrance.com/app.php but its not working. I believe there could be issues on my code or the url is simply redirecting to another url that defaultHttpClient doesn't supports.

See my code,

Android Parsing Activity Class

public class AndroidJSONParsingActivity extends ListActivity {

    private static String url = "http://www.inspirontrance.com/app";

    private static final String TAG_UPLOADS = "uploads";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_DATE = "date";
    private static final String TAG_UPLOAD_NO = "0";

    JSONArray uploads = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayList<HashMap<String, String>> uploads_list = new ArrayList<HashMap<String, String>>();

        JSONParser jParser = new JSONParser();

        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            uploads = json.getJSONArray(TAG_UPLOADS);

            for (int i = 0; i < uploads.length(); i++) {
                JSONObject c = uploads.getJSONObject(i);

                JSONObject upload_no = c.getJSONObject(TAG_UPLOAD_NO);
                String name = upload_no.getString(TAG_NAME);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_NAME, name);

                uploads_list.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(this, uploads_list,
                R.layout.list_item, new String[] { TAG_NAME },
                new int[] { R.id.name });

        setListAdapter(adapter);

    }

}

JSON Parser Class

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}
  • first your getJSONFromUrl should perform in a separate thread,but what's happen excactly?do you get any error? I think your code is well done – Xenione Sep 14 '13 at 07:44
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – Selvin Sep 14 '13 at 07:52
  • Can you post Logcat here? – Vishal Pawale Sep 14 '13 at 08:41

1 Answers1

0

The answer to your problem is stated in your stack trace :

Your content must have a ListView whose id attribute is 'android.R.id.list'

The corresponding activity seems to be your AndroidJSONParsingActivity which extends ListActivity. A ListActivity performs many things automatically and assume that your layout (in your case R.layout.main) contains a ListView with the android.R.id.list.

Check your main layout and make sure that you have a ListView like this :

<ListView 
    android:id="@android:id/list" 
    [...]
    />
XGouchet
  • 10,002
  • 10
  • 48
  • 83