在HttpPost请求后无法获取Json数据:

时间:2013-08-13 20:26:38

标签: android json rest http-post

我正在尝试向HttpPost服务器发出REST请求,该服务器应该返回一些JSON,但它不会返回任何内容,而我的HttpResponse对象的statusLineHTTP/1.1 400

这是我的代码

protected void onCreate(Bundle savedInstanceState) 
{
 //some code
String login = ((TextView)findViewById(R.id.Login)).getText().toString();
String password = ((TextView)findViewById(R.id.Password)).getText().toString();
List<NameValuePair> paramsList=new ArrayList<NameValuePair>();
paramsList.add(new BasicNameValuePair("rquest", "login"));
paramsList.add(new BasicNameValuePair("&login", login));
paramsList.add(new BasicNameValuePair("&pwd", password));
rq.login2(paramsList);
}

void login2(List<NameValuePair> paramsList)
{
    HttpClient httpclient = new DefaultHttpClient();  
    HttpPost request = new HttpPost("http://darate.free.fr/rest/api.php");

    try 
    {
        request.setEntity(new UrlEncodedFormEntity(paramsList));    
    } 
    catch (UnsupportedEncodingException e) 
    {
        e.printStackTrace();
    }

    ResponseHandler<String> handler = new BasicResponseHandler();  
    String result = null;
    try 
    {  
      HttpResponse sult = httpclient.execute(request);
      Log.i("Hossam: ",sult.toString());
    }
    catch (ClientProtocolException e) 
    {  
        e.printStackTrace();  
    } 
    catch (IOException e) 
    {  
        e.printStackTrace();  
    }  
    httpclient.getConnectionManager().shutdown();  
}
}

在服务器端,这是我的请求后需要执行的功能:

服务器端登录功能

private function login(){
        // Cross validation if the request method is POST else it will return "Not Acceptable" status
        if($this->get_request_method() != "POST"){
            $this->response('',406);
        }

        $login = $this->_request['login'];      
        $password = $this->_request['pwd'];

        // Input validations
        if(!empty($email) and !empty($password)){
            if(filter_var($email, FILTER_VALIDATE_EMAIL)){
                $sql = mysql_query("SELECT id, firstname, lastname FROM users WHERE login = '$login' AND password = '$password' LIMIT 1", $this->db);
                if(mysql_num_rows($sql) > 0){
                    $result = mysql_fetch_array($sql,MYSQL_ASSOC);

                    // If success everythig is good send header as "OK" and user details
                    $this->response($this->json($result), 200);
                }
                $this->response('', 204);   // If no records "No Content" status
            }
        }

        // If invalid inputs "Bad Request" status message and reason
        $error = array('status' => "Failed", "msg" => "Invalid Email address or Password");
        $this->response($this->json($error), 400);
    }

1 个答案:

答案 0 :(得分:0)

在我的情况下,我也使用Json作为请求:

StringEntity entity = new StringEntity(json.toString(), "utf-8");
httppost.setEntity(entity);

在这种情况下,您需要正确构建 json 参数。

示例:

JSONObject json = new JSONObject();
json.put("pass", "123");

修改  别忘了这个:

httppost.setHeader("content-type", "application/json; charset= utf-8");
httppost.setHeader("Accept", "application/json");