我很新于Retrofit库,我想知道使用Retrofit的post方法发送对象的最佳方式是什么。
以下是一些代码:
我的类:
public class ExampleClass implements Serializable {
@SerializedName("id")
int id;
@SerializedName("name")
String name;
public ExampleClass(int id, String name) {
this.id = id;
this.name= name;
}
}
我的界面:
public interface ApiInterface {
@Headers("Content-Type: application/json")
@POST("getclass/")
Call<ExampleClass> getExampleClass(@Body ExampleClass exampleClass);
}
我和Retrofit的通话:
......
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<ExampleClass> call = apiService.getExampleClass(exampleClass);
call.enqueue(new Callback<ExampleClass>() {
@Override
public void onResponse(Call<ExampleClass> call, Response<ExampleClass> response) {
int statusCode = response.code();
Log.i(TAG, "Status Code: " + statusCode);
}
@Override
public void onFailure(Call<ExampleClass> call, Throwable t) {
Log.i(TAG, "Error: " + t.toString());
}
});
但每次它都返回状态码500。
有什么方法可以用Retrofit发送对象吗?