2012-04-26 86 views
1

我正在從android中的AsyncTask向服務器發出HTTP post請求發佈請求。Android中的Mange HTTP響應

我在管理響應時遇到的問題。在這裏我如何閱讀回覆。

HttpEntity entity =response.getEntity(); 
if (entity != null) { 

    DataInputStream in = new DataInputStream(entity.getContent()); 
    String str; 
    while ((str = in.readLine()) != null){ 


    Log.e("Debug","Server Response second url"+str); 

} 
    in.close(); 
} 

的問題是我收到的作品的反應,但我希望他們在一塊

電流響應

Server Response second url 
Server Response second url<header><status>Success</status><message>Check the Numbers: 
Server Response second url123456.</message><vbal>5005</vbal></header> 

預期的響應

<header><status>Success</status><message>Check the Numbers:123456.</message><vbal>5005</vbal></header> 

如果我將它在一塊我可以輕鬆解析xml響應並獲取所需的值。

回答

1

這應該爲你工作:

StringBuilder sb = new StringBuilder(); 
String str; 
while ((str = in.readLine()) != null) { 
    sb.append(str); 
} 
in.close(); 
str = sb.toString(); //here you have the string you need. 
Log.e("Debug","Server Response second url" + str); 
1

如果你有Apache的百科全書:

Reader in = new InputStreamReader(entity.getContent(), "UTF-8"); 

StringWriter writer = new StringWriter(); 
IOUtils.copy(in, writer); 

String str = writer.toString(); 

否則,

final char[] buffer = new char[0x10000]; 
StringBuilder out = new StringBuilder(); 
Reader in = new InputStreamReader(entity.getContent(), "UTF-8"); 

try { 
    int read; 

    do { 
     read = in.read(buffer, 0, buffer.length); 
     if (read > 0) 
      out.append(buffer, 0, read); 
    } while (read >= 0); 
} finally { 
    in.close(); 
} 

String str = out.toString();