Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

java - Send information from android application to a Web Service and back

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 /
Host:google.com

");
        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();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...