gpt4 book ai didi

Android Retrofit 2.0 JSON 文档未完全消耗

转载 作者:行者123 更新时间:2023-11-30 00:42:10 25 4
gpt4 key购买 nike

我正在使用改造 2.0 以带有 pojo 类的发布请求的形式发布数据,并返回字符串作为失败或成功的响应,现在问题是它说无法使用 json。

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.google.code.gson:gson:2.6.1'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp:okhttp:2.4.0

现在我正在尝试发布 jsonobect,如下所示。我的 pojo 类如下

  public class RecruiterProfileModel implements Serializable
{
@SerializedName("fname")
private String firstname;
@SerializedName("lname")
private String lastname;
@SerializedName("company")
private String companyName;
@SerializedName("address")
private String address;
@SerializedName("email")
private String email;
@SerializedName("paswd")
private String password;
@SerializedName("ph")
private String phone;
@SerializedName("location")
private String location;
@SerializedName("city")
private String city;
@SerializedName("state")
private String state;
@SerializedName("contry")
private String contry;
@SerializedName("pincode")
private String pincode;
@SerializedName("landmark")
private String landmark;

public RecruiterProfileModel(String fname, String lastname, String landmark, String location,
String city, String state, String contry, String pincode,
String email, String password, String phone,
String address,String companyName)
{
this.firstname=fname;
this.lastname=lastname;
this.landmark=landmark;
this.location=location;
this.city=city;
this.state=state;
this.contry=contry;
this.pincode=pincode;
this.email=email;
this.password=password;
this.phone=phone;
this.address=address;
this.companyName=companyName;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getContry() {
return contry;
}

public void setContry(String contry) {
this.contry = contry;
}

public String getPincode() {
return pincode;
}

public void setPincode(String pincode) {
this.pincode = pincode;
}

public String getLandmark() {
return landmark;
}

public void setLandmark(String landmark) {
this.landmark = landmark;
}
}

我的界面使用 pojo 模型作为正文参数并返回成功或失败的字符串响应

@POST("empowerapp/providersreg.php")
Call<String> registerRecruiter(
@Body RecruiterProfileModel profileModel);

我的post请求方法

     public void registerRecruiter(RecruiterProfileModel profileModel) {
Gson gson = new GsonBuilder().setLenient().create();

OkHttpClient client = new OkHttpClient();

Retrofit retrofit = new Retrofit.Builder().baseUrl(Allconstants.MAIN_URL).client(client).addConverterFactory(GsonConverterFactory.create(gson)).build();

RetrofitInterface service = retrofit.create(RetrofitInterface.class);

Call<String> call = service.registerRecruiter(profileModel);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Response<String> response, Retrofit retrofit) {
System.out.println("###coming"+response.body().toString());
if (response.body().toString().equalsIgnoreCase("success"))
{
pd.dismiss();
loginSession.createLoginSession(Allconstants.RECRUITER,Allconstants.R_REG_ACTIVITY);
Toast.makeText(Registration.this,"successfully registered",Toast.LENGTH_LONG).show();
System.out.println("###coming"+response.body().toString());
}else{
pd.dismiss();
Toast.makeText(Registration.this,"oops!!!something went wrong..try again",Toast.LENGTH_LONG).show();
}
}

@Override
public void onFailure(Throwable t) {
pd.dismiss();
Toast.makeText(Registration.this,t.getStackTrace().toString(),Toast.LENGTH_LONG).show();
System.out.println("###error1"+t.getMessage());
}
});
}

现在的问题是它说无法使用 json。请帮助我解决这个问题。非常感谢您的帮助。提前致谢

最佳答案

我还发现顺序也需要与 ScalarsConverterFactory.create 相同

    .addConverterFactory(ScalarsConverterFactory.create()) //important
.addConverterFactory(GsonConverterFactory.create(gson))

如果 GSON 像下面这样在顶部,它将无法工作:

    .addConverterFactory(GsonConverterFactory.create(gson))
.addConverterFactory(ScalarsConverterFactory.create()) //important

关于Android Retrofit 2.0 JSON 文档未完全消耗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42386250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com