Click here to Skip to main content
14,880,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Java
When compiler reaches onPostExecute and trying to run execute JSONArray the line

jsonArray=jsonObject.getJSONArray("server_response");
throws an exception:

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



What I have tried:

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)*/**
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???
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900