Android Http Response is incomplete. Returns an unterminated json object
<p>I am using <code>HttpClient</code> 4.3.6 to perform http GET and POST requests. Right now I am using multipartentity to send a few string parameters and an image in the form of a file. I am able to successfully post the data but my problem comes in when I get the HTTP response. The response contains json data.</p>

<p>What happens is the HTTP response is incomplete and when i try to create a json object with the data i get jsonexception error saying:</p>

<blockquote>
  <p>Unterminated object at character 407. </p>
</blockquote>

<p>I noticed that the response does not contain closed braces. Is this a problem on android or should I check the server? Because I am able to see the data properly on postman and on ios. I have never faced this issue before and don't know how to solve this.</p>

<p>This is my code to post and get the response:</p>

<pre><code>@Override
protected String doInBackground(String... params) {

    try {
        String url = params[0];
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        MultipartEntity entity = new MultipartEntity();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        ByteArrayBody bab = new ByteArrayBody(imageBytes, "image.jpg");
        entity.addPart("image_data", bab);
        entity.addPart("action", new StringBody("1", "text/plain", Charset.forName("UTF-8")));
        entity.addPart("name", new StringBody("asdfg", "text/plain", Charset.forName("UTF-8")));
        entity.addPart("user_id", new StringBody("157", "text/plain", Charset.forName("UTF-8")));
        entity.addPart("birthday", new StringBody("18-04-1995", "text/plain", Charset.forName("UTF-8")));
        entity.addPart("gender", new StringBody("male", "text/plain", Charset.forName("UTF-8")));
        entity.addPart("is_jlpt_student", new StringBody(String.valueOf(0), "text/plain", Charset.forName("UTF-8")));
        entity.addPart("relationship", new StringBody("Father", "text/plain", Charset.forName("UTF-8")));
        entity.addPart("relationship_id", new StringBody(String.valueOf(10002), "text/plain", Charset.forName("UTF-8")));
        entity.addPart("is_creator", new StringBody(String.valueOf(1), "text/plain", Charset.forName("UTF-8")));
        entity.addPart("email", new StringBody(email, "text/plain", Charset.forName("UTF-8")));

        httppost.setEntity(entity);
        HttpResponse resp = httpclient.execute(httppost);
        String response = EntityUtils.toString(resp.getEntity());
        Log.i("HttpResponse", response);

        return response;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

}

@Override
protected void onPostExecute (String result) {
    super.onPostExecute(result);

    JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject(result);
        JSONObject json_data = jsonObject.getJSONObject("data");
        String json_userid = json_data.getString("user_id");
        String json_username = json_data.getString("name");
        String json_email = json_data.getString("email");
        String json_country = json_data.getString("country_code");
        String json_imagefilename = json_data.getString("image_filename");
        String json_imgurl = json_data.getString("image_url");

        Toast.makeText(ParentGuardianProfile.this, "ImageFile " + json_imagefilename, Toast.LENGTH_SHORT).show();


       User new_user = userdao.createUser(json_userid, json_username, json_email,json_imagefilename,json_country,selectedImageUri.toString(), 1);


       Log.i("SQLITE", "added user : " + new_user.getmUserName() + new_user.getmId());


    } catch (JSONException e) {
        e.printStackTrace();
    }
}                             
</code></pre>

<p>And my json response is :</p>

<pre><code>{"status":1,"message":"success","data":{"child_id":"381","name":"asdfg","image_filename":"C201603021734476.jpg","image_url":"https:\/\/innokid.blob.core.windows.net\/media\/child\/381.jpg","birthday":"18-04-1995","gender":"male","is_jltp_student":"0","relationship":"Father","relationship_id":"10002","is_creator":1,"rank":1,"qrcode_url":"http:\/\/innokid.azurewebsites.net\/uploads\/qrcode\/child_381.png"
</code></pre>

<p>I tried using String buffer as suggested in this post <a href="https://stackoverflow.com/questions/3580062/string-is-being-truncated-when-its-too-long">String is being truncated when its too long</a> . But i still get the same result. </p>