Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我正在尝试执行POST请求,但它没有被执行?

我使用Reformation2创建一个post请求,将数据存储为json。我设置了this demo之后的所有内容,但post请求从未返回成功或失败。我加了一些祝酒词,看看它是成功还是失败,但我从来没见过它们。欢迎任何帮助

这是我的客户端类:

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

这是我用来在主活动中运行post请求的代码:

在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();

        }
    });
}

这就是回应的价值。errorBody()。字符串()错误:

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)

这就是回应的价值。代码:500


共 (0) 个答案