Failed to use retrofit library in Android Studio
Following the tutorial below,
Http://www.vogella.com/tutorials/Retrofit/article.html
Which deals with the use of the retrofit library, for testing, I created:
A Activity that 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();
}
});
}
}
);
}
}
Uma Client Interfaceesi
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 format JSON , which is just below
{"clientes":[{"idClientesT":"1","tipo":"s","nome":"Carlos"},{"idClientesT":"2","tipo":"s","nome":"Rogério"}]}
It happens that when it arrives 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 OnResponse
method, a reference 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?
1 answers
The problem is that you are using the library annotation wrong. In fact 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
Is an annotation that does parameter substitution of @GET
so that the same send values dynamically.
Only the parameter of @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 you actually 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 it is your URL will give problem anyway. I made some tests, and I think it would be interesting for you 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);
Clients
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!