Programming for android: Empty exception when converting inputstream to JSONArrary on newest questions tagged android – Stack Overflow

I’ve an Android app that tries to consume a WCF service that returns a list of objects. The stream reads correctly, but throws an empty JSONException when trying to convert the string to a JSONArray. I checked the JSON response on a JSON validator and this seams OK.

My WCF Servicecontract:

   [ServiceContract]
   public interface IAndroidWebService
    {
        [OperationContract]
        [WebGet(UriTemplate = "AndroidGetTenants",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
        List<TenantEntity> AndroidGetTenants();
    }

The JSON response it generates when it is called:

[{"MaxVotesPerSecond":0,"Name":"Duurzaam OV","PartitionKey":"a63569b9-e794-4674-832b-46e85de0dc0a","RowKey":"02f1654c-9746-4da5-a146-bff7fe611468"},
    {"MaxVotesPerSecond":0,"Name":"Overheid","PartitionKey":"e38f3d93-5b4d-41e9-8513-3fd350a019af","RowKey":"11ec2c3e-e65f-45cf-9c89-4aa40a2c21c7"}]

The java code used in my app:

HttpGet request = new HttpGet("http://XX.XX/WebService.svc/Android/AndroidGetTenants");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");  

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity responseEntity = response.getEntity();         

byte[] buffer = new byte[(int)responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
stream.read(buffer);
stream.close();
String tekst = new String(buffer,"UTF-8");
JSONArray tenants = new JSONArray(tekst);

The java code throws an empty JSONException (parameter e in the catch doesn’t exists). It seems that the service returns an array of objects.
When I change:

JSONArray tenants = new JSONArray(tekst);

with

JSONObject tenants = new JSONObject(tekst);

The code throws an JSONException: Value [{"RowKey":"02f1654c-9746-4da5 ...... esPerSecond":0}] of type org.json.JSONArray cannot be converted to JSONObject

Which indicates to use the JSONArray, but that againg throws the empty exception mentioned above.

Hope someone can help.

See Answers


source: http://stackoverflow.com/questions/11509195/empty-exception-when-converting-inputstream-to-jsonarrary
Programming for android: programming-for-android



online applications demo