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

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

最佳答案

我有同样的错误,但发现 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();

关于java - Android JSON 字符处未终止的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10586523/

相关文章:

javascript - 为什么在通过 AJAX 发送数据时,您必须对对象进行 JSON.stringify()?

javascript - 如何检查 undefined variable javascript?

java - Vaadin 动画师淡入刷新后消失

java - Hibernate:跨多个表的@UniqueConstraint?

android - 我怎样才能到达 getResources() 和 Context 除了 Activity 类?

android - JetPack Compose 中的副作用

json - 在 Erlang 中解析 JSON

java - 将数据插入DB ERROR: 'VARCHAR'类型的列不能保存 'INTEGER'类型的值

java.sql.SQLException :[SQLITE_ERROR] SQL error or missing database (near "=":syntax error) 异常

java - Smack XMPP android 连接时崩溃