2012-12-28 37 views
0

我的代碼來獲得圖像URL額外在每一個前JSON響應/ java中,Android的

DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     httpPost.setEntity(new UrlEncodedFormEntity(params)); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     InputStream inputStream = httpEntity.getContent(); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8); 
     StringBuilder stringBuilder = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      stringBuilder.append(line + "\n"); 
     } 
     inputStream.close(); 
     return stringBuilder.toString(); 

其中服務器的代碼是在PHP

但問題是有多餘的\每前/ 例如在數據庫中的圖像鏈接,http://www.dvimaytech.com/markphoto/upload/[email protected]/Pic.jpg 但每次都遇到http:\/\/www.dvimaytech.com\/markphoto\/upload\/[email protected]\/Pic.jpg

是這個問題不解決的,那麼另一種解決方案(對我來說它的最後一個選項)是刪除所有。

但是當我嘗試,使用url = url.replace("\",""); 它給語法錯誤String literal is not properly closed by a double-quote

回答

2

只需使用一個JSON解析器庫像GSON爲你解碼JSON的數據包。 http://code.google.com/p/google-gson/

它會讓你的生活更容易,避免必須string.replace()特定的字符。

+0

或[傑克遜(http://jackson.codehaus.org/),例如。無論哪種方式,都不應該用字符串替換來完成。另外,我會[驗證](http://jsonlint.com/)原始的JSON來查看它是否實際上是JSON。 – eis

1

可以使用下面的方法來處理

public static String extractFileName(String path) { 

    if (path == null) { 
     return null; 
    } 
    String newpath = path.replace('\\', '/'); 
    int start = newpath.lastIndexOf("/"); 
    if (start == -1) { 
     start = 0; 
    } else {  
     start = start + 1; 
    } 
    String pageName = newpath.substring(start, newpath.length()); 

    return pageName; 
} 
相關問題