Consume PHP Webservice using Java/Android

Asked

Viewed 523 times

0

I built following the guidance of a friend a webservice using php (actually is a web application that returns to all requests made via post a string in format json).

First what I’d like to know is whether what I’ve done is in fact a webservice?

Second, what I wanted to do now was consume this webservice using an application made in java, more precisely android.

I have no idea what to do to consume this webservice.

So I would expect you to explain to me what needs to be done and indicate some tutorials on the Internet or even make a code available for me to base.

Thank you for your cooperation!

  • 1

    Behold here, has a lot to do with!

2 answers

1


In general terms, a webservice is a function that can be accessed by another program using the web protocol (HTTP), ie if your program returns an html that can be viewed by a human, this is not a webservice, however, if it is returning a JSON that will be consumed by another program, then yes we can say that it is a webservice

This is a brief definition and there’s a whole more elaborate concept behind it.

To send http requests using Android, I use the library Volley, it is present in the official Android documentation that you can check here.

A simple request example using Volley:

// Criamos a fila de requisições
RequestQueue fila = Volley.newRequestQueue(this);
String endpoint = "https://api.punkapi.com/v2/beers";

// Criamos a request string a partir da URL
StringRequest stringRequest = new StringRequest(Request.Method.GET, endpoint, 
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("Response", "is: "+ response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Response Error", "Erro na requisição!");
        }
    });
    // Adicionamos a requisição a fila de requisições
    fila.add(stringRequest);
  • Thank you for answering and exemplifying. I’m going to test this library and see if I can do things here. I gave a search and saw that it is also possible to do with native Java libraries. I will test and compare to see which is the best solution. Sorry it took me so long to answer!

0

Basically, you will need to run calls via HTTP pro web address that is running the webservice application.

You can call the webservice manually, using a tool like Curl, Postman, etc, to better understand how it works.

To do it in Java you can use a library that abstracts a lot of logic for you, like https://github.com/OpenFeign/feign and implement as required in your programme.

  • Um, I’m gonna go check out this library, too. I searched and saw what can be done using the native libraries of java, I will test those indicated and see which is the simplest to use. Thank you for the reply and sorry for the delay!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.