使用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.");
}
});}
提前感谢您帮助我
答案 0 :(得分:0)
我最近用翻新来检索数据...... 首先使用http://www.jsonschema2pojo.org/从可能的结果JSON响应中创建模型类。
我使用了Retrofit实例 -
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient客户端= new OkHttpClient.Builder()。addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();