2011-02-09 16 views
5

Non ottenere il corpo della risposta per la richiesta di post http in Android.
se ne fa menzione userid, password nelle coppie di valori nome e mettere nella richiesta HttpPost daNon si ottiene il corpo della risposta per la richiesta http post in Android?

postrequest.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

ma sull'esecuzione sto ottenendo la risposta come SC_OK (200), ma il corpo di risposta è null.

Un altro problema è se si specifica l'intestazione nella richiesta HTTPGet o HTTPPost utilizzando la funzione setHeader() che ottengo risposta BAD REQUEST (404).

Si prega di aiutarmi a risolvere il problema di cui sopra.

Grazie & saluti,
SSuman185

+1

inserire il codice dove si chiama e leggere la risposta. – Aliostad

+1

Hai verificato che il server abbia effettivamente inviato una risposta? – sstn

+0

Il server non è nella nostra azienda, il suo server di terze parti. E sto ricevendo la richiesta BAD e dall'intestazione e dai registri di rete ho scoperto che la risposta proviene solo dal server – Suman

risposta

0

Ciao amico pl provare questo modo per ottenere risposta dal server ..

Dove link indicato il link dove u desidera inviare la richiesta ..

String link2 = "http://184.106.227.45/quaddeals/university-of-illinois/androids_deals/user_card_list.json"; 

DefaultHttpClient hc1 = new DefaultHttpClient(); 

ResponseHandler<String> res1 = new BasicResponseHandler(); 

HttpPost postMethod1 = new HttpPost(link2); 

List<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>(1); 

nameValuePairs1.add(new BasicNameValuePair("user_id",account_userId)); 

postMethod1.setEntity(new UrlEncodedFormEntity(nameValuePairs1)); 

String response1 = hc1.execute(postMethod1, res1); 
+0

Ciao Venkatesh, in realtà io uso HttpClient e client.execute() e funziona bene nel Target ma non nel simulatore, dato che stiamo usando il proxy nella nostra azienda ho provato a mettere il proxy al simulatore con -http-proxy come bene, ma ancora senza fortuna – Suman

1

ecco un metodo per POST facile a una pagina e ottenere la risposta come una stringa. Il primo parametro (HashMap) è un elenco di valori-chiave dei parametri che si desidera inviare. Non vi è alcuna gestione degli errori, quindi dovrete farlo:

public static String doPost(HashMap<String,String> params, String url) { 
     HttpPost httpost = new HttpPost(url); 
     try { 
      httpost.setURI(new URI(url)); 
     } catch (URISyntaxException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     HttpResponse response = null; 

     List <NameValuePair> nvps = new ArrayList <NameValuePair>(); 

     Set entryset = params.entrySet(); 
     Iterator iterator = entryset.iterator(); 
     Log.d("Posted URL: ", url+" "); 
     while(iterator.hasNext()) { 
      Map.Entry mapentry = (Map.Entry)iterator.next(); 
      String key = ((String)mapentry.getKey()).toString(); 
      String value = (String)mapentry.getValue(); 
      nvps.add(new BasicNameValuePair(key, value)); 

      Log.d("Posted param: ", key+" = "+value); 
     } 

     try { 
      httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 
      try { 
       httpost.setURI(new URI(url)); 
      } catch (URISyntaxException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      response = getClient().execute(httpost); 
      HttpEntity entity = response.getEntity(); 

      return convertStreamToString(entity.getContent()); 
     } catch (Exception e) { 
      // some connection error occurred, nothing we can do 
      e.printStackTrace(); 
     } 

     return null; 
    } 
Problemi correlati