Lakhwinder Singh Dhillon Lakhwinder Singh Dhillon - 6 months ago 8
JSON Question

org.json.JSON Exception : End of input at character 0 at

Error received is "org.json.JSON Exception : End of input at character 0 at"

MY GET JSON Parser

// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();


//IS is already truncated here. why?

is = httpEntity.getContent();
String jsonString=convertStreamToString(is);
try{
response = new JSONObject((jsonString.substring(jsonString.indexOf("{"), jsonString.lastIndexOf("}")+1)));
}catch (Exception e){e.printStackTrace();}


I receive json (which is Correct, Checked here http://jsonlint.com/

Php gives following output (in browser) so it means php works fine

{"profile":[{"image":"iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABHNCSVQICAgIfAhkiAAAIABJREFU\neJzcvWuMZdl13\/e71ftWr9NzbnOfYRV5j9RN1mVmCNaYDKfbppFuhBE1BBNoAskhmcSxCU8cKYAB\nOxYcGwnyJbDz+pAPVvxIPkq2DMgOYFiyZXgIROCQAp0e2GN3j0Bm7sg90W17WjpXmjLPnumjPqu7\nd1flw9r7nFM9MxQp8SHnzDSq6j7OY++11+O\/\/mvtGf8\/PP78j\/8HHz95sOOLM7NPAcxi\/6mgPb+x\nDYgXts3247OHVFIWaNfThsDJCe3h4dO\/QoTN7VcpRJjBV+5GmM35iszPhy\/\/03\/xK9\/nR\/uOH7Pv\n9w38Xo8\/\/Afe9\/EHcfZDZ+c8vd3+5qcKkZX3nkoKVqsl9V5Nc9SwubkBKWhDS1FWiECzbQCh3qtR\nVTa3NogIvvRswxZfCiJCVVa0ETT2HK4ONk2z\/YqXsy\/\/2f\/qz\/zyM\/\/ZT\/4bLRT\/xgnAYS0fPJ7v\n\/pHiXPEp3OxTs0ilndKqUohABBEgKgLUezV17ekVtneCTbL31L6iVygctKEFJxCVEALeeypf......T0AAAAASUVORK5CYII=\n"}],"success":1,"message":"profile pic found"}



(..... means i deleted some part of it for showing here)


Which is correct.
But when it goes through everything 'is'=>(InputStream) get truncated to

{"profile":[{"image":"iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAABHNCSVQICAgIfAhkiAAAIABJREFU\neJzcvWuMZdl13\/e71ftWr9NzbnOfYRV5j9RN1mVmCNaYDKfbppFuhBE1BBNoAskhmcSxCU8cKYAB\nOxYcGwnyJbDz+pAPVvxIPkq2DMgOYFiyZXgIROCQAp0e2GN3j0Bm7sg90W17WjpXmjLPnumjPqu7\nd1flw9r7nFM9MxQp8SHnzDSq6j7OY++11+O\/\/mvtGf8\/PP78j\/8HHz95sOOLM7NPAcxi\/6mgPb+x\nDYgXts3247OHVFIWaNfThsDJCe3h4dO\/QoTN7VcpRJjBV+5GmM35iszPhy\/\/03\/xK9\/nR\/uOH7Pv\n9w38Xo8\/\/Afe9\/EHcfZDZ+c8vd3+5qcKkZX3nkoKVqsl9V5Nc9SwubkBKWhDS1FWiECzbQCh3qtR\nVTa3NogIvvRs...EHoF0



(..... again it means i deleted some part of it for showing here)
but it ends just there with EHoF0 (somewhere in between real full string of image


Receiving on the android side with

responseFromServer = JSONParser.makeHttpRequestReturnJson(url, "GET",param);


Checked various other question answers here but none works. I even tried POST

Please help!

Answer

I think you should learn JSON Parsing in Android exactly. I would suggest you to go for any video tutorial!

To resolve issue you are facing, update your code with:

String jsonString=convertStreamToString(is);
try{
    response = new JSONObject(jsonString)
}catch (Exception e) {
    e.printStackTrace();
}

From

String jsonString=convertStreamToString(is);
try{
     response = new JSONObject((jsonString.substring(jsonString.indexOf("{"), jsonString.lastIndexOf("}")+1)));
}catch (Exception e) {
      e.printStackTrace();
}