0

I having an error while parsing json array in my android application.

My json object is in the following form:

{"post":[
      [{"0":"all the best to every one...!!","post":"all the best to every one...!!"},
     {"0":"hello every one...","post":"hello every one..."}]
]}

My java file in android is as follows:

public class Newsactivity extends ListActivity {
private static final String TAG_SUCCESS = "";
private ProgressDialog pDialog;

JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> postsList;

private static String url_all_products = "myurl";
private static final String TAG_POST = "post";
JSONArray posts = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.allposts);        
    postsList = new ArrayList<HashMap<String, String>>();
    new LoadAllProducts().execute();
    ListView lv = getListView();
}
class LoadAllProducts extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Newsactivity.this);
        pDialog.setMessage("Loading posts. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
        Log.d("All Posts: ", json.toString());
        try {
            posts = json.getJSONArray(TAG_POST);
            for (int i = 0; i < posts.length(); i++) {
                JSONObject c = posts.getJSONObject(i);
                String post = c.getString(TAG_POST);

                HashMap<String,String> map = new HashMap<String,String>();
                map.put(TAG_POST, post);

                postsList.add(map);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {

                ListAdapter adapter = new SimpleAdapter(
                        Newsactivity.this, postsList,
                        R.layout.singlepost, new String[] { TAG_POST},
                        new int[] { R.id.pid});

                setListAdapter(adapter);
            }
        });
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_newsactivity, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

I am getting a fatal exception starting this activity. This code is modified code of an online tutorial on json parsing. As I am new to android I am unable to find where the id is. So, please help me. Thank you.

13
  • makeHttpRequest is most likely failing because you aren't passing a valid url Commented Mar 10, 2015 at 1:06
  • I removed my url ..while posting here.@samgak Commented Mar 10, 2015 at 1:06
  • Post your logcat here Commented Mar 10, 2015 at 1:25
  • I am not understanding how to indent it here.. can you help me? @mr.icetea Commented Mar 10, 2015 at 1:31
  • Your 'doInBackground(String...args)' method contains arguments but your call for '.execute()' does not have any arguments. Commented Mar 10, 2015 at 1:39

3 Answers 3

1

I am afraid that your json format is "a little bit strange" (or i may say it is in a different format with what your code is trying to do) according to your code. As you can see, there are two "[" after the first "post", which means the array of elements is actually inside another array.

{"post":[ // first '['
  [ // second '['
    {"0":"all the best to every one...!!","post":"all the best to every one...!!"},
    {"0":"hello every one...","post":"hello every one..."}
  ]
]}

so to make it correct, do following

try {
        posts = json.getJSONArray(TAG_POST);
        posts = posts.getJSONArray(0) // to get the first array
        for (int i = 0; i < posts.length(); i++) {
            JSONObject c = posts.getJSONObject(i);
            String post = c.getString(TAG_POST);

            HashMap<String,String> map = new HashMap<String,String>();
            map.put(TAG_POST, post);

            postsList.add(map);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

yes , what I mean "wrong" is the format is not what his code is doing and I don't know the json format or the code means "correct" to him
0

as your json responce having nested array you need to code like this...

JSONObject json; = jParser.makeHttpRequest(url_all_products, "GET", params);

    Log.d("All Posts: ", json.toString());

    try {

        posts = json.getJSONArray(TAG_POST);

        for (int i = 0; i < posts.length(); i++) {

            subposts = posts.getJSONArray(i);

            for (int j = 0; j < subposts.length(); j++) {

                JSONObject c = subposts.getJSONObject(i);
                String post = c.getString(TAG_POST);

                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_POST, post);

                postsList.add(map);

            }
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

Comments

0

Try this:

posts = json.getJSONArray(TAG_POST);
for (int i = 0; i < posts.getJSONArray(0).length(); i++) {
    JSONObject c = posts.getJSONArray(0).getJSONObject(i);
    String post = c.getString("0");

    HashMap<String,String> map = new HashMap<String,String>();
    map.put(TAG_POST, post);

    postsList.add(map);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.