发送PHP数组与邮政的android [英] sending php array with POST android

查看:136
本文介绍了发送PHP数组与邮政的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过POST从机器人发送一个PHP数组到PHP的服务器和我有这个code

  HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(URL);
StringEntity dades =新StringEntity(数据);
httppost.setEntity(dades);

//执行HTTP POST请求
HTT presponse响应= httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
返回resEntity.getContent();
 

我认为PHP的阵列可能是可以进去 StringEntity dades =新StringEntity(数据); (数据是PHP数组)。谁能帮我?

解决方案

 公共无效POSTDATA(){
//创建一个新的HttpClient和门柱头球
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(http://www.yoursite.com/script.php);

尝试 {
    //添加数据
    //你可以添加所有你的PHP需要在BasicNameValuePair的参数。
    //第一个参数是指名称例如PHP领域
    // $ n = $ _ POST ['身份证'];第二个参数是值。
    名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(2);
    nameValuePairs.add(新BasicNameValuePair(ID,12345));
    nameValuePairs.add(新BasicNameValuePair(StringData是,AndDev就是酷!));
    httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));

    //执行HTTP POST请求
    HTT presponse响应= httpclient.execute(httppost);

}赶上(ClientProtocolException E){
    // TODO自动生成的catch块
}赶上(IOException异常E){
    // TODO自动生成的catch块
}}
 

在code以上将发送一个这样的数组: [ID = 12345,StringData是= AndDev是酷!]

如果你想有一个bidimentional数组,你应该这样做。

 叠B =新包();
b.putString(ID,12345);
b.putString(StringData是,Android是酷);
nameValuePairs.add(新BasicNameValuePair(信息,b.toString()));
 

这将创建一个包含一个数组的数组:

  [信息=包[{ID = 12345,StringData是= Android是酷}]
 

我希望这是你想要的。

I want to send a php array via POST from android to php server and i have this code

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
StringEntity dades = new StringEntity(data);
httppost.setEntity(dades);

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
return resEntity.getContent();

I think that the php array may be can go in StringEntity dades = new StringEntity(data); (data is the php array). Can anyone help me?

解决方案

public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

try {
    // Add your data
    //you can add all the parameters your php needs in the BasicNameValuePair. 
    //The first parameter refers to the name in the php field for example
    // $id=$_POST['id']; the second parameter is the value.
    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);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}}

The code above will send an array like this: [id=12345, stringdata=AndDev is Cool!]

If you want a bidimentional array you should do this

Bundle b= new Bundle();
b.putString("id", "12345");
b.putString("stringdata", "Android is Cool");
nameValuePairs.add(new BasicNameValuePair("info", b.toString())); 

This will create an array containing an array:

[info=Bundle[{id=12345, stringdata=Android is Cool}]]

I hope this is what you want.

这篇关于发送PHP数组与邮政的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆