通过JSON连接Android应用到Java服务器 [英] Connecting Android app to Java server via JSON
问题描述
我创建Android应用程序存储数据,如在MSSQL数据库名称,纬度,经度。
I am creating an android app which stores data such as "name" "latitude" "longitude" in a MSSQL database.
我计划在Android应用程序连接,然后连接到MSSQL数据库服务器的服务器。
I am planning to connect the android app to a server which then connects to a mssql database server.
我已经建立了Java - > MSSQL连接罚款,可以没有任何问题的读/写数据
I have established the Java -> MSSQL connection fine and can read/write data with no problems.
现在我希望能够在Java服务器和Android应用程序之间发送/接收这些对象。
Now I want to be able to send / receive these objects between the java server and the android app.
我创建'file.json,并将其存储包含在Web服务器上:
I created 'file.json' and stored it on a web server containing:
{"name":"shopname","address":"123 fake street","type":"unknown", "notes":"service is really good."}
和我能够把这个数据回与不麻烦的对象。
and I am able to pull this data back into an object with no trouble.
我的问题是:
我如何获得Java服务器将这些数据有助于我的Android应用?我希望能够使用:
How do I get the java server to serve this data to my android app? I want to be able to use:
URL objectGet = new URL("http://10.0.1.8/file.json");
URLConnection yc = objectGet.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
然后
DataObject objs = gson.fromJson(in, DataObject.class);
什么是设置此功能的最佳方式?
What would be the best way of setting this up?
推荐答案
要测试您的应用程序,你可以承载你的开发机器上通过IIS或Apache服务,然后通过
To test your app you can host the service via IIS or Apache on your development machine and then hit your server via
http://10.0.2.2/file.json
10.0.2.2是特殊别名主机回环接口(即127.0.0.1在开发机器上)
10.0.2.2 is the special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)
请参阅:模拟器网络地址空间在官方文档
这篇关于通过JSON连接Android应用到Java服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!