java - 发送 http post 请求不起作用

标签 java php android http-post

我正在制作一个 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/

相关文章:

java - 即时创建 Object[][] 类型

php - 使用 Php Get 方法将 URL 中的空格更新为连字符

php - 无法创建 Laravel 项目,因为缺少 mcrypt 扩展

Android检测按下电源键

java - 无法启动 DSE 搜索的 solr 方面

java - 参数化 native 查询返回空结果集 [参数为逗号分隔字符串](使用 JPA)

java - Android Studio的Gradle Sync(SDK)问题

android - 身份验证请求失败 PayPal Android SDK 示例

java - JavaScript 中哪种类型的值对应于 Java`List<>`?

php - 将 DateTime 转换为 Date Doctrine php