简体   繁体   English

android改造嵌入式查询参数

[英]android retrofit embedded query parameters

Android How to pass embedded query parameters using Retrofit... This is my GET METHOD Url: Android如何使用翻新来传递嵌入式查询参数...这是我的GET METHOD网址:

localhost:5000/at_invitations?embedded={"invitee":1}

My Question is how to pass the embedded value {"invitee":1} as query parameter using Retrofit.. 我的问题是如何使用Retrofit将嵌入值{"invitee":1}作为查询参数传递。

This is My Service Call declaration Code : 这是我的服务电话声明代码:

@GET("at_invitations")
Call<JsonElement> invitationLists(
        @Header("Authorization") String token,
        @Query("embedded") String embedded

);

This My Service Call Code : 我的服务电话代码:

String token = App.getInstance().getToken();
    AppearancesService service = App.getInstance().getRESTClient().create(AppearancesService.class);
    Call<JsonElement> call = service.invitationLists(token, "{invitee:1}");

    call.enqueue(new Callback<JsonElement>() {
        @Override
        public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
            if (response.code() == 200) {
                Log.v(TAG,"Success"+ response.body(););
                }
            }
        }

        @Override
        public void onFailure(Call<JsonElement> call, Throwable t) {
            System.out.println("Failed");
        }
    });

This is error Line :(How to declare this value( "{"invitee":1}" )) 这是错误行:(如何声明此值( "{"invitee":1}" ))

Call<JsonElement> call = service.invitationLists(token, "{"invitee":1}");

Please help me.. Thanks.. 请帮助我..谢谢..

try this out 试试这个

JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("invite",1);
            String a=jsonObject.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }

here string "a" will have the format you want and your call should be like this 此处的字符串“ a”将具有您想要的格式,您的通话应如下所示

@GET("at_invitations?")

Hello Try this if it helps 您好,如果有帮助,请尝试此

your Service Interface 您的服务界面

@Headers("Content-Type: application/json")
@POST("index.php")
Call<Model>getUserDetails(@QueryMap Map<String,JSONObject> stringTaskMap);

Method to get details 获取详细信息的方法

 public void getDetails(Map<String, JSONObject> task) {
        ServiceInterfaceApi interfaceApi = ServiceClass.getApiService();
        Call<Model> call = interfaceApi.getUserDetails(task);
        Log.v("@@@WWE", "Retrofit Request Method =  " + call.request().method());
        Log.v("@@@WWE", "Retrofit Request Body =  " + call.request().body());
        Log.v("@@@WWE", "Retrofit Request Url = " + call.request().url());
        Log.v("@@@WWE", "Retrofit Request executed = " + call.isExecuted());
        Log.v("@@@WWE", "Retrofit Request Headers= " + call.request().headers());
        call.enqueue(new Callback<Model>() {
            @Override
            public void onResponse(Call<Model> call, Response<Model> response) {
                Log.v("@@@", "Response");
                if (response.isSuccessful()) {
                    Log.v("@@@", "Sucess");
                }
            }

            @Override
            public void onFailure(Call<Model> call, Throwable t) {
                Log.v("@@@WWE", "Failure " + t.getMessage());
            }
        });
    }

Calling the method where required 在需要的地方调用方法

JSONObject taskM = new JSONObject();
        try {
            taskM.put("invitee", "1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Map<String, JSONObject> taskMap = new HashMap<>();
        taskMap.put("embedded", taskM);
        getDetails(taskMap);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM