【问题标题】:Retrofit2 Replacing query parametersRetrofit2 替换查询参数
【发布时间】:2017-01-27 19:49:45
【问题描述】:

我正在使用Retrofit2 并发送带有输入参数的请求,如下所示。但是改造会自动将 + 符号转换为 %2B。如何对此进行编码并作为 + 本身发送

相关代码

1) 接口

@POST("/registrationapi.php")
    Call<RegistrationPOJO> registrationResponse(@Query("firstname") String firstname , @Query("lastname") String lastname,
                                                @Query("email") String email, @Query("password") String password,
                                                @Query("uid") String uid, @Query("mobile") String mobile,
                                                @Query("key") String key
    );

2) RestClient

private APIInterface service;

public RestClient() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(AppConfiguration.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build();
        service = retrofit.create(APIInterface.class);
    }
public void getRegistrationInfo(final Registration context, String firstname, String lastname, String email,
                                    String password, String uid, String mobile, String key
                                    ){
        Call<RegistrationPOJO> reg =service.registrationResponse(firstname,lastname,email,password,uid,mobile,key);
        reg.enqueue(
                new Callback<RegistrationPOJO>() {

                    @Override
                    public void onResponse(Call<RegistrationPOJO> call, Response<RegistrationPOJO> response) {
                        success = response.isSuccessful();

                        if(success) {

                            //Handle success flow 
                        } else {
                            //Handle error flow 
                        }
                    }
                    @Override
                    public void onFailure(Call<RegistrationPOJO> call, Throwable t) {
                        //Handle error flow 
                    }
                }
        );
    }

我的手机号码开头有+ 符号。来自 改造日志,我可以看到这是转换为 mobile=%2B11111111111 发送请求时。

我期待编码并使输入参数像 mobile=+11111111111

对应的gradle依赖是

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'

按照 anurag 的建议。我已将参数更改为

@Query(value = "mobile" , encoded=true) String mobile

它按预期工作

【问题讨论】:

    标签: android retrofit2 url-encoding okhttp


    【解决方案1】:

    尝试在查询参数中使用encoded = true

    Call<ResponseBody> method(@Query(value = "+11111111111", encoded = true) String mobile) {
            .....
          }
    

    【讨论】:

    • 如何根据您的回答修改此语句? RestClient().getRegistrationInfo(this,firstname,lastname,email,password,uid,mobile,key);此外,这种硬编码可能吗? As Call 将在 interfce 中
    • @Stallion 上述编码 = true 的解决方案是否有效?
    • @Stallion 目前我正在设置我的系统,因为我以前的笔记本电脑已损坏。尝试使用 @Query("firstname", encoded = true ) String firstname
    • 谢谢哥们,它成功了。我已经在接口@Query(value = "mobile" , encoded=true) String mobile 中这样修改
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2022-08-04
    • 1970-01-01
    • 2020-07-12
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多