Android JSON字符处未终止的字符串

时间:2012-05-14 15:37:31

标签: java android json

我从WCF服务获取JSON字符串,但有时候我遇到问题,有时它工作正常。这很奇怪,因为返回的结果应该完全相同,但有时它工作正常,有时我需要在我的应用程序中刷新几次,它工作正常。所以我不确定我的JSON字符串中是否存在某种不确定性,或者是否有任何想法。这是要返回的JSON字符串

[{"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"}]

我得到的错误是“2431字符未被终止的字符串”,如果有人有任何想法,我真的很感激帮助。感谢

编辑:这是我用来获取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());
                }
            });
        }
}

2 个答案:

答案 0 :(得分:7)

我有同样的错误,但发现reader.read(buffer)没有阅读所有内容。 你可以做一个循环,直到你拥有所有东西,如:

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

答案 1 :(得分:2)

我的猜测是responseEntity.getContentLength()没有返回正确的值,所以你的字符缓冲区最终会变得太小。