0

我正在尝试向服务器发出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);
    }
4

1 回答 1

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");
于 2013-08-13T20:56:15.500 回答