我正在制作一个 Android 应用程序并在模拟器上运行它。当我发送http post请求时,服务器上的所有post参数都是空的。这是 Android 应用程序中的代码:
public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(page);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
InputStreamReader inreader = new InputStreamReader(response.getEntity().getContent());
BufferedReader reader = new BufferedReader(inreader);
String line = "";
while((line = reader.readLine()) != null){
System.out.println(line);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
这是服务器上的代码:
<?php
print_r($_POST);
?>
当我在模拟器上运行它时,它返回一个空的 php 数组。
最佳答案
在请求url中加上www就解决了。有一个 htaccess 文件纠正了所有不带 www 的 url。
关于java - 发送 http post 请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12777996/