但我没有任何意识如何使用Gson做到这一点。 我使用Retrofit 2并需要将这个JSON传递给一个正文请求。 任何ideias?
解决方案是的,您需要将此JSON传递给正文请求。 b 改造界面:
public interface RetrofitInterface< R extends RetrofitClass> { @Headers({Content-Type:application / json,Cache-Control:max-age = 640000}) @POST(v1 / auth /) public Call< ResponseBody> callLogin(@Query(key)字符串键,@Body LoginModel body); @Headers({Content-Type:application / json,Cache-Control:max-age = 640000}) public static final Retrofit retrofit = new Retrofit.Builder ) .baseUrl(AppConstants.mBaseUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); }Api调用活动:
将json对象传递给body请求使用@Body参数。
在这里,您可以在 www.jsonschema2pojo/ 通过使用json请求格式转换gson转换器。
之后,使用该gson pojo类设置值,并在改进时将json对象传递给body请求。
例如: LoginModel: public class LoginModel { @SerializedName(username)私人字符串用户名; @SerializedName(password)私人字符串密码; public String getUsername(){ return username; } public void setUsername(String username){ this.username = username; } public String getPassword(){ return password; } public void setPassword(String password){ this.password = password; $ b $ h $> b $ b LoginModel model_obj = new LoginModel(); mModel_obj.setUsername(mUsername); mModel_obj.setPassword(mPassword);Api调用:
通话< ResponseBody> call = service.callLogin(AppConstants.mApiKey,model_obj); call.enqueue(new Callback< ResponseBody>(){ @Override public void onResponse(Call< ResponseBody> call,Response< ResponseBody> response){} @Override public void onFailure(Call< ResponseBody> call,Throwable t){} });
So, i have a List of my custom Object and I need a JSON like this:
{ "surveys": [{ "survey": { "code": "05052017153632", "date": "05/05/2017 15:36:32", "device_id": 1, "questions_attributes": [{ "kind": "string", "label": "Você encontrou tudo o que procurava?", "value": "Infelizmente, não" }, { "kind": "string", "label": "Em qual departamento você não encontrou o produto?", "value": "FERRAMENTAS, TAPETES" }, { "kind": "string", "label": "Deseja que a Havan entre em contato com você?", "value": "Não informado" }, { "kind": "string", "label": "Nome", "value": "Não informado" }, { "kind": "string", "label": "E-mail", "value": "Não informado" }, { "kind": "string", "label": "Telefone", "value": "Não informado" }] } }]}But I dont have any ideia how to do it using Gson. I'm Using Retrofit 2 and need to pass this JSON into a body request. Any ideias?
解决方案Yes you need to pass this JSON into body request.
Retrofit Interface: public interface RetrofitInterface<R extends RetrofitClass> { @Headers({"Content-Type: application/json", "Cache-Control: max-age=640000"}) @POST("v1/auth/") public Call<ResponseBody> callLogin(@Query("key") String key, @Body LoginModel body); @Headers({"Content-Type: application/json", "Cache-Control: max-age=640000"}) public static final Retrofit retrofit = new Retrofit.Builder() .baseUrl(AppConstants.mBaseUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); }Api call Activity:
pass json object into body request use @Body param.
Here you can create gson model classes in www.jsonschema2pojo/ json to gson converter by using that json request format.
After that set values with that gson pojo classes and pass the json object to body request in retrofit.
For example: LoginModel: public class LoginModel { @SerializedName("username") private String username; @SerializedName("password") private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }set Values with pojo class:
LoginModel model_obj = new LoginModel(); mModel_obj.setUsername(mUsername); mModel_obj.setPassword(mPassword);Api calling:
Call<ResponseBody> call = service.callLogin(AppConstants.mApiKey, model_obj); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { } });
更多推荐
翻新2
发布评论