如何正确形成Android HttpPost请求 - 服务器响应''406 Not Acceptable''

I have developed a small android application that communicates with server over the Http protocol, using json objects .

It was working all fine on my local machine, but when I decided (just in learning purposes) to upload my php script to some free hosting web service, I get the 406 ERROR.

I've done some research, and found that it must be that Content-type was not set in my HttpPost request. But setting Content-type didn't help.

This is the android code:

            ArrayList<NameValuePair> userData = new ArrayList<NameValuePair>();
            userData.add(new BasicNameValuePair("email", userData_email));
            userData.add(new BasicNameValuePair("pass", userData_pass));

            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
            HttpConnectionParams.setSoTimeout(httpParameters, 10000);

            HttpClient client = new DefaultHttpClient(httpParameters);

            HttpPost postRequest = new HttpPost(uri);

            // I tried this too
            // postRequest.setHeader("Content-Type", "application/json");

            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(userData);

            ent.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

            postRequest.setEntity(ent);

            HttpResponse response = client.execute(postRequest);

            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity);

            Log.e("SERVER: ", result);

            JSONObject json = new JSONObject(result); 

            userData_fname = json.getString("fname");
            userData_lname = json.getString("lname");
            userData_age = json.getInt("age");
            userData_gender = json.getString("gender");

And this is the code from the server (really nothing fancy):

$mysql_host = 'xxxx';
$mysql_user = 'xxxx';
$mysql_pass = 'xxxx';
$mysql_db = 'xxxx';

if (isset($_POST["email"]) && isset($_POST["pass"])) {
$email = $_POST["email"];
$pass = $_POST["pass"];

if(!mysql_connect($mysql_host, $mysql_user, $mysql_pass) || !mysql_select_db($mysql_db)) {
    echo '-1';
}

$query = "SELECT * FROM `users` WHERE `email`='".$email."' AND `password`='".$pass."'";
if($query_run = mysql_query($query)){

    $firstname = mysql_result($query_run, 0, 'firstname');
    $lastname = mysql_result($query_run, 0, 'lastname');
    $age = mysql_result($query_run, 0, 'age');
    $gender = mysql_result($query_run, 0, 'gender');

    $return_data = array (
        'fname' => $firstname,
        'lname' => $lastname,
        'age' => $age,
        'gender' => $gender
    );

    echo json_encode($return_data);

} else {
    echo 'not found';
    exit();
   }
}

Now, it may be something with my php code (missing something maybe), but since I know only basics of php I'm asking you for help.

Thank you in advance.

If you wanna be sure about your php code you have to open that page from a browser and if you will see your data in JSON format then server is working fine! Otherwise your server doesn't retrieve json_encode() function.

You change:

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
HttpConnectionParams.setSoTimeout(httpParameters, 10000);`
HttpClient client = new DefaultHttpClient(httpParameters);

To:

HttpClient httpclient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(),
            timeOut);
HttpConnectionParams.setSoTimeout(httpclient.getParams(), timeOut);`

Surely, it works.