繁体   English   中英

发送http post请求无效

[英]Sending a http post request is not working

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

把www放在请求网址中,它就解决了。 有一个htaccess文件纠正了没有www的所有网址。

Volley Library(来自谷歌的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

这里有与你相同的问题: 在android中优化HTTP请求

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM