Hussam Ali Alzoubi
Hussam Ali Alzoubi

Reputation: 87

JSONObject catch exception

Dears Im new in Android , I have a problem in Json here is my code... i debug it , everything is good but it jumped to the catch block when reaching this statment

jArray = new JSONObject(result);

so its return null...

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url){
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(url);
                HttpResponse response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

      //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }

        try{

            jArray = new JSONObject(result);            
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }

        return jArray;
    }
}

Upvotes: 0

Views: 2499

Answers (5)

Rahul Baradia
Rahul Baradia

Reputation: 11951

parse as shown below

JSONObject mainJSON = new JSONObject();

    JSONArray jsonMainArr = mainJSON.getJSONArray("result");
    for (int i = 0; i < jsonMainArr.toArray().length; i++) {
        JSONObject childJSONObject = jsonMainArr.getJSONObject(i);
        String CompletionStatus= childJSONObject.getString("CompletionStatus");
        String ContactMobile= childJSONObject.getString("ContactMobile");
    }

hope this ll help you out.

Upvotes: 0

Pankaj Singh
Pankaj Singh

Reputation: 2311

replace this because JSONObject is not converted to JSONArray

 jArray = new JSONArray(result);

this may help you.

Upvotes: 2

Deepanker Chaudhary
Deepanker Chaudhary

Reputation: 1704

If Your Sring starts with these brackets

 [ ]

then Use JSONArray or

if String Starts with these brackets {} Then JSONObject Should Use.

I thinks this the region otherwise response is not comming then Try in place of

 HttpGet httpget = new HttpGet(url);

Replace

HttpPost post=new HttpPost(url);

Upvotes: 0

laplasz
laplasz

Reputation: 3497

log out your result, before parse it to check whether is a valid json string or not, perhaps not

Upvotes: 0

Blundell
Blundell

Reputation: 76506

I'm going to guess you need an Array not an Object for your JSON.

try:

jArray = new JSONArray(result); 

not

jArray = new JSONObject(result); 

Log your error like this:

 Log.e("log_tag", "Error parsing data ", e);

and you will get a more detailed description of what and where your problem is

Json Array API

Upvotes: 0

Related Questions