Boris El Gareh
Boris El Gareh

Reputation: 51

Retrofit2 POST Request, null values in data reponse

I have some issues with Retrofit2 while posting data to an API using POST request. The response from the server is successful but all the values displayed on the response are null :

I/SUCCESS: test : Post{id='null', titre='null', description='null', prix='null', pseudo='null', emailContact='null', telContact='null', ville='null', cp='null', image='null', date='null'}

Here is an example of the JSON that I've to receive

{
"success": true,
"response": {
"id": “5a5f11196c2aa”,
"titre": "Vends 406",
"description": "Pas chere",
"prix": "4500",
"pseudo": "jml",
"emailContact": "",
"telContact": "Vends 406",
"ville": "",
"cp": "",
"images": [],
"date": 1516179737
}
}

Here is my data model :

public class Post {

@SerializedName("cp")
private String cp;
@SerializedName("date")
private Long date;
@SerializedName("description")
private String description;
@SerializedName("emailContact")
private String emailContact;
@SerializedName("id")
private String id;
@SerializedName("images")
private String[] images;
@SerializedName("prix")
private String prix;
@SerializedName("pseudo")
private String pseudo;
@SerializedName("telContact")
private String telContact;
@SerializedName("titre")
private String titre;
@SerializedName("ville")
private String ville;

public String getCp() {
    return cp;
}

public void setCp(String cp) {
    this.cp = cp;
}

public Long getDate() {
    return date;
}

public void setDate(Long date) {
    this.date = date;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getEmailContact() {
    return emailContact;
}

public void setEmailContact(String emailContact) {
    this.emailContact = emailContact;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String[] getImages() {
    return images;
}

public void setImages(String[] images) {
    this.images = images;
}

public String getPrix() {
    return prix;
}

public void setPrix(String prix) {
    this.prix = prix;
}

public String getPseudo() {
    return pseudo;
}

public void setPseudo(String pseudo) {
    this.pseudo = pseudo;
}

public String getTelContact() {
    return telContact;
}

public void setTelContact(String telContact) {
    this.telContact = telContact;
}

public String getTitre() {
    return titre;
}

public void setTitre(String titre) {
    this.titre = titre;
}

public String getVille() {
    return ville;
}

public void setVille(String ville) {
    this.ville = ville;
}

@Override
public String toString(){
    return "Post{" +
            "id='" + getId() + '\'' +
            ", titre='" + getTitre() + '\'' +
            ", description='" + getDescription() + '\'' +
            ", prix='" + getPrix() + '\'' +
            ", pseudo='" + getPseudo() + '\'' +
            ", emailContact='" + getEmailContact() + '\'' +
            ", telContact='" + getTelContact() + '\'' +
            ", ville='" + getVille() + '\'' +
            ", cp='" + getCp() + '\'' +
            ", image='" + getImages() + '\'' +
            ", date='" + getDate() + '\'' +
            '}';
}}

Here is my Retrofit Instance :

retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()))
                .build();

Here is my API Service Interface :

public interface APIService {
@POST("/android-api")
@FormUrlEncoded
Call<Post> savePost(@Field("apikey") String apikey,
                       @Field("method") String method,
                       @Field("titre") String title,
                       @Field("description") String description,
                       @Field("prix") String prix,
                       @Field("pseudo") String pseudo,
                       @Field("emailContact") String emailContact,
                       @Field("telContact") String telContact,
                       @Field("ville") String ville,
                       @Field("cp") String cp,
                       @Field("images") String[] images
                    );}

And here is my sendPost method :

public void sendPost(String title, String prix, String cp, String ville, String description) {

    // On récupère ici les préférences de l'utilisateur
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String pseudo = preferences.getString("pseudo", "DEFAULT");
    String emailContact = preferences.getString("mail", "DEFAULT");
    String telContact = preferences.getString("tel", "DEFAULT");

    // Provisoire
    String[] images = {} ;

    mAPIService.savePost(APIKEY,METHOD,title,description,prix,pseudo,emailContact,telContact,ville,cp,images).enqueue(new Callback<Post>() {
        @Override
        public void onResponse(Call<Post> call, Response<Post> response) {

            if(response.isSuccessful()) {
                showResponse(response.body().toString());
                Log.i("SUCCESS", "test : "+ response.body().toString());
            }
        }

        @Override
        public void onFailure(Call<Post> call, Throwable t) {
            Log.e("FAIL", "Unable to submit post to API.");
        }
    });}

Thank you in advance for helping me

Upvotes: 0

Views: 1606

Answers (1)

Biki
Biki

Reputation: 130

I recently used retrofit to retrieve data... First used http://www.jsonschema2pojo.org/ to create a model class from your possible outcome JSON response .

And I used Retrofit instance--

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build(); 

Upvotes: 0

Related Questions