My system based on an android client and a wcf – rest – web service.
I’m using this tutorial: http://fszlin.dymetis.com/post/2010/05/10/Comsuming-WCF-Services-With-Android.aspx
I’m having a weird problem. I have a valid JSON string (checked it with online tools), the reading to the buffer goes well but when I’m trying to create a JSON array it throws JSONException without an exception variable (the variable is NULL – never happend to me before)
The line that throws the excption:
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));
the last line throws the exception.
The text from the service is this:
[{“dtFromDate”:”1899-12-30 20:00:00″,”fBuyWindow”:120,”fNewPrice”:150,”fOldPrice”:400,”lLatitude”:32.021327,”lLongitude”:34.776452,”nDestinatedPlatform”:1,”nMessageID”:1,”nRange”:5,”nStickingHours”:48,”strDetailedDescription”:”נעלי נייק מסוג 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,”nDestinatedPlatform”:1,”nMessageID”:2,”nRange”:5,”nStickingHours”:48,”strDetailedDescription”:”נעלי נייק מסוג 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”:””,”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”:4,”nRange”:5,”nStickingHours”:48,”strDetailedDescription”:”נעלי נייק מסוג 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”:5,”nRange”:5,”nStickingHours”:48,”strDetailedDescription”:”נעלי נייק מסוג N95, הנעליים של לאונל מסי, בהנחה מטורפת.”,”strDisplayAddress”:””,”strFullAddress”:”חנקין 49, חולון, ישראל”,”strImagePath”:””,”strItemDescrpition”:”נעלי נייק דגם N95 לבן”,”strThumbPath”:””,”strTitle”:”נייק קניון חולון”}]
Any suggestions?
Thanks.
Try the following.
Change this line…
…to use a byte array as follows…
You’ll have to forget about using
InputStreamReaderas it only works withchar[]. Instead read the stream using…Then change this line…
…to this to create a UTF-8 encoded String…