2011-02-12 40 views
0

我正在執行名稱/值對的HTTP POST,然後嘗試獲取HTTP響應正文(並將其放入字符串中,稱爲描述)。無法從http響應中找到正文數據

我無法找到訪問響應消息正文的方法。我的下面是迄今爲止我所提出的最好的,但它不起作用。 「description」變量只是空的。

最終目標是在服務器端獲取代碼,該代碼接收發布數據並返回包含有用信息的消息主體。現在我只有一個stub html,它始終返回相同的內容(請參見下面的html)。換句話說,應用程序會將DTC(汽車診斷故障代碼)發送到服務器,服務器將查找它並將描述文本發送回HTML主體。

源代碼的方法:

public String getDTCDescription (String DTC) { 
    String description = ""; 

    String url = "http://site/test.html"; 
    List<NameValuePair> args = new ArrayList<NameValuePair>(); 

    args.add(new BasicNameValuePair("testkey","testValue")); 

    HttpResponse h; 

    h = postData(url, args); 

    BufferedInputStream content = null; 
    try { 
     content = new BufferedInputStream(h.getEntity().getContent()); 
    } catch (Exception e) { 
     Log.e("NetDTCInfo",e.getMessage()); 
    } 

    if (content == null) { 
     return ""; 
    } 

    // try and loop through all the data waiting on the input stream. 
    try { 
     while (content.available() > 0) { 
      Log.d("NetDTCInfo","Reading a byte..."); 
      description = description + (char) content.read(); 
      Log.d("NetDTCInfo","Description is now: " + description); 
     } 
    } catch (IOException e) { 
     Log.e("NetDTCInfo","Error while reading. " + e.getMessage()); 
    } 


    return description; 
} 

源代碼文件的test.html

<html> 
     <body> 
       <pre> 
         P0521|This is a description of DTC code P0521. 
       </pre> 
     </body> 
</html> 

回答

1

它看起來你的戰略思考做,這是複雜的,rahter比respose發送HTML頁面只需發送一個你最終想要在你的客戶端獲得的字符串。如果在響應消息中有更復雜的數據,那麼我會建議一件事,然後在客戶端使用XML響應和SAX解析。

+0

所以你是說如果我從響應中刪除HTML格式併發送一個字符串(八位字節流)來清除問題? – 2011-02-13 01:44:17