簡體   English   中英

如何處理Android 1.6至2.x中的版本問題

[英]How to deal with version issues in android 1.6 to 2.x

我在Android中為1.6構建了一個JSON使用者(這是在最大支持的最舊版本上構建的好習慣)。 我能夠在1.5-1.6下成功檢索json。 但是,我只是將應用程序放到了droid(2.x)上,現在我收到“ org.json.jsonexception:字符....的預期文字值”。 為什么版本不同? 我該如何處理?

我有同樣的錯誤信息。 在我的情況下,事實證明JSON庫是混亂的,因為返回的JSON中的引號(“)轉義了。這顯然使它與字符串的長度混淆,並導致了異常。

如果無法使用該庫,請嘗試使用GSON,這很棒

http://code.google.com/p/google-gson/

我有一種方法可以將InputStream轉換為JSON的字符串表示形式。 我需要將子字符串從(intial,length()-1)調整為(initial,length()-2),以使其在1.5和2.x中都能正常工作。 謝謝您的幫助。

private static String convertStreamToString(InputStream is) {  
   /* 
    * To convert the InputStream to String we use the BufferedReader.readLine() 
    * method. We iterate until the BufferedReader returns null which means 
    * there's no more data to read. Each line will appended to a StringBuilder 
    * and returned as String. 
    */  
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));  
    StringBuilder sb = new StringBuilder();  

    String line = null;  
    try {  
        while ((line = reader.readLine()) != null) {  
            sb.append(line + "\n");  
        }  
    } catch (IOException e) {  
        e.printStackTrace();  
    } finally {  
        try {  
            is.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }
    // API is defective and does not return correct json response.
    // Reformat string response to confirm to expected json response
    // return sb.toString();
    return  "{\"Results\":" + sb.substring(11, sb.length()-2) + "}\n" ;
}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM