Josh
Josh

Reputation: 16532

Connecting Android to ASP.NET MVC with json

I have an asp.net MVC 2.0 application. I also have a WFC service, if I want to go that route. but I am not particularly fond of parsing xml in Java. (I haven't worked in Java in about 7 years). This is my first time using eclipse, so please explain things to me like the android/java noob that I am.

After some research I think I would like to use my MVC site as a restful service to pass json back and forth between the android and the server. I know how to pass Json out of the controller:

public ActionResult SomeActionMethod() 
{
    return Json(new {foo="bar", hello="World"});
}

How do I go about connecting to the service and then translating the json into objects within my android application?

Upvotes: 2

Views: 3031

Answers (3)

Rutesh Makhijani
Rutesh Makhijani

Reputation: 17235

Android has JSON parsers build in its SDK - the package is org.json

Beside this you can use libraries like Gson that wraps the json parsing in simple to use API. Here is a brief tutorial on the same along with WCF example: Gson example/tutorial

Hope the above information would be useful in your context.

Beside this I would like to mention my humble opinion on your decision to go ahead with Android native application and WCF Json services. If you find dealing with Android development a bit cumbersome - and if you are comfortable with .NET MVC and ASP.NET, then you should look at frameworks like Sencha Touch - Snecha Touch URL

Sencha Touch is javascript framework that can be generated via your ASPX pages - the framework generates look and feel that is similar to native look of Android and iPhone. You can try out the examples on your device. This would allow you to save effort of learning new language and API sdk - with the flexibility of consuming RESTful services via javascript framework where JSON support is native.

This is in my opinion - I am not aware of the context, you would be best judge in selecting the platform.

Upvotes: 1

user614538
user614538

Reputation: 106

For translating the JSON objects I personally use jackson parser. Get the .jar lib here: http://jackson.codehaus.org/1.7.3/jackson-all-1.7.3.jar .

Here is an example on how I use it (there are a lot of different ways that might please you more. See the jackson website for more info):

        JsonNode userData = null;
        ObjectMapper mapper = new ObjectMapper();
        try {
            userData = mapper.readValue(res.getEntity().getContent(), JsonNode.class);
            String name = (String)userData.get("name").getTextValue();
        } catch (Exception e) {
            alert(R.string.errorGeneral);
            e.printStackTrace();
            return;
        }

As for the http client you can do this

HttpResponse res;
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try{
            HttpGet get = new HttpGet(URI.create("http://yourlink"));
            res = httpclient.execute(get);
        }catch(Exception e){
            e.printStackTrace();
        }

Upvotes: 1

Robby Pond
Robby Pond

Reputation: 73494

At the high-leve use HttpClient to get data from the RESTful Service and use JSONObject to parse the response. When you get some code together and if you get stuck we can help you some more

Upvotes: 2

Related Questions