Failed to use the Retrofit library in Android Studio

0

Following the tutorial below,

link

What about the use of the Retrofit library , for testing, I've created:

A Activity that I called retrofit and a BUTTON on it

package carcleo.com.radiosingular;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import carcleo.com.radiosingular.classes.Clientes;
import carcleo.com.radiosingular.classes.ClientesI;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class retrofit extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.retrofit);
        Button btnRf = (Button) findViewById(R.id.btnRf);
        btnRf.setOnClickListener(new View.OnClickListener() {
                                     @Override
                                     public void onClick(View view) {
             ClientesI clientes = ClientesI.retrofit.create(ClientesI.class);
             final Call<Clientes> call = clientes.getClientes();
             call.enqueue(new Callback<Clientes>() {
                 @Override
                 public void onResponse(Call<Clientes> call, Response<Clientes> response) {
                     int code = response.code();
                     if (code == 200) {
                         Clientes cliente = response.body();
                         Toast.makeText(getBaseContext(), "Id do cliente: " + cliente.getIdClientesT(), Toast.LENGTH_LONG).show();
                         Toast.makeText(getBaseContext(), "Tipo do cliente: " + cliente.getTipo(), Toast.LENGTH_LONG).show();
                         Toast.makeText(getBaseContext(), "Nome do cliente: " + cliente.getNome(), Toast.LENGTH_LONG).show();
                     } else {
                         Toast.makeText(getBaseContext(), "Falha: " + String.valueOf(code), Toast.LENGTH_LONG).show();
                     }
                 }

                 @Override
                 public void onFailure(Call<Clientes> call, Throwable t) {
                     Toast.makeText(getBaseContext(), t.getMessage(), Toast.LENGTH_LONG).show();
                 }

             });
         }
     }
        );
    }

}

A CustomersI

package carcleo.com.radiosingular.classes;

import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface ClientesI {

    @GET("/")

    Call<Clientes> getClientes();

    public static final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://hotplateprensas.com.br/ws/clientest.php/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

}
The url of the code above already delivers a string in JSON format, p>
{"clientes":[{"idClientesT":"1","tipo":"s","nome":"Carlos"},{"idClientesT":"2","tipo":"s","nome":"Rogério"}]}

It happens that when you get to the line

final Call<Clientes> call = clientes.getIdClientesT("");

is giving the console error.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: carcleo.com.radiosingular, PID: 4015
    java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.).
        for method ClientesI.getClientes
        at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
        at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:711)
        at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:174)
        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
        at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
        at java.lang.reflect.Proxy.invoke(Proxy.java:813)
        at $Proxy0.getClientes(Unknown Source)
        at carcleo.com.radiosingular.retrofit$1.onClick(retrofit.java:27)
        at android.view.View.performClick(View.java:5640)
        at android.view.View$PerformClick.run(View.java:22455)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6165)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

In the OnResponse method, a Call<Clientes>

 public void onResponse(Call<Clientes> call, Response<Clientes> response) {

And it looks like a search is done and nothing is returned. Because it searches for a parameter in url that does not exist.

Where am I going wrong?

    
asked by anonymous 17.12.2018 / 17:25

1 answer

1

The problem is that you are using the wrong annotation from the library. In fact you're not even using it.

The error is as follows:

  

@Path can only be used with relative url on @GET

First you need to understand what each one does:

@GET

It's an annotation that adds new values to the base URL.

@Path

It is an annotation that makes substitution of parameters of @GET so that it sends values dynamically.

Only the @GET parameter is empty. So there's nothing to replace.

See, your base URL is this:

baseUrl("http://hotplateprensas.com.br/ws/clientest.php/")

Let's suppose you really need to send a string to your API. So it would look something like this:

@GET("/{cliente}") // <-- adiciona parâmetro do cliente
Call<Clientes> getIdClientesT(@Path("cliente") String cliente); // <-- substitui parâmetro pelo nome

Now your URL would change to, for example, this:

http://hotplateprensas.com.br/ws/clientest.php/Carlos

One solution

The way you look at your URL will give you trouble anyway. I did some tests, and I think it would be interesting to pass this clientest.php as a parameter to the url. I do not know where you create the Client Object but you can do something like this:

Interface

 @GET("{url}")
 Call<List<Clientes>> getIdClientesT(@Path("url") String url);

Customers

In the Customers class you create a static final attribute like this:

static final String url = "clientest.php";

Activity

 final Call<List<Clientes>> call = clientes.getIdClientesT(Clientes.url);

And change the base URL to:

 baseUrl("http://hotplateprensas.com.br/ws/");

This will probably solve the problem!

    
17.12.2018 / 18:16