android - Retrofit2 替换查询参数

标签 android retrofit2 url-encoding okhttp

我正在使用 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 
                    }
                }
        );
    }

My mobile number is having + symbol at the beginning. From the retrofit logs, I can see this is converted like mobile=%2B11111111111 while sending the request.

I am expecting encoding and making input parameter like 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

它按预期工作

最佳答案

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

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

关于android - Retrofit2 替换查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41892723/

相关文章:

android - Firebase 应用程序启动时间分析如何在 Android 上工作?

安卓。 Activity 启动时自动聚焦 TextView

java - 错误 : Entities and Pojos must have a usable public constructor.

android - 在 Jetpack Compose 中的 API 响应后更新 LazyColumn

java - Java中的URL解码日语字符等

Android Dagger 2 POJO字段注入(inject)空

android - 使用 ActionBarSherlock/HoloEverywhere Light 的对话框

android - 显示上传图片进度 Multipart

javascript - Google App Script使用参数向另一个脚本发出get请求

ios - URLPathAllowedCharacterSet 不编码感叹号