0

I am returning values from the server side using a php wrapper via JSON. But the the following error occurs when I am returning the value to the client side.

This is my client side code

        @Override
    protected Boolean doInBackground(String... arg0) {

        try {

            // Setup the parameters
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("FirstNameToSearch",
                    strNameToSearch));
            // Create the HTTP request
            HttpParams httpParameters = new BasicHttpParams();

            // Setup timeouts
            HttpConnectionParams
                    .setConnectionTimeout(httpParameters, 45000);
            HttpConnectionParams.setSoTimeout(httpParameters, 45000);

            HttpClient httpclient = new DefaultHttpClient(httpParameters);
            HttpPost httppost = new HttpPost(
                    "http://172.16.12.142/etsmobile/menuload.php");

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity);       

            // Create a JSON object from the request response
            JSONObject jsonObject = new JSONObject(result);

            // Retrieve the data from the JSON object
            pasName = jsonObject.getString("Name");
            pasPost = jsonObject.getString("Post");
            pasStation = jsonObject.getString("Station");


        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return true;
    }

This is my Server Side Code

<?php

$firstname = $_POST["FirstNameToSearch"];

$con = mysql_connect("localhost", "root", "") or die("Unable to connect to MySQL");


if (mysqli_connect_errno()) {
    echo 'Database connection error: ' . mysqli_connect_error();
    exit();
}

$selected = mysql_select_db("ets", $con) or die("Could not select ets");

$userdetails = mysql_query("SELECT users.* FROM login, users WHERE username = '$firstname' and login.emp_no=users.emp_no");
$getUser_result = mysql_fetch_assoc($userdetails);


$name = $getUser_result['name'];
$post = $getUser_result['post'];
$station = $getUser_result['station'];

mysql_close($con);

$result_data = array('Name' => $name, 'Post' => $post, 'Station' => $station);
//print_r($result_data);
echo json_encode($result_data);
?> 

This is my JSON Output

{"Name":"Sameera Yatawara","Post":"Station Master","Station":"Dematagoda"} 
12
  • response is a string not a jsonobject Commented Dec 16, 2013 at 19:24
  • @SotiriosDelimanolis I posted my JSON. Commented Dec 16, 2013 at 19:29
  • @Raghunandan I have converted the string to a JSON when I am echoeing the resultset. Commented Dec 16, 2013 at 19:32
  • Are your db connection parameters OK? Commented Dec 16, 2013 at 19:35
  • @Leonardo Yes. They are okay and the SQL returns values. Commented Dec 16, 2013 at 19:37

1 Answer 1

1

Try retrieving data with something like this:

...
String result = EntityUtils.toString(entity);

JSONArray array = (JSONArray) new JSONTokener(result).nextValue(); 
JSONObject json = array.getJSONObject(0);
json.getString('name');
...

or

...
String result = EntityUtils.toString(entity);

JSONObject json = (JSONObject ) new JSONTokener(result).nextValue();
json.getString('name');
...

Also check the PHP script encoding. I had similar problem. I set encoding to UTF-8 with DOM in text editor (Notepad++,..) and it start working.

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.