0

I am developing client/server apps (the client is an Android application which sends data to the server). I am developing the server using Java. I already connected the sever application "using NetBeans" using SQL Navigator 5.5. I want to use Json as a middle ware, but I don't know how! What is the most suitable, XML or Json? Do i need to use 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." Also, I need to understand more about the concept of web service that should work on the application.

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 | 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;
    }

}
NioShobu
  • 775
  • 2
  • 10
  • 21
  • The main issue is really creating the restful service at this point. It looks like you are using annotations to create the service. What types of errors are you seeing. – Code Droid Jul 17 '12 at 22:21
  • i have got the service running with no error,,, i got the result right. but what am asking about is the next step to get the service running at the client...am new at those concepts...i didn't find useful tutorials ...so if there any links to help me – user1531389 Jul 17 '12 at 22:55

1 Answers1

2

A few things:

1) JSON Thumbs up for the transport layer. Don't give it a second thought.

2) Consuming JSON Web Services. Use HttpClient in Java based systems. Just to get the response back than you can convert JSON to objects as needed. But calling web service is via HttpClient

3) Creating JSON Services. Well you could use Jersey. There are a few choices. I would recommend developing in Tomcat server. Its faster and not so much up/down as with Java EE servers. 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. 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. 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. Its a good way to structure your web services.

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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Code Droid
  • 10,344
  • 17
  • 72
  • 112
  • Thanks for advising...^_^ "Use HttpClient in Java based systems" am asking about the approach! "Tomcat server"?? am using NetBeans.... so what should i DO? – user1531389 Jul 17 '12 at 22:57
  • "So step one is write a JSON Web Service and just test it in the browser""???? HOW? what should i do in this code?...if there is any useful tutorial please show me :) – user1531389 Jul 17 '12 at 23:05
  • Well first thing is to decide what solution you are going to try first. If you have the option, I would start with Jersey for right now. Try get one of the sample services working. Then write your own. If you have questions with Jersey let us know. I think you probably need to install tomcat or maybe Jersey comes with Tomcat. Download Jersey and find out. – Code Droid Jul 18 '12 at 00:06
  • If there are some constraints on your project like you need to use this web server vs. that then try with that first, but if its open choice just start with Jersey for now. Getting it installed and running with samples is enough for right now. – Code Droid Jul 18 '12 at 00:08
  • right now am learning what you suggest ... there is no any constraint on my project, but i think because of i DONT know any thing about jersey Tomcat..etc...i think am in lost with those new concept,,, really i need simple tutorial to start my project!!!! – user1531389 Jul 23 '12 at 18:08
  • The truth is most web services are no longer written in Java anymore. I don't know if you want to take on another language, but Python is in very high demand (in the job market right now), but php also works very well. Its more common for a web service to be written in these languages lately. – Code Droid Jul 24 '12 at 22:03
  • ok well..i will start developing using Python next time... because right now; am tied by TIME!!!! – user1531389 Jul 27 '12 at 00:08