devman
devman

Reputation: 1549

Android JSON Unterminated string at character

I'm getting a JSON string from a WCF service but I'm getting issues sometimes and sometimes it works fine. It's weird because the results that are getting returned should be exactly the same but sometimes it works fine, and other times I need to hit refresh in my application a couple times and it works fine. So i'm not sure if there is an inconstancy with something in my JSON string or if anyone has any ideas. Here is the JSON string that is being returned

[{"AssmtStatus":1,"AssmtStatusText":null,"CreatedBy":"","IntervalDescr":null,"Status":0,"WoundLocation":null,"dtAssmtDate":"1\/1\/1900","dtLastCompleted":"3\/30\/2012 3:19:00 PM","iAssessmentID":1,"iAssessmentType":2,"sAssmtName":"Weekly Skin"},{"AssmtStatus":1,"AssmtStatusText":null,"CreatedBy":"","IntervalDescr":null,"Status":0,"WoundLocation":null,"dtAssmtDate":"1\/1\/1900","dtLastCompleted":"1\/1\/1900","iAssessmentID":1,"iAssessmentType":4,"sAssmtName":"Admission Skin"},{"AssmtStatus":1,"AssmtStatusText":null,"CreatedBy":"","IntervalDescr":null,"Status":0,"WoundLocation":null,"dtAssmtDate":"1\/1\/1900","dtLastCompleted":"1\/1\/1900","iAssessmentID":1,"iAssessmentType":5,"sAssmtName":"PHQ - 9 - Resident"},{"AssmtStatus":1,"AssmtStatusText":null,"CreatedBy":"","IntervalDescr":null,"Status":0,"WoundLocation":null,"dtAssmtDate":"1\/1\/1900","dtLastCompleted":"1\/1\/1900","iAssessmentID":1,"iAssessmentType":6,"sAssmtName":"PHQ - 9 - Staff"},{"AssmtStatus":1,"AssmtStatusText":null,"CreatedBy":"","IntervalDescr":null,"Status":0,"WoundLocation":null,"dtAssmtDate":"1\/1\/1900","dtLastCompleted":"1\/1\/1900","iAssessmentID":1,"iAssessmentType":7,"sAssmtName":"Brief Interview for Metal Status (BIMS)"},{"AssmtStatus":1,"AssmtStatusText":null,"CreatedBy":"","IntervalDescr":null,"Status":0,"WoundLocation":null,"dtAssmtDate":"1\/1\/1900","dtLastCompleted":"1\/1\/1900","iAssessmentID":1,"iAssessmentType":8,"sAssmtName":"Staff Assessment for Mental Status"},{"AssmtStatus":1,"AssmtStatusText":null,"CreatedBy":"","IntervalDescr":null,"Status":0,"WoundLocation":null,"dtAssmtDate":"1\/1\/1900","dtLastCompleted":"1\/1\/1900","iAssessmentID":1,"iAssessmentType":1001,"sAssmtName":"Open Note"},{"AssmtStatus":1,"AssmtStatusText":null,"CreatedBy":"","IntervalDescr":null,"Status":0,"WoundLocation":null,"dtAssmtDate":"1\/1\/1900","dtLastCompleted":"1\/1\/1900","iAssessmentID":1,"iAssessmentType":1002,"sAssmtName":"Labs Test"},{"AssmtStatus":1,"AssmtStatusText":null,"CreatedBy":"","IntervalDescr":null,"Status":0,"WoundLocation":null,"dtAssmtDate":"1\/1\/1900","dtLastCompleted":"1\/1\/1900","iAssessmentID":1,"iAssessmentType":1003,"sAssmtName":"Smoking Assessment"},{"AssmtStatus":1,"AssmtStatusText":null,"CreatedBy":"","IntervalDescr":null,"Status":0,"WoundLocation":null,"dtAssmtDate":"1\/1\/1900","dtLastCompleted":"5\/7\/2012 9:15:00 AM","iAssessmentID":1,"iAssessmentType":1004,"sAssmtName":"Inquiry Assessment"}]

the error i get is "Unterminated string at character 2431" if anyone has any ideas, i would really appreciate the help. thanks

EDIT: Here is the entire class I'm using to get the JSON.

private class GetAssmts extends AsyncTask<String,Void,Void>{
     private ProgressDialog Dialog = new ProgressDialog(AssmtSelectionUnScheduled.this);

     protected void onPreExecute(){
         Dialog.setMessage("Loading..");
         Dialog.show();
     }

    @Override
    protected Void doInBackground(String... params) {
        try {
            HttpGet request = new HttpGet(SERVICE_URL + "/GetAssmtsUnScheduled/" + FacID + "/" + ResID + "/" + UserID);
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-type", "application/json");

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(request);

            HttpEntity responseEntity = response.getEntity();

            // Read response data into buffer
            char[] buffer = new char[(int)responseEntity.getContentLength()];
            InputStream stream = responseEntity.getContent();
            InputStreamReader reader = new InputStreamReader(stream);
            reader.read(buffer);
            stream.close();

            JSONArray plates = new JSONArray(new String(buffer));
            MyAssmts.clear();
            for (int i = 0; i < plates.length(); ++i) {
                JSONObject jo = (JSONObject) plates.get(i);
                Assmt myassmt = new Assmt(Integer.parseInt(jo.getString("iAssessmentType")),Integer.parseInt(jo.getString("iAssessmentID")),jo.getString("sAssmtName"),jo.getString("dtLastCompleted"),
                        Integer.parseInt(jo.getString("Status")),jo.getString("WoundLocation"),jo.getString("IntervalDescr"),jo.getString("CreatedBy"),Integer.parseInt(jo.getString("AssmtStatus")),
                        jo.getString("AssmtStatusText"),jo.getString("dtAssmtDate"));
                MyAssmts.add(myassmt);
            }  
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Void unused){
        Dialog.dismiss();
            final GridView lv = (GridView) AssmtSelectionUnScheduled.this.findViewById(R.id.gridView_AssmtList);
            lv.setAdapter(new MyAdapter(AssmtSelectionUnScheduled.this, MyAssmts));
            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
                    Assmt sel = (Assmt) (lv.getItemAtPosition(arg2));   
                    boolean isNewAssmt = true;
                    if(sel.getsCreatedBy().length() > 0 && sel.getsCreatedBy().toUpperCase().equals(UserID.toUpperCase())){
                        isNewAssmt = false;
                    }
                    launchAssmt(sel.getiAssessmentType(), sel.getiAssessmentID(), isNewAssmt, sel.getsAssmtDate());
                }
            });
        }
}

Upvotes: 2

Views: 5374

Answers (2)

MiniGod
MiniGod

Reputation: 3802

I had the same error, but found out that reader.read(buffer) did not read everything. You could do a loop until you have everything, like so:

int contentLength = (int) responseEntity.getContentLength();
char[] buffer = new char[contentLength];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);

int hasRead = 0;
while (hasRead < contentLength)
    hasRead += reader.read(buffer, hasRead, contentLength-hasRead);

stream.close();

Upvotes: 8

dmon
dmon

Reputation: 30168

My guess would be that responseEntity.getContentLength() is not returning the right value, so your character buffer ends up being too small.

Upvotes: 2

Related Questions