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 に答える