2015-01-05 30 views
1

我想解析來自php服務器的json響應。但是當我嘗試登錄並使用時,我只能在手機上獲得部分響應。但在瀏覽器加載url時,我得到了完整的響應。在調試模式下,響應字符串變量具有完整數據Android在手機中獲取部分json字符串響應,但在瀏覽器中獲得完整響應

HttpPost httpPost = new HttpPost(url); 
    httpPost.setEntity(new UrlEncodedFormEntity(params)); 
    httpResponse = httpClient.execute(httpPost); 
    httpEntity = httpResponse.getEntity(); 

if (httpEntity != null) { 

      // A Simple JSON Response Read 
      InputStream instream = httpEntity.getContent(); 
      response = convertStreamToString(instream); 
      // now you have the string representation of the HTML request 
      instream.close(); 
     } 


    private static String convertStreamToString(InputStream is) { 

    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
      reader.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 
+0

我目前有在這裏同樣的問題:http://stackoverflow.com/questions/29223980/json-string-cut-off-half-way -java-inputstream你是否設法解決這個問題? – Zy0n

回答

0

嘗試在textview中設置響應並查看,如果字符串大小更大並且logcat無法顯示,則會修剪該字符串。

+0

但試圖解析字符串時,它是不完整的。沒有完整的json字符串。長度僅爲-6055。 –

0

嘗試做類似如下

   /******/ 


try { 
URL u = new URL(url); 
HttpURLConnection c = (HttpURLConnection) u.openConnection(); 

c.connect(); 


InputStream in = c.getInputStream(); 

byte[] buffer = new byte[1024]; 
int len1 = 0; 
while ((len1 = in.read(buffer)) > 0) { 
    getData(in); 
} 
//f.close(); 

} catch (MalformedURLException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (ProtocolException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (FileNotFoundException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 



/*** stored in builder**/ 
StringBuilder builder = new StringBuilder(); 
void getData(InputStream is){ 
try { 


    BufferedReader in = new BufferedReader(new InputStreamReader(is)); 

    String line; 
    while((line = in.readLine()) != null){ 
     try { 
      builder.append(line); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 



} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
} 
相關問題