Failed to use Retrofit library in Android Studio

Asked

Viewed 160 times

0

Following the tutorial below,

http://www.vogella.com/tutorials/Retrofit/article.html

which deals with the use of retrofit library, for testing, I created:

One Activity who I called retrofit and a BUTTON in 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();
                 }

             });
         }
     }
        );
    }

}

One Interface Clientesi

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

}

To url of code above already delivers a string format JSON,that is just below

{"clientes":[{"idClientesT":"1","tipo":"s","nome":"Carlos"},{"idClientesT":"2","tipo":"s","nome":"Rogério"}]}

Turns out when you get on the line

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

is giving error in console.

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 method OnResponse, a reference is made Call<Clientes>

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

 And it seems that a search is made and nothing is returned. Because it searches for a parameter in the url that doesn’t exist. 

Where am I going wrong?

1 answer

1


The problem is that you are using the library annotation wrong. Actually you are 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

Is an annotation that adds new values to the base URL.

@Path

It is an annotation that overwrites parameters of the @GET to send values dynamically.


Except that the @GET is empty. Therefore, there is nothing to replace.

See, your base URL is this:

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

Let’s assume that you really need to send a string to your API. So, it would be 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

A solution

The way your URL is going to be a problem anyway. I did some tests, and I think it would be interesting to pass this clientest.php as a url parameter. I don’t 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 Clients 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!

  • 1

    Please avoid long discussions in the comments; your talk was moved to the chat (and can proceed normally there, just click on the link)

Browser other questions tagged

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