2011-08-10 4 views
5

Por ejemplo, si necesitamos enviar contenido que esté en este formato, ¿cómo lo hacemos? {"name1": [{ "name11": "value11"}, {"name11": "value12"}, {"name11": "value13"}], "name2": value2}apache httppost cómo configurar el contenido: que tienen un par de valores nominales que apuntan a otro conjunto de pares de valores nominales

Sé cómo configurar el tipo básico { "nombre1": "valor1", "nombre2": valor2}

NameValuePair[] nameValuePairs = new NameValuePair[2]; 
      nameValuePairs[0]= new BasicNameValuePair("name1", "value1"); 
      nameValuePairs[1] = new BasicNameValuePair("name2", value2); 

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

¿Cómo podemos lograr que anidan

Respuesta

7

favor ver this pregunta porque tiene un par de respuestas que deberían ayudarte. He aquí una breve fragmento del código de respuestas:

HttpPost request = new HttpPost(serverUrl); 
request.setEntity(new ByteArrayEntity(
    postMessage.toString().getBytes("UTF8"))); 
HttpResponse response = client.execute(request); 

La otra respuesta dice que usted puede hacer algo como esto:

protected void sendJson(final String email, final String pwd) { 

       HttpClient client = new DefaultHttpClient(); 
       HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 
       HttpResponse response; 
       JSONObject json = new JSONObject(); 
       try{ 
        HttpPost post = new HttpPost(URL); 
        json.put("email", email); 
        json.put("password", pwd); 
        StringEntity se = new StringEntity("JSON: " + json.toString()); 
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
        post.setEntity(se); 
        response = client.execute(post); 
        /*Checking response */ 
        if(response!=null){ 
         InputStream in = response.getEntity().getContent(); //Get the data in the entity 

       } 
       catch(Exception e){ 
        e.printStackTrace(); 
        createDialog("Error", "Cannot Estabilish Connection"); 
       } 

      } 
+2

:) gracias por tonelada, la primera opción que funcionó para mí .I acaba de crear una cadena en el formato que necesitaba, como el valor de par de nombre anidado y el ByteArrayEntity utilizado – Kavitha

Cuestiones relacionadas