【问题标题】:HttpPost can't get a value in phpHttpPost无法在php中获取值
【发布时间】:2014-05-15 07:12:56
【问题描述】:

我正在尝试使用HTTP 发布一些值, 但是每当我发布我的值时,我在 PHP 方面都会变得空白。 我的代码有问题吗?

代码:

public void postData() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();

            //httppost url
        HttpPost httppost = new HttpPost("http://192.168.x.x:80/phptest");

        try {


            // Add data in name-value parilist
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("people[name]", "Shyam"));
            nameValuePairs.add(new BasicNameValuePair("people[sex]", "male"));
            nameValuePairs.add(new BasicNameValuePair("people[birthyear]", "1999"));


            //http-enity                
            HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);

                    //setting httppost header and entity
            httppost.addHeader(entity.getContentType());
            httppost.setEntity(entity);

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            HttpEntity resEntity = response.getEntity();  
            if (resEntity != null) {    
                   //print httprespose   
                   Log.i("RESPONSE",EntityUtils.toString(resEntity));
            }

【问题讨论】:

  • 发布时 http 标头是否为空?例如,在 chrome devtools 中检查它。
  • 能把php代码也贴一下吗?
  • 你的 PHP 代码是做什么的?
  • 如何在 php 中获取数据?你必须像 $_POST['people'] 这样在 php 中得到它
  • @tauitdnmd ,是的,类似的东西,但关键是我对此感到空白

标签: php android http-post


【解决方案1】:

2 分,

为什么你使用这些名称作为参数?你真的需要他们这样吗?

nameValuePairs.add(new BasicNameValuePair("people[name]", "Shyam"));
nameValuePairs.add(new BasicNameValuePair("people[sex]", "male"));
nameValuePairs.add(new BasicNameValuePair("people[birthyear]", "1999"));

他们应该是...new BasicNameValuePair("name", "Shyam"));...new BasicNameValuePair("people_name", "Shyam"));

第二点: 你如何在 PHP 端读取参数? 如果这是有效的参数名称,它将起作用 你必须这样阅读它们:$_POST["people[name]"]

如果你使用第二种命名方法,它应该是$_POST["name"]

【讨论】:

  • 是的,我需要它们,我想传递整个数组。
  • 最后,回显 $people['name']
猜你喜欢
  • 1970-01-01
  • 2015-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多