1

I am trying to parse json response from php server. But I am getting only partial response on phone, when I am trying to log it and use. But I am getting full response when loading url at browser. In debug mode response string variable has full data.

   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();
}
Sino
  • 886
  • 7
  • 21
  • I'm currently having the same issue over here: http://stackoverflow.com/questions/29223980/json-string-cut-off-half-way-java-inputstream Did you manage to fix this? – Zy0n Mar 24 '15 at 21:02

2 Answers2

0

Try to set the response in textview and look, If string size is more and if logcat can't display then it trims the string.

Sanjay Hirani
  • 406
  • 2
  • 6
  • 18
  • but when trying to parse string it is incomplete. not having complete json string. length is -6055 only. – Sino Jan 05 '15 at 09:35
0

Try to do like as following

              /******/


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();
}
}
Saj
  • 849
  • 7
  • 12