我正在使用Retrofit2创建一个发布请求,以将数据存储为json。我设置了this demo之后的所有内容,但发布请求从不返回成功甚至失败。我添加了祝酒词,看它是失败还是成功,但是我从没有看到过。任何帮助都将受到欢迎。
这是我的RetrofitClient类:
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
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;
}
}
这是我的Api服务接口:
import com.community.jboss.visitingcard.Data.model.VisitingCard;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
public interface APIService {
@POST("/posts")
@FormUrlEncoded
Call<VisitingCard> saveVisitingCard(@Field("name") String name,
@Field("email") String email,
@Field("number") String number,
@Field("id") int id,
@Field("adress") String adress);
}
这是我的ApiUtils类:
public class ApiUtils {
private ApiUtils() {}
public static final String BASE_URL =
"http://jsonplaceholder.typicode.com/";
public static APIService getAPIService() {
return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}
}
这是我的模特
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class VisitingCard {
@SerializedName("name")
@Expose
private String name;
@SerializedName("email")
@Expose
private String email;
@SerializedName("number")
@Expose
private String number;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("adress")
@Expose
private String adress;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNumber(){
return number;
}
public void setNumber(String number){
this.number = number;
}
public String getAdress(){
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
}
这是我用于在Main活动中运行发布请求的代码:
在OnCreate方法中:
mAPIService = ApiUtils.getAPIService();
当我单击“保存”按钮时,将执行以下代码:
private void sendVisitCard(SharedPreferences settings) {
mAPIService.saveVisitingCard(
settings.getString("card_name",""),
settings.getString("card_email",""),
settings.getString("card_phone",""),
1,
settings.getString("card_address", "") ).enqueue(new Callback<VisitingCard>() {
@Override
public void onResponse(Call<VisitingCard> call,
Response<VisitingCard> response) {
if(response.isSuccessful()) {
Toast.makeText(VisitingCardActivity.this,"Successfully
stored your visiting card",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<VisitingCard> call, Throwable t) {
Toast.makeText(VisitingCardActivity.this,"error while storing
your visiting card",Toast.LENGTH_SHORT).show();
}
});
}
这是response.errorBody()。string()错误的值:
Error: Insert failed, duplicate id
at Function.insert (/app/node_modules/lodash-id/src/index.js:49:18)
at /app/node_modules/lodash/lodash.js:4388:28
at arrayReduce (/app/node_modules/lodash/lodash.js:683:21)
at baseWrapperValue (/app/node_modules/lodash/lodash.js:4387:14)
at LodashWrapper.wrapperValue (/app/node_modules/lodash/lodash.js:9050:14)
at create (/app/node_modules/json-server/lib/server/router/plural.js:221:52)
at Layer.handle [as handle_request]
(/app/node_modules/express/lib/router/layer.js:95:5)
at next (/app/node_modules/express/lib/router/route.js:137:13)
at next (/app/node_modules/express/lib/router/route.js:131:14)
at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
这是response.code的值v:500
答案 0 :(得分:0)
响应似乎不成功。您只检查了成功方案。
考虑在方法onResponse
中添加else块:
if(response.isSuccessful()) {
Toast.makeText(VisitingCardActivity.this,"Successfully stored your visiting card",Toast.LENGTH_SHORT).show();
} else {
// do something.
// also check the value of `response.code()` and `response.errorBody().string()`
}
答案 1 :(得分:0)
如果您发布Json响应会很有帮助。
如果您的json响应以“ [”开头,那么您需要添加
Call<List<VisitingCard>> saveVisitingCard(@Field("name") String name,
@Field("email") String email,
@Field("number") String number,
@Field("id") int id,
@Field("adress") String adress);
以大写字母“ {”
Call<VisitingCard> saveVisitingCard(@Field("name") String name,
@Field("email") String email,
@Field("number") String number,
@Field("id") int id,
@Field("adress") String adress);
希望这会对您有所帮助。或尝试使用其他方法(如果不是成功的话)。