简体   繁体   English

Retrofit2 POST请求,数据响应中为空值

[英]Retrofit2 POST Request, null values in data reponse

I have some issues with Retrofit2 while posting data to an API using POST request. 使用POST请求将数据发布到API时,Retrofit2存在一些问题。 The response from the server is successful but all the values displayed on the response are null : 来自服务器的响应成功,但是响应上显示的所有值均为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 这是我必须接收的JSON的示例

{
"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 : 这是我的API服务接口:

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 : 这是我的sendPost方法:

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 预先感谢您对我的帮助

I recently used retrofit to retrieve data... First used http://www.jsonschema2pojo.org/ to create a model class from your possible outcome JSON response . 我最近使用了改造来检索数据...首先使用http://www.jsonschema2pojo.org/从可能的结果JSON响应中创建模型类。

And I used Retrofit instance-- 我使用了Retrofit实例

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); HttpLoggingInterceptor拦截器= new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 拦截器.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); OkHttpClient客户端=新的OkHttpClient.Builder()。addInterceptor(interceptor).build();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM