2

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

2 回答 2

8

我有同样的错误,但发现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();
于 2012-06-08T15:49:01.520 回答
2

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

于 2012-05-14T17:24:12.753 回答