I need a lot of help from you, I'm trying to make a POST request via Retrofit, just send a parameter:
{
"select":"select id || ' | ' ||senha_terminal,nome,login_web,null,5,null n2,7,senha_terminal,senha_web,data_inclusao from usuario where id in (3257) order by id desc"
}
And as an answer I get:
[
{
"campo1": "3257 | 74327",
"campo2": "Sidnei",
"campo3": "sidnei01",
"campo4": null,
"campo5": 5,
"campo6": null,
"campo7": 7,
"campo8": "74327",
"campo9": "56c07af798f309dbd75822a849ce47b6",
"campo10": "2012-02-08T11:00:06"
}
]
The method I use in Postman is POST, using the url. I have done several Retrofit tutorials, but none with success. Could someone help?
My code: ApiUtils:
public class ApiUtils {
private ApiUtils() {}
public static final String BASE_URL = "https://services-dev.redetendencia.com.br/api-rest/helper-qa/select/";
public static APIService getAPIService() {
return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}
}
APIService Interface:
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
/**
* Created by romeu on 27/03/18.
*/
public interface APIService {
@POST("select")
Call<Post> consulta(@Body JsonObject post);
}
Post:
package com.example.romeu.testedatabase;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Post {
@SerializedName("campo1")
@Expose
private String campo1;
@SerializedName("campo2")
@Expose
private String campo2;
@SerializedName("campo3")
@Expose
private String campo3;
@SerializedName("campo4")
@Expose
private Object campo4;
@SerializedName("campo5")
@Expose
private Integer campo5;
@SerializedName("campo6")
@Expose
private Object campo6;
@SerializedName("campo7")
@Expose
private Integer campo7;
@SerializedName("campo8")
@Expose
private String campo8;
@SerializedName("campo9")
@Expose
private String campo9;
@SerializedName("campo10")
@Expose
private String campo10;
public String getCampo1() {
return campo1;
}
public void setCampo1(String campo1) {
this.campo1 = campo1;
}
public String getCampo2() {
return campo2;
}
public void setCampo2(String campo2) {
this.campo2 = campo2;
}
public String getCampo3() {
return campo3;
}
public void setCampo3(String campo3) {
this.campo3 = campo3;
}
public Object getCampo4() {
return campo4;
}
public void setCampo4(Object campo4) {
this.campo4 = campo4;
}
public Integer getCampo5() {
return campo5;
}
public void setCampo5(Integer campo5) {
this.campo5 = campo5;
}
public Object getCampo6() {
return campo6;
}
public void setCampo6(Object campo6) {
this.campo6 = campo6;
}
public Integer getCampo7() {
return campo7;
}
public void setCampo7(Integer campo7) {
this.campo7 = campo7;
}
public String getCampo8() {
return campo8;
}
public void setCampo8(String campo8) {
this.campo8 = campo8;
}
public String getCampo9() {
return campo9;
}
public void setCampo9(String campo9) {
this.campo9 = campo9;
}
public String getCampo10() {
return campo10;
}
public void setCampo10(String campo10) {
this.campo10 = campo10;
}
@Override
public String toString() {
return "Post{" +
"campo1='" + campo1 + '\'' +
", campo2='" + campo2 + '\'' +
", campo3='" + campo3 + '\'' +
", campo4=" + campo4 +
", campo5=" + campo5 +
", campo6=" + campo6 +
", campo7=" + campo7 +
", campo8='" + campo8 + '\'' +
", campo9='" + campo9 + '\'' +
", campo10='" + campo10 + '\'' +
'}';
}
}
RetrofitClient:
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private APIService mAPIService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAPIService = ApiUtils.getAPIService();
enviarConsulta("select id || ' | ' ||senha_terminal,nome,login_web,null,5,null n2,7,senha_terminal,senha_web,data_inclusao from usuario where id in (3257) order by id desc");
}
public void enviarConsulta(String query)
{
JsonObject data = new JsonObject(); data.addProperty("select", query);
mAPIService.consulta(data).enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if(response.isSuccessful()) {
Log.d("SQL", "post submitted to API." + response.body().toString());
}
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
Log.d("SQL", "Unable to submit post to API.");
}
});
}
}