Saad
Saad

Reputation: 21

Why I can't getting json in android?

I am having problem in getting one dimensional JSON please guide me where either the problem is in my JSON or in my code?

JSON:

{
    "data": {
        "id": "S000010",
        "name": "ZS Solutions",
        "email": "[email protected]",
        "phone": "051-1234567",
        "address": "p.o.box 123",
        "about": "im the company\r\nHAhahhaa"
    }
}

Android activity JSON retrieval code:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// TODO Auto-generated method stub
try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("abc.php?Id="+id+"");

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse hresponse = httpclient.execute(httppost);
    HttpEntity entity = hresponse.getEntity();
    is = entity.getContent();
    String result=co(is);
    JSONObject json=new JSONObject(result);

    JSONArray a=  json.getJSONArray(data);
    for (int i = 0; i <= a.length(); i++) {

        json = a.getJSONObject(i);
        String  cname=json.getString("name");
        String  cemail=json.getString("email");
        String  cphone=json.getString("phone");
        String  caddress=json.getString("address");
        String  cabout=json.getString("about");
        Log.w("DATA  ","NAME  "+cname+"E-mail  "+cemail+"Phone "+cphone+"ADDRESS"+caddress+"ABOUT"+cabout);
    }


}
catch(Exception e){}

Upvotes: 0

Views: 117

Answers (1)

Pragnani
Pragnani

Reputation: 20155

JSONArray a= json.getJSONArray(data); <-- this causing the Exception as

data is not a json Array, instead , it is a JSONObject

Your code should be

       JSONObject json=new JSONObject(result);

       JSONObject jsonobj=json.getJSONObject("data");

        String  cname=jsonobj.getString("name");
        String  cemail=jsonobj.getString("email");
        String  cphone=jsonobj.getString("phone");
        String  caddress=jsonobj.getString("address");
        String  cabout=jsonobj.getString("about");

Upvotes: 5

Related Questions