0

我正在制作一个 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 数组。

4

2 回答 2

2

将 www 放入请求 url 中就解决了。有一个 htaccess 文件可以更正所有没有 www 的 url。

于 2012-10-08T09:51:41.310 回答
0

Volley 库(来自 google 的 sim-official)更好,http、https 等 https://developers.google.com/live/shows/474338138

这里有一个非常小的示例:
https ://github.com/ogrebgr/android_volley_examples /blob/master/src/com/github/volley_examples/Act_SimpleRequest.java

这里有和你一样的问题:Optimizing HTTP requests in android

于 2013-08-14T08:42:20.930 回答