2

Let's say I need to create an android app which will, on the click of a button, send a number from a textbox to a web service. This service will send back a string saying "your number was ... " and a list of employees taken from a database sent back as XML

I do not have a physical access to the code of the web service but I know that it has a "getData" method which takes an int and returns the string. It also has a "GetEmployees" method which takes nothing and returns the XML mentionned above.

The address of the web service looks something like this : http://exemple.qc.ca/exemple/Service1.svc

After searching I came across 3 ways of communicating between an android App and a service

I am having trouble figuring out which of these methods fit my needs.

To make what I need clearer I have managed to do a sample code using Visual Studio and VB.Net:

Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim service As New   ServiceReference2.Service1Client(ServiceReference2.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1)
Try
    lblReturn.Text = Await service.GetDataAsync(CInt(txtValueSent.Text))
Catch ex As Exception
    lblReturn.Text = ex.Message
    If Not ex.InnerException.Message Is Nothing Then
        lblReturn.Text = lblReturn.Text + ex.InnerException.Message
    End If
End Try

I am new to mobile programming and can't figure out how to do this using java in android studio.

micbobo
  • 862
  • 7
  • 24
  • I *personally* recommend the httpClient route over the others. But, only you have all the knowledge to make the decision as we don't really know what all your needs are. – Noah Mar 19 '15 at 19:26
  • This is for an internship and I do not have all the details on what the program will be yet but I know that I will need to get information such as a list of employees and then will need to browse through that list and get the informations of said employees. Information from any query I send will be sent back using XML. – micbobo Mar 19 '15 at 19:34

2 Answers2

1

I think the best way to achieve your work is using HTTP post, to do that you need 5 things:

1) do a Json object: JSONObject jsonobj = new JSONObject(); //you need to fill this object

2) Create a http client:

DefaultHttpClient httpclient = new DefaultHttpClient();

3) create a http response and a http request:

HttpResponse httpresponse;
HttpPost httppostreq;

3) complete your http request:

httppostreq = new HttpPost(SERVER_URL);

4) attach the json object (the one you will send):

StringEntity se = new StringEntity(jsonobj.toString());
se.setContentType("application/json;charset=UTF-8");
httppostreq.setEntity(se);

5) to get the response: httpresponse = httpclient.execute(httppostreq); //as a Json object

2 observations: you need to catch the possibles exceptions and always a http request must be done in a different thread.

NoobMaster69
  • 2,291
  • 3
  • 18
  • 24
  • i hope it works, and sorry for my syntax im new posting here ;) – Niklas Tampier Holmgren Mar 19 '15 at 19:44
  • If I understand well the service will need to receive a JSON object and then send one back after treatments? Is it problematic if the information is sent back as XML ? Also how do you call a specific method on the service. Is it SERVER_URL + "/" + METHOD in the httppostreq ? Thank you for your time, you have already been very helpful. – micbobo Mar 19 '15 at 20:06
  • there is no problem if you send an xml back, you must receive correctly. – Niklas Tampier Holmgren Mar 20 '15 at 02:29
  • To call an specific method you have 3 principal objects: – Niklas Tampier Holmgren Mar 20 '15 at 02:33
  • 1) Defaulthttpclient, HttpResponse and HttpPost, to make the request to the specific URL, just use Defaulthttpclient.execute(HttpPost); where HttpPost is "loaded" with the SERVER_URL (when you create the HttpPost object httppostreq = new HttpPost(SERVER_URL);) and finaly you recieve the answer from the server, like this: HttpResponse = Defaulthttpclient.execute(HttpPost); also you can use httpResponse.getEntity().getContent(); to get your stream back. I hope it is understandable – Niklas Tampier Holmgren Mar 20 '15 at 02:43
1

Depends pretty much on how the webservice is build up. Because your question has a lack of detail here I can only give you the advice to stick with the Android HTTP client if you want to have your requests managed.

If you only want to send/receive plain data from a webservice you can use Sockets and write/read to their output/inputstreams. Of course you have to implement the HTTP protocol on your own this way. Nevertheless for simple requests this is my preferred method. If you are not known to the HTTP protocol I suggest to take a look at browserplugins like Live HTTP Headers.

Example querying the google startpage:

    try {
        Socket socket = new Socket("google.com", 80);
        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        writer.print("GET /\r\nHost:google.com\r\n\r\n");
        writer.flush();

        InputStreamReader isr = new InputStreamReader(socket.getInputStream());
        BufferedReader reader = new BufferedReader(isr);
        for(String s; (s = reader.readLine()) != null;) {
            System.out.printf("%s", s);
        }
        isr.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
Konstantin W
  • 409
  • 3
  • 11