Java 使用改造在 POST 请求中发送 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37139117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending JSON in POST request with retrofit
提问by Teasel
I've seen this questions many times and tried many solutions but without resolving my problem, I'm trying to send a json in a POST request using retrofit, I'm not an expert in programming so I may miss something obvious.
我已经多次看到这个问题并尝试了很多解决方案,但没有解决我的问题,我正在尝试使用改造在 POST 请求中发送一个 json,我不是编程专家,所以我可能会错过一些明显的东西。
My JSON is in a string and looks like that:
我的 JSON 在一个字符串中,看起来像这样:
{"id":1,"nom":"Hydrogène","slug":"hydrogene"}
My Interface (called APIService.java) looks like that:
我的接口(称为APIService.java)如下所示:
@POST("{TableName}/{ID}/update/0.0")
Call<String> cl_updateData(@Path("TableName") String TableName, @Path("ID") String ID);
And my ClientServiceGenerator.javalooks like that:
我的ClientServiceGenerator.java看起来像这样:
public class ClientServiceGenerator{
private static OkHttpClient httpClient = new OkHttpClient();
public static <S> S createService(Class<S> serviceClass, String URL) {
Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
}}
And finally here is the code in my activity
最后这是我活动中的代码
APIService client = ClientServiceGenerator.createService(APIService.class, "http://mysiteexample.com/api.php/");
Call<String> call = client.cl_updateData("atomes", "1");
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Response<String> response, Retrofit retrofit) {
if (response.code() == 200 && response.body() != null){
Log.e("sd", "OK");
}else{
Log.e("Err", response.message()+" : "+response.raw().toString());
}
}
@Override
public void onFailure(Throwable t) {
AlertDialog alertError = QuickToolsBox.simpleAlert(EditDataActivity.this, "updateFail", t.getMessage(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertError.show();
}
});
Tell me if you need anything else, hope someone could help me.
如果您还需要什么,请告诉我,希望有人可以帮助我。
EDITDidn't mention it first time but my JSON won't always be with the same keys (id, nom, slug).
编辑第一次没有提到它,但我的 JSON 不会总是使用相同的键(id、nom、slug)。
采纳答案by Oleg Khalidov
First you need to create an object to represent the json you need:
首先你需要创建一个对象来表示你需要的json:
public class Data {
int id;
String nom;
String slug;
public Data(int id, String nom, String slug) {
this.id = id;
this.nom = nom;
this.slug = slug;
}
}
Then, modify your service to be able to send this object:
然后,修改您的服务以能够发送此对象:
@POST("{TableName}/{ID}/update/0.0")
Call<String> cl_updateData(@Path("TableName") String TableName, @Path("ID") String ID, @Body Data data);
Finally, pass this object:
最后,传递这个对象:
Call<String> call = client.cl_updateData("atomes", "1", new Data(1, "Hydrogène", "hydrogene"));
UPD
UPD
To be able to send any data use Object
instead of Data
:
为了能够发送任何数据使用Object
而不是Data
:
@POST("{TableName}/{ID}/update/0.0")
Call<String> cl_updateData(@Path("TableName") String TableName, @Path("ID") String ID,
@Body Object data);