android改造嵌入式查询参数

时间:2017-05-27 06:33:05

标签: java android rest retrofit retrofit2

Android如何使用Retrofit传递嵌入式查询参数...这是我的GET METHOD网址:

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

我的问题是如何使用Retrofit将嵌入值{"invitee":1}作为查询参数传递..

这是我的服务电话申报代码:

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

);

此我的服务电话代码:

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");
        }
    });

这是错误行:(如何声明此值("{"invitee":1}"))

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

请帮帮我..谢谢..

2 个答案:

答案 0 :(得分:0)

试试这个

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

这里是字符串&#34; a&#34;将具有您想要的格式 你的电话应该是这样的

@GET("at_invitations?")

答案 1 :(得分:0)

你好尝试这个,如果它有帮助

您的服务界面

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

获取详细信息的方法

 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());
            }
        });
    }

根据需要调用方法

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);