【发布时间】: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 ,是的,类似的东西,但关键是我对此感到空白