[英]How to pass request params for json body in retrofit?
如何在来自 MainActivity 的调用中为 json 类型参数设置请求参数?
这是我的接口类,其中有类型为 application/json 的头注释,并使用 post 方法和使用调用,其中使用具有参数请求模型的主体注释
public interface ApiStorePhotoInterface {
@Headers("Content-Type: application/json")
@POST("photo/st/v7")
Call<PhotoStoreMainModel> getResponse(@Body ParamModel paramModel);
}
这里的 ParamModel 是 json 模型,并使用它进行调用。
接口中@Body 的 2 个参数模型 - 此模型包含带有列表的值,其中 ParamProductDetailModel 是其中的第二个模型。 第一参数模型 -
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;
}
}
第二参数模型 -
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;
}
}
这是客户端类
客户类-
这是改造建造者类
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;
}
}
现在,在我的主课中-
ApiStorePhotoInterface apiInterface = ApiStorePhotoClient.getApi();
Call<PhotoStoreMainModel> call = apiInterface.getResponse("What to write here??");
如何在 getResponse() 中在模型内部发送请求?
PhotoStoreMainModel 是响应模型。 该代码有 3 个响应模型,其中 PhotoStoreMainModel 是其中之一,以及上面发布的 2 个请求参数模型。
如何在getResponse()中的Mainclass内的@body ParamModel中发送请求参数?
如何在我的 MainActivity 类中设置呼叫线路?
我期待结果,但由于请求参数而无法访问 API
您可以像这样添加 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);
你可以像这样传递你的对象:
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();
}
无需添加content type
改变你的interface
public interface ApiStorePhotoInterface {
@POST("photo/st/v7")
Call<PhotoStoreMainModel> getResponse(@Body JsonObject obj);
}
使json object
像:
JsonObject json = new JsonObject();
json.addProperty("affId", id_value)
// 就像上面所有的参数一样
在 getResponse 中的对象中传递上述内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.