Send information from an Android application to and from a web service

Let's say I need to create an Android application that sends a number from a text box to a web service with the click of a button. This service will send back a line with the message "your number ..." and a list of employees taken from a database sent in XML format

I don't have physical access to the web service code, but I know that it has a getData method that takes an int and returns a string. It also has a GetEmployees method that takes nothing and returns the XML mentioned above.

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

After searching, I came across three ways to communicate between an Android application and a service

It’s hard for me to figure out which of these methods fits my needs.

To do what I need more clearly, I was able to create 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 cannot figure out how to do this using java in android studio.

+1
source share
2 answers

I think the best way to get your work done is to use an HTTP message to do this, you need 5 things:

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

2) Create an http client:

 DefaultHttpClient httpclient = new DefaultHttpClient(); 

3) create an http response and an http request:

 HttpResponse httpresponse; HttpPost httppostreq; 

3) fill out 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 answer: httpresponse = httpclient.execute(httppostreq); //as a Json object httpresponse = httpclient.execute(httppostreq); //as a Json object

2 observations: you need to catch exceptions for exceptions, and always the http request must be executed in another thread.

+1
source

Pretty much depends on how the web service is created. Since there is no details in your question, I can give you advice to stick with the Android HTTP client if you want your requests to be managed.

If you want to send and receive only simple data from a web service, you can use Sockets and write / read their output / input streams. Of course, you must implement the HTTP protocol yourself. However, for simple queries, this is my preferred method. If you do not know the HTTP protocol, I suggest taking a look at browsers such as Live HTTP Headers .

Example google start page request:

  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(); } 
+1
source

Source: https://habr.com/ru/post/1216561/


All Articles