Android在手机中获得部分json字符串响应,但在浏览器中获得完整响应

时间:2015-01-05 09:25:23

标签: android http

我正在尝试从php服务器解析json响应。但是当我尝试登录并使用时,我只能通过电话获得部分响应。但是我在浏览器加载URL时得到了完整的响应。在调试模式下,响应字符串变量具有完整数据。

   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();
}

2 个答案:

答案 0 :(得分:0)

尝试在textview中设置响应并查看,如果字符串大小较大且如果无法显示logcat,则会修剪字符串。

答案 1 :(得分: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();
}
}