2

我的系统基于一个 android 客户端和一个 wcf - rest - web 服务。我正在使用本教程: http: //fszlin.dymetis.com/post/2010/05/10/Comsuming-WCF-Services-With-Android.aspx 我遇到了一个奇怪的问题。我有一个有效的 JSON 字符串(使用在线工具检查过),对缓冲区的读取很顺利,但是当我尝试创建一个 JSON 数组时,它会抛出 JSONException 而没有异常变量(变量为 NULL - 我以前从未发生过) 抛出异常的行:

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

最后一行抛出异常。

来自服务的文本是这样的: [{"dtFromDate":"1899-12-30 20:00:00","fBuyWindow":120,"fNewPrice":150,"fOldPrice":400,"lLatitude":32.021327 ,“Llongitude”:34.776452,“ndestineplatform”:1,“nmessageid”:1,“nrange”:5,“nstickinghours”:48,“strdetaileddescription”:“strילייתמסוגn95,הנעלייםםשלמסי,בהנחהמארפת。” ,"strDisplayAddress":"חנקין 49, חולון","strFullAddress":"חנקין 49, חולון, ישראל","strImagePath":"http://images.free-extras.com/pics/n/nike_swoosh-703。 jpg","strItemDescrpition":"נעלי נייק דגם N95 לבן","strThumbPath":"http://images.free-extras.com/pics/n/nike_swoosh-703.jpg","strTitle":"נייק קניון חולון"},{"dtFromDate":"1899-12-30 20:00:00","fBuyWindow":120,"fNewPrice":150,"fOldPrice":400,"lLatitude":32.021327,"lLongitude “:34.776452,”ndestindplatform“:1,”nmessageid“:2,”nrange“:5,”nstickinghours“:48,”strdetaileddescription“:”strילייתמסוגn95,הנעלייםשלשלמסי,בהנחהמארפת。“,”strdisplayaddress ":"","strFullAddress":"חנקין 49, חולון, ישראל","strImagePath":"","strItemDescrpition":"נעלי נייק דגם N95 לבן","strThumbPath":"","strTitle":"נייק קניון חולון"},{"dtFromDate":"1899-12-30 20:00:00","fBuyWindow":120,"fNewPrice":150,"fOldPrice":400,"lLatitude":32.021327,"lLongitude":34.776452,"nDestinatedPlatform":1,"nMessageID":3,"nRange":5,"nStickingHours":48,"strDetailedDescription":"נעלי נייק מסוג N95 ,אייתשללאונלמסי,בהנחהמטורפת。“strdisplayaddress”:“”,“strullladdress”:“חנקין49,חולון,יורה”,“strimagepath”:“”,“stritemdescrpition”:“נעלינייקקדגםn95לבן”, "strThumbPath":"","strTitle":"נייק קניון חולון"},{"dtFromDate":"1899-12-30 20:00:00","fBuyWindow":120,"fNewPrice":150,"fOldPrice ":400,"lLatitude":32.021327,"lLongitude":34.776452,"nDestinatedPlatform":1,"nMessageID":4,"nrange“:5,”nstickinghours“:48,”strdetaileddescription“:”נעלילייתמסוגn95,הנעלייםשלשלמסי,בהנחהמארפת。“,”strdisplayaddress“:”strdisplayaddress“:”“,”strufulladdress“:”strין49,חולון,יוראל“ ,"strImagePath":"","strItemDescrpition":"נעלי נייק דגם N95 לבן","strThumbPath":"","strTitle":"נייק קניון חולון"},{"dtFromDate":"1899-12-30 20 :00:00","fBuyWindow":120,"fNewPrice":150,"fOldPrice":400,"lLatitude":32.021327,"lLongitude":34.776452,"nDestinatedPlatform":1,"nMessageID":5,"nRange ":5,"nStickingHours":48,"strDetailedDescription":"נעלי נייק מסוג N95,הנעליים של לאונל מסי,בהנחה מטורפת。","strDisplayAddress":"","strFullAddress":"חנקין 49, חולון, ישראל","strImagePath":"","strItemDescrpition":"נעלי נייק דגם N95 לבן","strThumbPath":"","strTitle": "נייק קניון חולון"}]

有什么建议么?

谢谢。

4

2 回答 2

0

我会建议这样的事情:

String newString = reader.read(buffer); //assuming you only get one line in response, rather than many (add a loop if so)
JSONArray plates = new JSONArray(newString);

这可能会有所帮助。否则,这是我使用 JSONArray 完成的一些与 JSON 相关的编码:

public JSONArray getQuestionsJSONFromUrl(String url, List<NameValuePair> params) {  
    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        String jsonData = reader.readLine();
        JSONArray jarr = new JSONArray(jsonData);
        is.close();
        return jarr;
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    return null;
}
于 2012-05-08T00:54:14.153 回答
0

试试下面的。

改变这一行...

char[] buffer = new char[(int)responseEntity.getContentLength()];

...使用字节数组如下...

byte[] buffer = new byte[(int)responseEntity.getContentLength()];

您将不得不忘记使用InputStreamReader,因为它仅适用于char[]. 而是使用...读取流

stream.read(buffer);

然后改变这一行...

JSONArray plates = new JSONArray(new String(buffer));

...为此创建一个 UTF-8 编码的字符串...

JSONArray plates = new JSONArray(new String(buffer, "UTF-8"));
于 2012-05-08T23:19:55.343 回答