我的 android 应用程序有一个后端,它在 GET 上返回 404,在 POST 上返回 json。现在,我正在尝试使用此代码段执行 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(!) 请求。可能是什么问题呢?我究竟做错了什么?