Android发送GET而不是POST

时间:2013-04-22 16:56:17

标签: android post get

我的Android应用程序有一个后端,它在GET上返回404,在POST时返回json。现在,我正在尝试使用this snippet执行POST请求:

public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/api/login");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("email", "email@email.com"));
    nameValuePairs.add(new BasicNameValuePair("password", "qwerty"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}
}
然而,

服务器接收GET请求。使用curl POST后端按预期返回json。但不知何故,httpPost发送了GET(!)请求。可能是什么问题呢?我做错了什么?

1 个答案:

答案 0 :(得分:0)

好的,很快就回答了我自己的问题,可能对其他人有帮助。

我用ip地址替换了主机名,并且有效!

相关问题