Birendra Prasad
Birendra Prasad

Reputation: 1

JSONException: Value cannot be converted to JSONObject

When compiler reaches onPostExecute and trying to run execute JSONArray the line

jsonArray=jsonObject.getJSONArray("server_response");

throws an exception:

org.json.JSONException: 
    Value[{"code":"login_true","name":"hhh","email":"hhh"}]
    of type org.json.JSONArray cannot be converted to JSONObject".

What is the correct statement?

My code:

protected String doInBackground(String... params)
    {
     String call_type=params[0];
        if(call_type.equals("login"))
        {
            try {
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)     url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String email,pass;
                email=params[1];
                pass=params[2];

                String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" +
                        URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(pass, "UTF-8");

                bufferedWriter.write(data);


                InputStream IS = httpURLConnection.getInputStream();
                BufferedReader BR= new BufferedReader(new InputStreamReader(IS));
                StringBuilder stringBuilder = new StringBuilder();
                String line="";
                while ((line=BR.readLine())!=null)
                {
                    stringBuilder.append(line+"\n");
                }
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                //IS.close();
                httpURLConnection.disconnect();
                Thread.sleep(500);
                Log.d("Test","Test 3 pass");
                return  stringBuilder.toString().trim();

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
**strong text**/*my onPostExecute method is as below;*/***

 @Override 
    protected void onPostExecute(String json)
    {

        try
        {
            progressDialog.dismiss();

            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray=jsonObject.getJSONArray("server_response");
            *******//here exception coming org.json.JSONException: Value [{"code":"login_true","name":"hhh","email":"hhh"}] of type org.json.JSONArray cannot be converted to JSONObject and compiler jumps to the exception part*******
            JSONObject JO= jsonArray.getJSONObject(0);
            String code=JO.getString("code");
            String message=JO.getString("message");

            if(code.equals("reg_true"))
            {
                ShowDialog("Registration Success",message,code);
            }
            else if (code.equals("reg_false"))
            {
                ShowDialog("Registration Fail",message,code);
            }
            else if(code.equals("login_true"))
            {
                Intent intent= new Intent(activity,HomeActivity.class);
                intent.putExtra("message",message);
                activity.startActivity(intent);
            }
            else if(code.equals("login_false"))
            {
                ShowDialog("Login Error,",message,code);
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
**/* php scrip is as follows: (login.php)*/**
<?php
    require"init.php";

$email=$_POST["email"];
$pass=$_POST["pass"];

$sql_query="select name, email from beneficiary_details where email like '".$email."' and pass like '".$pass."' ";
$result=mysqli_query($con,$sql_query);
$response = array();

 $rowcount=mysqli_num_rows($result);
//print_r( $rowcount);
    if($rowcount > 0)
    {

        $row=mysqli_fetch_row($result);
        $name=$row[0];
        $email=$row[1];
        $code="login_true";

        array_push($response,array("code"=>$code,"name"=>$name,"email"=>$email));
        echo json_encode($response);
    }
    else
    {
        $code="login_false";
        $message="User not found, Please try again..";
        array_push($response,array("code"=>$code,"message"=>$message));
        echo json_encode($response);
    }
    mysqli_close($con);
?>
**plz find out where I am doing mistake???

Upvotes: 0

Views: 314

Answers (1)

Clive Seebregts
Clive Seebregts

Reputation: 2024

Assuming the JSON returned from the server is:

[{"code":"login_true","name":"hhh","email":"hhh"}]

To parse the above JSON:

try {
  JSONArray jsonArray = new JSONArray(json);
  JSONObject jsonObject = jsonArray.getJSONObject(0);
  String code = jsonObject.getString("code");
  String name = jsonObject.getString("name");
} catch (JSONException e) {
  e.printStackTrace();
}

Upvotes: 1

Related Questions