What i want to achieve is to send an Object using JSON from PHP and recieve it also as object in Java. When ever I used the below code it gives JSON exception from java saying String cannot be converterd to JSONObject. Please I will appreciate you help. Thanks
PHP code
<?php
require_once("/classes/Login.class.php");//Class that connect to the database
$user = $_GET['ballername'];
$pass = $_GET['ballerpassword'];
$log = new Login($user, $pass);
$log->connect();
//header('Content-type: application/json');
$correct = array('success'=> true)
echo json_encode($log);
?>
JAVA Code
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
ConnectToServer connectToServer = null;
String urlstring = null;
Campusian campusian;
@Override
protected Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service.
urlstring = CampusPalURL.LOGIN_URL +"ballername=" + mUserName +"&ballerpassword="+mPassword;
HttpClient client = new DefaultHttpClient();
HttpGet getMethod = new HttpGet(urlstring);
HttpResponse response;
boolean confirmation;
try {
response = client.execute(getMethod);
HttpEntity httpEntity = response.getEntity();
String state = EntityUtils.toString(httpEntity);
Log.e("STTTTTTTTTTTTTTTTTTTTTTTTTTTT ", state);
try {
JSONObject jsonObject = new JSONObject(state);
//JSONObject confirmation = jsonObject.getJSONObject("");
confirmation = jsonObject.getBoolean("logged_in");
return confirmation;
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.e("Object: ", e.getMessage());
e.printStackTrace();
}
} catch (IOException ee) {
// TODO Auto-generated catch block
Log.e("EXCEPTION: ", ee.getMessage());
ee.printStackTrace();
}
// TODO: register the new account here.
return true;
}
Error in Log
01-07 10:54:22.689: E/Object:(1022): Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
I decided to check the input received from the server displaying EntityUtils.toString(httpEntity) in the Log this is what i got. Is it right?
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <html xmlns="http://www.w3.org/1999/xhtml">
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <head>
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <title>Untitled Document</title>
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): </head>
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <body>
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <html xmlns="http://www.w3.org/1999/xhtml">
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <head>
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <title>Untitled Document</title>
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): </head>
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): <body>
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): </body>
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): </html>{"logged_in":true} </body>
01-07 11:44:37.171: E/STTTTTTTTTTTTTTTTTTTTTTTTTTTT(1380): </html>