Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在改型中传递json正文的请求参数?_Android_Gson_Retrofit2 - Fatal编程技术网

Android 如何在改型中传递json正文的请求参数?

Android 如何在改型中传递json正文的请求参数?,android,gson,retrofit2,Android,Gson,Retrofit2,如何在MainActivity的调用中为json类型参数设置请求参数 这是我的接口类,其中有类型为application/json的headers注释,并使用post方法和use调用,其中使用body注释具有param请求的模型 public interface ApiStorePhotoInterface { @Headers("Content-Type: application/json") @POST("photo/st/v7") Call&

如何在MainActivity的调用中为json类型参数设置请求参数

这是我的接口类,其中有类型为application/json的headers注释,并使用post方法和use调用,其中使用body注释具有param请求的模型

public interface ApiStorePhotoInterface {
        @Headers("Content-Type: application/json")
        @POST("photo/st/v7")
        Call<PhotoStoreMainModel> getResponse(@Body ParamModel paramModel);
    }
这是客户机类

客户端类-

这是改装生成器类

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??");
ApiStorePhotoInterface apinterface=ApiStorePhotoClient.getApi();
Call Call=apinterface.getResponse(“在这里写什么?”);
如何在getResponse()中的模型内发送请求

PhotostoreMain模型是响应模型。代码有3个响应模型,其中PhotostoreMain模型是其中之一,上面发布了2个请求参数模型

如何在getResponse()中Mainclass内的@body ParamModel中发送请求参数

如何在MainActivity类中设置通话线路

我期望得到结果,但由于请求参数,无法命中API


您可以像这样添加ArrayObject,更新后您可以使用日志检查您的请求:

        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 jsonObject1=新的JsonObject();
jsonObject1.addProperty(“apiKey”,“wseoagbiis5dd6vq5tfbs3r1c8rsz”);
jsonObject1.addProperty(“appver”,“1.00”);
jsonObject1.添加属性(“附加”、“团队”);
Global.printLog(“jsonObject1”,“==”+jsonObject1);
JsonArray productDetail1Array=新的JsonArray();
for(int i1=0;i1<1;i1++){
JsonObject productDetail1=新的JsonObject();
productDetail1.addProperty(“productId”,“6670002”);
JsonArray array1=新的JsonArray();
对于(int j=0;j<2;j++){
JsonObject imageDetails=新的JsonObject();
imageDetails.addProperty(“数量”,j+);
addProperty(“url”,“something.com/assets\u admin/uploads/frame\u image/…);
阵列1.添加(图像详细信息);
}
productDetail1.添加(“imageDetails”,第1列);
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();
    }
JSONObject JSONObject=new JSONObject();
试一试{
jsonObject.put(“键1”、“值1”);
jsonObject.put(“键2”,值2);
JsonParser JsonParser=新的JsonParser();
ApiStorePhotoInterface apiInterface=ApiStorePhotoClient.getApi();
**Call Call=apinterface.getResponse((JsonObject)jsonParser.parse(JsonObject.toString());//这不是解析json的正确方法。没有得到正确的命中率**
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
call.cancel();
t、 printStackTrace();
}
});
}捕获(JSONException e){
e、 printStackTrace();
}

无需添加
内容类型

更改
界面

public interface ApiStorePhotoInterface {


        @POST("photo/st/v7")
        Call<PhotoStoreMainModel> getResponse(@Body JsonObject obj);
    }
//最重要的是


在getResponse中传递上面的in对象

我不能使用此行getResponse((JsonObject)jsonParser.parse(JsonObject.toString());在我的代码中,我的接口有@Body ParamModelparamModel@AnkitGupta在接口内部进行更改,如调用getResponse(@Body JsonObject paramModel);为什么我们要使用JsonParser?它会将JSONObject转换为JSONObject在这个函数中没有得到响应--public void onResponse(调用,响应){PhotoStoreMainModel PhotoStoreMainModel=response.body();//PhotoStoreMainModel是我的响应模型}我的接口正确吗?调用=apiInterface.getResponse((JSONObject)parse(jsonObject.toString().trim());给errorJsonObject这是不支持的。第二个参数类型错误。找到:“java.lang.String”,必需:“com.google.gson.JsonElement”,但JSONObject支持
 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();
    }
public interface ApiStorePhotoInterface {


        @POST("photo/st/v7")
        Call<PhotoStoreMainModel> getResponse(@Body JsonObject obj);
    }
JsonObject json = new JsonObject();
json.addProperty("affId", id_value)