[英]How to pass request params for json body in retrofit?
How to set request params for json type params in a call from MainActivity?如何在来自 MainActivity 的调用中为 json 类型参数设置请求参数?
This is my Interface class in which there is headers annotation of type application/json and using the post method and use call in which using body annotation having a model for param request这是我的接口类,其中有类型为 application/json 的头注释,并使用 post 方法和使用调用,其中使用具有参数请求模型的主体注释
public interface ApiStorePhotoInterface {
@Headers("Content-Type: application/json")
@POST("photo/st/v7")
Call<PhotoStoreMainModel> getResponse(@Body ParamModel paramModel);
}
Here ParamModel is json model and Use this for call.这里的 ParamModel 是 json 模型,并使用它进行调用。
2 Param Models for @Body in Interface- This Model contains values with the List in which ParamProductDetailModel is the second model in it.接口中@Body 的 2 个参数模型 - 此模型包含带有列表的值,其中 ParamProductDetailModel 是其中的第二个模型。 1st Param Model -
第一参数模型 -
public class ParamModel {
@SerializedName("apiKey")
@Expose
private String apiKey;
@SerializedName("affId")
@Expose
private String affId;
@SerializedName("act")
@Expose
private String act;
@SerializedName("latitude")
@Expose
private String latitude;
@SerializedName("longitude")
@Expose
private String longitude;
@SerializedName("devinf")
@Expose
private String devinf;
@SerializedName("appver")
@Expose
private String appver;
@SerializedName("productDetails")
@Expose
private List<ParamProductDetailModel> productDetails = null;
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getAffId() {
return affId;
}
public void setAffId(String affId) {
this.affId = affId;
}
public String getAct() {
return act;
}
public void setAct(String act) {
this.act = act;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getDevinf() {
return devinf;
}
public void setDevinf(String devinf) {
this.devinf = devinf;
}
public String getAppver() {
return appver;
}
public void setAppver(String appver) {
this.appver = appver;
}
public List<ParamProductDetailModel> getProductDetails() {
return productDetails;
}
public void setProductDetails(List<ParamProductDetailModel> productDetails) {
this.productDetails = productDetails;
}
}
2nd Param Model -第二参数模型 -
public class ParamProductDetailModel {
@SerializedName("productId")
@Expose
private String productId;
@SerializedName("qty")
@Expose
private String qty;
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
}
This is the client class这是客户端类
Client class-客户类-
This is the retrofit builder class这是改造建造者类
public class ApiStorePhotoClient {
private static final String BASE_URL = " https://services.something.com/api/";
private static ApiStorePhotoInterface apiInterface;
public static ApiStorePhotoInterface getApi() {
if (apiInterface == null) {
apiInterface = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiStorePhotoInterface.class);
}
return apiInterface;
}
}
Now, in my Main class-现在,在我的主课中-
ApiStorePhotoInterface apiInterface = ApiStorePhotoClient.getApi();
Call<PhotoStoreMainModel> call = apiInterface.getResponse("What to write here??");
How to send Request inside the model in getResponse()?如何在 getResponse() 中在模型内部发送请求?
PhotoStoreMainModel is the response model. PhotoStoreMainModel 是响应模型。 The code has 3 response model in which the PhotoStoreMainModel is one of them and 2 request params model which is posted above.
该代码有 3 个响应模型,其中 PhotoStoreMainModel 是其中之一,以及上面发布的 2 个请求参数模型。
How to send request params in @body ParamModel inside Mainclass in getResponse()?如何在getResponse()中的Mainclass内的@body ParamModel中发送请求参数?
How to set lines for a call in my MainActivity class?如何在我的 MainActivity 类中设置呼叫线路?
I Expect the result but not able to hit the API due to request params
我期待结果,但由于请求参数而无法访问 API
You can add arrayObjects like this, after update you can check your request with Log.:您可以像这样添加 arrayObjects,更新后您可以使用 Log 检查您的请求:
JsonObject jsonObject1 = new JsonObject();
jsonObject1.addProperty("apiKey", "wseoagbiis5dd6vq5tfbs3r1c8rsz");
jsonObject1.addProperty("appver", "1.00");
jsonObject1.addProperty("affid", "team");
Global.printLog("jsonObject1", "===" + jsonObject1);
JsonArray productDetail1Array = new JsonArray();
for (int i1 = 0; i1 < 1; i1++) {
JsonObject productDetail1 = new JsonObject();
productDetail1.addProperty("productId", "6670002");
JsonArray array1 = new JsonArray();
for (int j = 0; j < 2; j++) {
JsonObject imageDetails = new JsonObject();
imageDetails.addProperty("qty", j + "");
imageDetails.addProperty("url", "something.com/assets_admin/uploads/frame_image/…");
array1.add(imageDetails);
}
productDetail1.add("imageDetails", array1);
productDetail1Array.add(productDetail1);
}
jsonObject1.add("productDetails", productDetail1Array);
Global.printLog("jsonObject1", "===" + jsonObject1);
You can pass your object like this:你可以像这样传递你的对象:
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("key1", "value1");
jsonObject.put("key2", value2);
JsonParser jsonParser = new JsonParser();
ApiStorePhotoInterface apiInterface = ApiStorePhotoClient.getApi();
**Call<PhotoStoreMainModel> call = apiInterface.getResponse((JsonObject) jsonParser.parse(jsonObject.toString())); //This is not the correct way to parse the json. Not getting correct hit**
call.enqueue(new Callback<PhotoStoreMainModel>() {
@Override
public void onResponse(Call<PhotoStoreMainModel> call, Response<PhotoStoreMainModel> response) {
}
@Override
public void onFailure(Call<PhotoStoreMainModel> call, Throwable t) {
call.cancel();
t.printStackTrace();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
No need to add content type
无需添加
content type
Change your interface
like改变你的
interface
public interface ApiStorePhotoInterface {
@POST("photo/st/v7")
Call<PhotoStoreMainModel> getResponse(@Body JsonObject obj);
}
Make json object
like:使
json object
像:
JsonObject json = new JsonObject();
json.addProperty("affId", id_value)
// like above all params // 就像上面所有的参数一样
pass the above in object in getResponse在 getResponse 中的对象中传递上述内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.