0

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;
    }
}
4
  • Show what in result String Commented Dec 28, 2012 at 11:48
  • plz post our json string you are getting from server Commented Dec 28, 2012 at 11:49
  • print the string result, and paste here. Commented Dec 28, 2012 at 11:49
  • this is the result [{"CompletionStatus":2,"ContactMobile":"962799407083","ContactPerson":"William Erwin","Description":" AS AS AS AS ","Details":"Testy CCCC","ScheduledDate":"\/Date(1356901200000+0300)\/","WorkOrderID":206}] Commented Dec 28, 2012 at 11:56

5 Answers 5

2

replace this because JSONObject is not converted to JSONArray

 jArray = new JSONArray(result);

this may help you.

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

Comments

0

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.

Comments

0

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

Comments

0

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

Comments

0

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);

2 Comments

my service is HttpGet, not post, I'll try JsonArray, i have some problems in understanding how to parse Json could you help me in finding a tips for that... I'll be thankful :)
Sometimes HttpGet not work But in Your case its working.. For Checking that JsonFormate is right just use Jsonlint.com & it will parse from jArray = new JSONArray(result);

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.