Home > Net >  org.json.JSONException cannot be converted to JSONArray in android
org.json.JSONException cannot be converted to JSONArray in android

Time:07-02

When I try to connect with webserver with IP address, I got this JSON error.

org.json.JSONException: Value {"dsdata":{"ttlogin":[{"BundyId":"9090","Name":"IT DEVELOPMENT"}]}} of type org.json.JSONObject cannot be converted to JSONArray
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:112)

My java code

    @Override
    protected String doInBackground(String... params) {
        
        String login_url ="http://200.200.200.20/bin/dev/dispatch/truckunloading.p";

        try {

            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);

            OutputStream OS = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));

            String data = URLEncoder.encode("ctype", "UTF-8")   "="   URLEncoder.encode("login", "UTF-8")   "&"
                      URLEncoder.encode("clogin", "UTF-8")   "="   URLEncoder.encode("9090", "UTF-8");

            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            OS.close();

            Log.e("ERROR OBJECT", "Conneting");

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(inputStream, "iso-8859-1"));

            String response = "";
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                response  = line;
            }

            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            try {
                JSONArray jsonarray = new JSONArray(response);
                for (int i = 0; i < jsonarray.length(); i  ) {
                    JSONObject jsonobject = jsonarray.getJSONObject(i);
                    Log.e("ERROR OBJECT", jsonobject.toString());
                }

            } catch (JSONException e) {
                e.printStackTrace();
                return response;
            }

            return "good";
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "fail";

        } catch (IOException ee) {
            ee.printStackTrace();
            return "fail";
        }
    }

I searched it about everywhere but I can't resolve it.

It gives me the desirable response in the browser but I don't know what's the matter with android !

Please help me.

CodePudding user response:

A JSONArray string is enclosed in [], while a JSONObject string is enclosed in {}. The string you are trying to parse is not a JSONArray - it is a JSONObject, so instead of

JSONArray jsonarray = new JSONArray(response);

you need to use

JSONObject jsonObj = new JSONObject(response);

That's why the error message says type org.json.JSONObject cannot be converted to JSONArray.

Once you have the object, you can get objects or arrays that are nested within in. In this case, your JSON string looks like this:

{"dsdata":{"ttlogin":[{"BundyId":"9090","Name":"IT DEVELOPMENT"}]}}

so if you wanted to get the "ttlogin" array you could use

JSONObject jsonObj = new JSONObject(response);
JSONArray arr = jsonObj.getJSONObject("dsdata").getJSONArray("ttlogin");
  • Related