简体   繁体   English

在Android中获取PHP JSON数据

[英]Get PHP JSON Data in Android

I do alot of development in PHP. 我在PHP中做了很多开发。 And I have recently started working with JSON data for various APIs for my websites to interact with each other. 我最近开始使用各种AP​​I的JSON数据,以便我的网站互相交流。 I would like to extend this a little further and create a Android app with my web applications. 我想进一步扩展这一点,并使用我的Web应用程序创建一个Android应用程序。

Please keep in mind I am fairly new at Android and have little experience. 请记住,我在Android上相当新,没什么经验。 I have worked with JSON data in JavaScript, and it is fairly easy. 我在JavaScript中使用过JSON数据,这很容易。 However that is using jquerys easy to use Ajax .post. 然而,这是使用jquerys易于使用Ajax .post。

So my question is, what is the easiest, most simple way to get JSON data from a website and turn it into a working variable in Android? 所以我的问题是,从网站获取JSON数据并将其转换为Android中的工作变量的最简单,最简单的方法是什么?

I would prefer something with no error checking or anything. 我更喜欢没有错误检查或任何事情的东西。 Just the basics. 只是基础知识。

Thanks! 谢谢!

If you're programming object oriented, there is not any easier way then to do it with JSON, so this is a good choice. 如果您正在编程面向对象,那么使用JSON没有任何更简单的方法,所以这是一个不错的选择。

You can use Google's Gson to parse the Json strings your PHP back-end sends to your Android app. 您可以使用Google的Gson来解析您的PHP后端发送到您的Android应用程序的Json字符串。

As long as you use Objects, you can parse the string into an object with 1 line of code. 只要使用Objects,就可以使用1行代码将字符串解析为对象。 Just be sure to type all fields correctly on your PHP side. 只需确保在PHP端正确键入所有字段。

Take a look at this tutorial , I used this a while back and it makes it pretty clear :) 看看这个教程 ,我暂时使用了它,它很清楚:)

As @alexander7567 mentioned, GSON is also very good to use. 正如@ alexander7567所提到的,GSON也非常好用。 But may be a bit more complex for you to pick up at first! 但是你最初可能会有点复杂!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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