我在Github问题上发现,清理项目很好,但没有帮助。我正在使用retofit 2。这些是依赖项-
实施"com.squareup.retrofit2:改造:2.8.1">
实现"com.squareup.retrofit2:converter gson:2.8.1">
实现‘com.google.code.gson:gson:2.8.5’
我的型号类别
public class Register {
private String email,password,firstname,lastname,snuid;
public Register(String email, String password, String firstname, String lastname, String snuid) {
this.email = email;
this.password = password;
this.firstname = firstname;
this.lastname = lastname;
this.snuid = snuid;
}
}
我的JsonApiHolder类
public interface Pythonforeverybody {
@GET("medicines")
Call<List<Post>> getprods();
@GET("medicines/{id}")
Call<List<Post>> getprods (@Path("id") int id1);
@POST("register/")
Call <Register> getreg(@Body Register register);
}
以及主活动中的一种方法
private void postcontent() {
Register register = new Register("ss501@snu.edu.in",
"123456","Shardul","Singh","ss501");
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https:// Some API")
//I gave the correct Api link over here, no problem with that
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
Pythonforeverybody jsonPlaceHolderApi = retrofit.create(Pythonforeverybody.class);
Call<Register> call = jsonPlaceHolderApi.getreg(register);
call.enqueue(new Callback<Register>() {
@Override
public void onResponse(Call<Register> call, Response<Register> response) {
if (!response.isSuccessful()) {
tv.setText("Code hh: " + response.code());
return;
}
String content = "";
content += "Code " + response.code() + "n";
tv.append(content);
}
@Override
public void onFailure(Call<Register> call, Throwable t)
{
tv.setText(t.getMessage());
}
});
}
}
使用此:
@POST("register/?format=json")
Call <Register> getreg(@Body Register register);
而不是
@POST("register/")
Call <Register> getreg(@Body Register register);