-1

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!

3 Answers3

1

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();
}
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • I made the changes . Now i happen to see this error `D/skia: --- SkImageDecoder::Factory returned null` while saving the bitmap and let me mention the bitmap (As string) is truncated. Its the full string of bitmap – Lakhwinder Singh Dhillon Jun 04 '16 at 07:28
1

Simply do like,

JSONObject jsonObject = new JSONObject(jsonString);

return End of input at character 0 because the string result is empty maybe you can try to replace it with some jsonString.

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0

Made changes as proposed my @Paresh

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

and then i further received error

D/skia: --- SkImageDecoder::Factory returned null

which was solved by replacing this

public Bitmap StringToBitMap(String getContent) {
        try {
            byte[] encodeByte = getContent.getBytes(Charset.forName("UTF-8"));
            Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,encodeByte.length);
            return bitmap;
        }
        catch (Exception e)
        {
            e.getMessage();
            return null;
        }

    }

with this

public Bitmap StringToBitMap(String encodedString){
        try {
            byte [] encodeByte=Base64.decode(encodedString, Base64.DEFAULT);
            Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
            return bitmap;
        } catch(Exception e) {
            e.getMessage();
            return null;
        }
    }

I hope it helps someone else

please Vote up if it helped :)