2012-05-21 14 views
9

Zrobiłem to:coraz ciało odpowiedź httpResponse

response = httpclient.execute(targetHost, httppost); 
    if(response.getStatusLine().getStatusCode() == 200) 
         { 
    HttpEntity entity = response.getEntity(); 
    System.out.println("Entity:"+entity); 
    if (entity != null) 
          { 
     String responseBody = EntityUtils.toString(entity); 
     System.out.println("finalResult"+responseBody.toString()); 
          } 

Chodzi o to, że pierwsze println() wyświetlacze następująco: [email protected] co jest dobre.

Ale drugi System.out.println("finalResult"+responseBody.toString()); wyświetla tylko ten finalResult. Więc co jest nie tak z tym:

String responseBody = EntityUtils.toString(entity); 
      System.out.println("finalResult"+responseBody.toString()); 

???

WAŻNE To HttpEntity entity = response.getEntity(); jest równe [email protected]. W takim przypadku problem musi być następujący:

String responseBody = EntityUtils.toString (entity) ;.

Proszę o pomoc !!!

Odpowiedz

24

Po pierwsze, sprawdź, czy Twój serwer nie zwraca pusty odpowiedzi:

response.getEntity().getContentLength(); //it should not be 0 

drugie, spróbuj wykonać następujące czynności w celu przekształcenia odpowiedzi na wyrażenie:

StringBuilder sb = new StringBuilder(); 
try { 
    BufferedReader reader = 
      new BufferedReader(new InputStreamReader(entity.getContent()), 65728); 
    String line = null; 

    while ((line = reader.readLine()) != null) { 
     sb.append(line); 
    } 
} 
catch (IOException e) { e.printStackTrace(); } 
catch (Exception e) { e.printStackTrace(); } 


System.out.println("finalResult " + sb.toString()); 
+0

wyświetla tylko finalResult. Nie wiem dlaczego! – adrian

+0

oznacza to, że nie ma danych zwróconych z usługi internetowej – waqaslam

+0

, ale kiedy wyświetlam HttpEntity entity = response.getEntity(); \t \t \t \t \t \t System.out.println ("finalResult" + encja); pokazuje [email protected] Oznacza to, że odpowiedź z usługi sieciowej nie jest zerowa, prawda? – adrian

1

Spróbuj tego:

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
String body = ""; 
while ((body = rd.readLine()) != null) 
{ 
    Log.e("HttpResponse", body); 
} 
+1

rd jest [email protected] ale body = rd.readLine(); ma wartość null! – adrian

0

spróbuj tego

BufferedReader in = new BufferedReader(new InputStreamReader(response 
         .getEntity().getContent())); 

       //SB to make a string out of the inputstream 
       StringBuffer sb = new StringBuffer(""); 
       String line = ""; 
       String NL = System.getProperty("line.separator"); 
       while ((line = in.readLine()) != null) { 
        sb.append(line + NL); 
       } 
       in.close(); 

       //the json string is stored here 
      String result = sb.toString(); 
1

Spróbuj tego:

HttpEntity entity = response.getEntity(); 
final String content; 
    try 
    { 
     content = EntityUtils.toString(entity); 

     runOnUiThread(new Runnable() 
     { 

      @Override 
      public void run() 
      { 

       webView.loadData(content, "text/html", "UTF-8"); 

      } 
     }); 
    } 
3
[email protected] 

odpowiedź przyjść kiedy bezpośrednio drukować HttpEntity przedmiot. np:

HttpEntity httpEntity=httpResponse.getEntity(); 

teraz na uzyskanie rzeczywistej odpowiedzi z serwera musimy wykonać następujące kroki:

public String convertStreamtoString(InputStream is){ 

    String line=""; 
    String data=""; 
    try{ 
     BufferedReader br=new BufferedReader(new InputStreamReader(is)); 
     while((line=br.readLine())!=null){ 

      data+=line; 
     } 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 
     return data; 
} 

zadzwoń powyżej metody i przekazać httpEntity jako argument. Cieszyć się!!

5

Można użyć tego:

String s = EntityUtils.toString(httpRes.getEntity());