0

in loop for I get JSONArrays using this :

try{
    JSONArray json = jParserArray.makeHttpRequest(url, "GET", param);
  } catch (Exception e) {
    Log.i("Message :", "the result is JSONObject"+e);
  }

but in some case the result is a JSONOBject, how can I verify what type of json I am getting?

NB: I am not getting the Log if the result is not JSONArray!!

this is my makeHttpRequest :

static InputStream is = null;
static JSONArray jObj = null;
static String json = "";

// constructor
public JSONArrayParser() {

}

// function get json from url
// by making HTTP POST or GET mehtod
public JSONArray makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request
    try {

        // check for request method
        if(method == "POST"){
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }else if(method == "GET"){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }           

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

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONArray(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

thank you.

7
  • Make a try catch and on your catch use JSONObject instead Commented Sep 23, 2014 at 16:30
  • @PedroOliveira I try it, but I never get the error message as I mentioned above. Commented Sep 23, 2014 at 16:39
  • That means that the object is always an array. Why do you say that there are also objects? Commented Sep 23, 2014 at 16:40
  • because I get this error on logcat : Error parsing data org.json.JSONException: Value {"message error":"The requested record does not exist","error":107} of type org.json.JSONObject cannot be converted to JSONArray . this error generated by the object jParserArray Commented Sep 23, 2014 at 16:45
  • Share your makeHttpRequest method Commented Sep 23, 2014 at 16:46

1 Answer 1

1

You may change your makeHttpRequest method's return type and handle it by checking its respose's instance:

public Object makeHttpRequest(String url, String method, List<NameValuePair> params) {


    //Your code at your question here (to your below comment) 

    // try parse the string to a JSON object
    Object jObj = null;
    try {
        jObj = new JSONArray(json);
    } catch (JSONException e) {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return jObj;
}

And handle object:

Object json = jParserArray.makeHttpRequest(url, "GET", param);
if(json instanceof JSONArray) {
    JSONArray jsonArr = (JSONArray) json ;
    // TODO: Handle json array
} else if (json  instanceof JSONObject) {
    JSONObject jsonObj = (JSONObject) json ;
    // TODO: Handle json object
}
Sign up to request clarification or add additional context in comments.

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.