views:

221

answers:

3

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

+1  A: 

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.

FlySwat
If Iam using post? should i Use WebInvoke
Troj
It is still not working,
Troj
It is still not working, I inserted webget on top of the method, and it didn't return json object,, I can still open it in a webbrowser and it shows as xml
Troj
You need to put it on the actual service method, your method does not look like the actual method defined in the WCF interface (Otherwise, why the override?)
FlySwat
A: 

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;
    }
Troj
A: 

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

OttO