2012-02-02 10 views
5

quiero enviar una matriz PHP a través de POST del androide al servidor php y tengo este códigoenvío de matriz PHP con el poste androide

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(); 

creo que la matriz PHP puede ser puede ir en StringEntity dades = new StringEntity(data); (data es la matriz php). ¿Alguien puede ayudarme?

+0

Normalmente lo hago de esta manera http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient – jsaye

+0

@jsaye: que califica como respuesta, coloque el código, alguna explicación, el enlace abajo para obtener un representante – konsolenfreddy

+0

@konsolenfreddy: gracias por su consejo, soy realmente nuevo aquí y, a veces, no sé qué hacer – jsaye

Respuesta

6
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 
}} 

El código anterior enviará una matriz de esta manera: [id=12345, stringdata=AndDev is Cool!]

Si desea una matriz bidimensional que debe hacer esta

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

Esto creará una matriz que contiene una matriz:

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

Espero que esto sea lo que querer.

+0

pero quiero enviar una matriz bidimensional ... –

+3

http://www.coderanch.com/t/533566/Android/Mobile/sending-array-namevaluepairs-http-post ¡aquí está la solución! –

9

se puede hacer algo como esto:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("colours[]","red")); 
nameValuePairs.add(new BasicNameValuePair("colours[]","white")); 
nameValuePairs.add(new BasicNameValuePair("colours[]","black")); 
nameValuePairs.add(new BasicNameValuePair("colours[]","brown")); 

donde el color es su etiqueta de matriz. Simplemente usuario [] después de su etiqueta de matriz y poner valor. P.ej. si el nombre de su matriz es colour, úselo como colour[] y ponga el valor en bucle.

+1

tenía 5 id. De lista de correo, nombre, dirección, etc. ¿cómo puedo agregar todo este valor en la lista o matriz sigle? – PankajAndroid

Cuestiones relacionadas