翻新2

编程入门 行业动态 更新时间:2024-10-17 02:50:03
本文介绍了翻新2 - 如何传递POST JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

{

所以,我有我的自定义对象的列表,我需要这样的JSON: 调查:[{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 departamentovocênãoencontrou o produto? value:FERRAMENTAS,TAPETES},{kind:string,label:Deseja que a Havan entre em contato comvocê ?,value:Nãoinformado},{kind:string,label:Nome, value:Nãoinformado},{kind:string,lab el:电子邮件,value:Nãoinformado},{kind:string,label:Telefone ,value:Nãoinformado}] } }]}

但我没有任何意识如何使用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

本文发布于:2023-07-14 19:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!