简体   繁体   English

使用Web服务用服务器开发android应用程序

[英]developing android application with server using web service

I am developing client/server apps (the client is an Android application which sends data to the server). 我正在开发客户端/服务器应用程序(客户端是一个将数据发送到服务器的Android应用程序)。 I am developing the server using Java. 我正在使用Java开发服务器。 I already connected the sever application "using NetBeans" using SQL Navigator 5.5. 我已经使用SQL Navigator 5.5“使用NetBeans”连接了服务器应用程序。 I want to use Json as a middle ware, but I don't know how! 我想用Json作为中间件,但我不知道怎么做! What is the most suitable, XML or Json? 什么是最合适的XML或Json? Do i need to use HTTP? 我需要使用HTTP吗? If so, how (as I want to be able to secure the application)? 如果是这样,如何(因为我希望能够保护应用程序)?

The other thing that the server should respond to is the Android application by sending "longitude and latitude", for which Android should "geocode" and display on the form of map "location." 服务器应该响应的另一件事是Android应用程序通过发送“经度和纬度”,Android应该“地理编码”并以地图“位置”的形式显示。 Also, I need to understand more about the concept of web service that should work on the application. 此外,我需要了解更多关于应该在应用程序上工作的Web服务的概念。

This is the server code: (The values that the server should get from the client are "long and lat") /* * To change this template, choose Tools | 这是服务器代码:(服务器应从客户端获取的值为“long and lat”)/ * *要更改此模板,请选择“工具”|“工具”。 Templates * and open the template in the editor. 模板*并在编辑器中打开模板。 */ * /

package pkg;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;


@WebService(serviceName = "lbsws")
public class lbsws {

    /** This is a sample web service operation */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String longg) {


        String result=null;
        try
        {



        Connection con = dbConnection.getdbConnection("system","lbs","orcll", "localhost");
        Statement st = con.createStatement(); 


        String lat ="10";

        String query="select longg,lat,abs(("+longg+"-longg))/abs(("+lat+"-lat)) as function1 from lbs where abs(("+longg+"-longg))/abs(("+lat+"-lat)) = ("+
        "select min(abs(("+longg+"-longg))/abs(("+lat+"-lat))) from lbs)";

        ResultSet rs = st.executeQuery(query);
        while(rs.next())
        {
            result = rs.getFloat("longg")+","+ rs.getFloat("lat")+"-"+ rs.getFloat("function1");
        }
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
        return result;
    }

}

A few things: 一些东西:

1) JSON Thumbs up for the transport layer. 1)JSON为传输层竖起大拇指。 Don't give it a second thought. 不要再想一想。

2) Consuming JSON Web Services. 2)使用JSON Web服务。 Use HttpClient in Java based systems. 在基于Java的系统中使用HttpClient。 Just to get the response back than you can convert JSON to objects as needed. 只是为了获得响应,您可以根据需要将JSON转换为对象。 But calling web service is via HttpClient 但是通过HttpClient调用Web服务

3) Creating JSON Services. 3)创建JSON服务。 Well you could use Jersey. 那么你可以使用泽西岛。 There are a few choices. 有几个选择。 I would recommend developing in Tomcat server. 我建议在Tomcat服务器上进行开发。 Its faster and not so much up/down as with Java EE servers. 它比Java EE服务器更快,而不是上升/下降。 There are some other good choices beside Jersey. 泽西岛旁边还有其他一些不错的选择。

But mainly write the simplest thing first and get it working from end-to-end. 但主要是先写最简单的东西,然后让它从端到端工作。 Thats what you want to do. 多数民众赞成你想做什么。

Just produce a service that returns the Sytem time and send that back via JSON consume it on the client and display. 只需生成一个返回Sytem时间的服务,然后通过JSON将其发送回客户端并显示它。 Then re-evaluate Jersey vs. whatever. 然后重新评估泽西对阵什么。 But get something running end to end. 但得到一些端到端的东西。

So step one is write a JSON Web Service and just test it in the browser. 因此,第一步是编写JSON Web服务,并在浏览器中进行测试。 Don't worry about client side right now. 现在不要担心客户端。 Just get the service running and invoke it using the browser. 只需运行服务并使用浏览器调用它。 BTW, there are reason to go with RESTful JSON web service. 顺便说一句,有理由使用RESTful JSON Web服务。 Its a good way to structure your web services. 它是构建Web服务的好方法。

Never attack two problems at the same time. 永远不要同时攻击两个问题。 Forget about connecting to a database until you have got the service up and running with just data that is stubbed out. 忘记连接到数据库,直到您获得服务并运行只有被删除的数据。

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

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