【问题标题】:Retrofit2 POST Request, null values in data reponseRetrofit2 POST请求,数据响应中的空值
【发布时间】:2018-08-08 17:06:22
【问题描述】:

在使用 POST 请求将数据发布到 API 时,我遇到了 Retrofit2 的一些问题。 来自服务器的响应是成功的,但是响应中显示的所有值都是空的:

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'}

这是我必须接收的 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
}
}

这是我的数据模型:

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() + '\'' +
            '}';
}}

这是我的改造实例:

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

这是我的 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
                    );}

这是我的 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.");
        }
    });}

提前感谢您帮助我

【问题讨论】:

    标签: java android post retrofit2


    【解决方案1】:

    我最近使用改造来检索数据... 首先使用 http://www.jsonschema2pojo.org/ 从您可能的结果 JSON 响应创建模型类。

    我使用了 Retrofit 实例--

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

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

    【讨论】:

    • 谢谢!日志对我帮助很大,因此我发现了错误。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    相关资源
    最近更新 更多