1

I am facing here problem in reading a json array which is got from server : http://XXX.XXX.XXX.XXX:9090/appsapi/rest/services/getStudentPersonalDetails/123789 , this json array doesn't have json object in it. It is like below.:

{"password":"raj","address":"dfsaf","state":"tamilnadu","image":"Bindu Appalam.jpg",
 "dept":"BE(Mech)","fname":"fasdf","mname":"saffd","dob":"14/12/1951","sex":"Male",
 "email":"[email protected]","contact":"212121212545855555545","city":"dsfadf",
 "pin":"600106","sname":"fdjsf","studid":"123789"}

I want to parse it..

I have tried to get the json array like below from httpclient request :

try {

            System.out.println("url tt : "+url);
            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            System.out.println(" Urls Input to "+url);

            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }

                httpResponse = httpClient.execute(httpPost);

            } else if (method == GET) {
                // appending params to url
                System.out.println(" Params Valyes "+params);

                if (params != null) {
                    String paramString = URLEncodedUtils.format(params, "utf-8");
                    url += "?" + paramString;
                    System.out.println(" Params String "+paramString);
                }
                HttpGet httpGet = new HttpGet(url);
                httpResponse = httpClient.execute(httpGet);
            }

            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);

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

but got below error in response :

05-20 16:38:25.420: I/System.out(29983):  
Output <!DOCTYPE html><html><head><title>Apache Tomcat/8.0.5 - Error report
</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} 
H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} 
H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} 
BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} 
B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} 
P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name
 {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 404 - Not Found
</h1><div class="line"></div><p><b>type</b> Status report</p><p><b>message</b> <u>Not Found</u></p><p><b>description</b>
 <u>The requested resource is not available.</u></p><hr class="line"><h3>Apache Tomcat/8.0.5</h3></body></html>

any body , pls help here...

5
  • 2
    Looks like you are getting 404 not found on your service call? It cant parse it because it cant be found Commented May 20, 2014 at 11:45
  • I think so your json is wrong Commented May 20, 2014 at 11:46
  • could you give us the correct url? Commented May 20, 2014 at 11:46
  • raj is in the double quote Commented May 20, 2014 at 11:48
  • @Vijay, sorry it is in double quetes there , i mistakenly did that, will edit it.. Commented May 20, 2014 at 11:50

3 Answers 3

1

A JSON Object starts with a { and ends with a } while a JSON Array starts with a [ and ends with a ].

In your case, change your code to have a JSONObject instead.

JSONObject json = new JSONObject(jsonString);
JSONArray jArray = json.getJSONArray("list");

System.out.println("*****JARRAY*****"+jArray.length());
for(int i=0;i<jArray.length();i++){


 JSONObject json_data = jArray.getJSONObject(i);
 Log.i("log_tag","_id"+json_data.getInt("account")+
  ", mall_name"+json_data.getString("name")+
  ", location"+json_data.getString("number")+
  ", telephone"+json_data.getString("url")+
  ",----"+json_data.getString("balance")+
  ",----"+json_data.getString("credit")+
  ",----"+json_data.getString("displayName")
 );

}

refer to:https://stackoverflow.com/a/4244963/2771869

Sign up to request clarification or add additional context in comments.

Comments

1

Kindly refer online for how to get JSON Response.

http://mrbool.com/how-to-use-json-to-parse-data-into-android-application/28944

Comments

1

In your code, there is no json array present, it is json object with name value pair data. So you have to parse JSONObject not JSONArray.

Like:

JSONObject json = new JSONObject(jsonString);

String password = json.getString("password");

Similarly you can get all the values of the object.

1 Comment

hi, thnks for reply my problem is here in getting response from server using httpclient.. i am getting here Http not found error..

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.