dongzhuang6417 2013-10-24 20:02
浏览 154

如何正确形成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.

  • 写回答

2条回答 默认 最新

  • douou1891 2013-10-24 20:10
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 comsol仿真等离激元
  • ¥15 静电纺丝煅烧后如何得到柔性纤维
  • ¥15 (标签-react native|关键词-镜像源)
  • ¥100 照片生成3D人脸视频
  • ¥15 伪装视频时长问题修改MP4的时长问题,
  • ¥15 JETSON NANO
  • ¥15 VS开发qt时如何在paintgl函数中用pushbutton控制切换纹理
  • ¥20 关于 openpyxl 处理excel文件地问题
  • ¥15 MS中不知道高分子的构型怎么构建模型
  • ¥60 QQOP数据,什么是op数据号,怎么提取op数据!能不能大量提取(语言-c语言)