Following the tutorial below,
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?