简体   繁体   中英

Android connecting - WCF Service should return JSON

Iam connecting my Android app to a wcf service, and in my Log.i I can see that it return data correct, The only thing I want to handle it as JSON but my service sends as XML-(I think): this is how the code in the app looks like:

if (entity != null) 
{
    InputStream instream = entity.getContent();  
    BufferedReader reader = new BufferedReader(new InputStreamReader(instream));  
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null){
        sb.append(line + "n");  
    }      

    String result = sb.toString();  
    Log.i(TAG,result);  
    instream.close();  

    JSONObject json=new JSONObject(result);  

    JSONArray nameArray=json.names();  
    JSONArray valArray=json.toJSONArray(nameArray);  

and my example method looks like this, I don't know how to return correct JSON data from the WCF webserivce:

/// <returns>An enumeration of the (id, item) pairs. Returns null if no items are present</returns>
protected override IEnumerable<KeyValuePair<string, SampleItem>> OnGetItems()
{
    // TODO: Change the sample implementation here
    if (items.Count == 0)
    {
        items.Add("A", new SampleItem() { Value = "A" });
        items.Add("B", new SampleItem() { Value = "B" });
        items.Add("C", new SampleItem() { Value = "C" });
    }
    return this.items;
}

This is the error I get: 09-12 17:11:04.924: WARN/System.err(437): org.json.JSONException: Value <ItemInfoList of type java.lang.String cannot be converted to JSONObject

Add:

 [WebGet(ResponseFormat = WebMessageFormat.Json)]

As an attribute over your WCF service method. Change WebGet to WebInvoke if you are not using GET requests to call the service.

This looks like the code from WCF REST Starter Kit, the REST Collection template, so it should already support both XML and JSON. It's the service URI you specify on the client side that returns either XML or JSON representation. By default it sends XML but if you put "?format=json" at the and of the service URI it sends the resource in JSON format.

You can get helpful information by using built-in description of the service returned in ATOM (if I remember well) with /help after service URI something like: http://localhost/servicetest/Service.svc/help

This is how the wcf serivce method looks like: this returns a collection of values. I added the [WebGet(ResponseFormat = WebMessageFormat.Json)] but still it is not working.

[WebGet(ResponseFormat = WebMessageFormat.Json)]
    protected override IEnumerable<KeyValuePair<string, SampleItem>> OnGetItems()
    {
        // TODO: Change the sample implementation here
        if (items.Count == 0)
        {
            items.Add("A", new SampleItem() { Value = "A" });
            items.Add("B", new SampleItem() { Value = "B" });
            items.Add("C", new SampleItem() { Value = "C" });
        }
        return this.items;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM