使用 json 将 Android 连接到 ASP.NET MVC
我有一个 asp.net MVC 2.0 应用程序。如果我想走那条路,我还有 WFC 服务。但我并不是特别喜欢用Java解析xml。 (我已经大约 7 年没有使用 Java 工作了)。这是我第一次使用 eclipse,所以请像我这样的 android/java 菜鸟向我解释一下。
经过一番研究后,我想我想使用我的 MVC 站点作为一个宁静的服务,在 android 和服务器之间来回传递 json。我知道如何将 Json 传递出控制器:
public ActionResult SomeActionMethod()
{
return Json(new {foo="bar", hello="World"});
}
如何连接到服务,然后将 json 转换为 Android 应用程序中的对象?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在高层使用 HttpClient 从中获取数据RESTful 服务并使用 JSONObject 解析响应。当您整理一些代码并且遇到困难时,我们可以为您提供更多帮助
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
为了翻译 JSON 对象,我个人使用 jackson 解析器。在此处获取 .jar 库:http://jackson.codehaus。 org/1.7.3/jackson-all-1.7.3.jar 。
这是我如何使用它的一个示例(有很多不同的方法可能会让您更满意。有关更多信息,请参阅 jackson 网站):
至于 http 客户端,您可以这样做
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):
As for the http client you can do this
Android 在其 SDK 中内置了 JSON 解析器 - 包是 org.json
除此之外,您还可以使用像 Gson 这样的库,它将 json 解析包装在简单易用的 API 中。以下是关于 WCF 示例的简短教程: Gson示例/教程
希望上述信息对您有用。
除此之外,我想谈谈我对您继续使用 Android 本机应用程序和 WCF Json 服务的决定的拙见。如果您发现处理 Android 开发有点麻烦 - 如果您熟悉 .NET MVC 和 ASP.NET,那么您应该考虑像 Sencha Touch 这样的框架 - Snecha Touch URL
Sencha Touch 是可以通过 ASPX 页面生成的 javascript 框架 - 该框架生成的外观和感觉类似于 Android 和 iPhone 的本机外观。您可以在您的设备上尝试这些示例。这将使您节省学习新语言和 API sdk 的精力 - 通过原生支持 JSON 的 JavaScript 框架灵活地使用 RESTful 服务。
这是我的观点 - 我不了解背景,您在选择平台时最好做出判断。
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.