0
I need a lot of help from you, I’m wanting to make a POST request via Retrofit, send only one 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 in return I receive:
[
{
"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 the POST, using the url. I have done several tutorials of Retrofit, but none successfully. Someone could 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);
}
}
Interface Apiservice:
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.");
}
});
}
}
Post the code you already tried, so we can analyze where you are missing and how to fix.
– Valdeir Psr
Hello @Valdeirpsr just posted the code.
– Romeu F
You are sending the
Post
in the methodenviarConsulta
? You can replaceCall<Post> consulta(@Body Post post);
forCall<Post> consulta(@Body JsonObject post);
and send a Json.– Valdeir Psr
The way you’re doing, you’re sending the class Post instead of the informed Json.
– Valdeir Psr
Okay I switched to jsonObject but as I create a json to send?
– Romeu F
JsonObject data = new JsonObject(); data.addProperty("key", "value");
– Valdeir Psr
mAPIService.query(-Which parameter-). enqueue(new Callback Which parameter I pass in the query?
– Romeu F
You pass the variable
data
– Valdeir Psr
Get it, I’ll test the return
– Romeu F
FATAL EXCEPTION: main Process: com.example.Romeu.testedatabase, PID: 806 java.lang.Runtimeexception: Unable to start activity ComponentInfo{com.example.romeu.testedatabase/com.example.romeu.testedatabase.MainActivity}: java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding. (Parameter #1 Apiservice.query
– Romeu F
this error appeared
– Romeu F
mAPIService is returning null
– Romeu F
I edited my post, of how to be currently the code
– Romeu F
I was building a code here. I’ll post it to Github for you to take a look.
– Valdeir Psr
great thank you!
– Romeu F
https://gist.github.com/valdeir2000/730bdcf479e8ac98fa8c3d74bbc315cb
– Valdeir Psr
Otimo valdeir gave it right!!! Wouldn’t I be able to do a function like I was doing and pass the value of the json parameter, and get the answer? Ex: sendConsult("select * from user"); and this method would return me the information
– Romeu F
You can, you can adapt or I can post as an answer. But the idea is the same.
– Valdeir Psr
Great, the last doubt how do I access a position of that answer? ex: I only want field 1, because the Post class is giving getCampo1, and is returning null
– Romeu F
I tested it here and it’s normal
– Valdeir Psr
how are you giving the get?
– Romeu F
Thank you very much!!!!
– Romeu F
As I rank you here valdeir??
– Romeu F
Looking at your code the return configuration is wrong, in the example vc shows that the answer is an Object Array, and in retrofit vc because Call<Post>, the correct would be Call<Post[]> or Call<List<Post>>, and you could use Map<String,String> instead of creating class for POST, and vc tbm could use a Map as a request parameter, so it tbm would create a json the way you wanted it to with minimal code
– Weslley Barbosa