限制对http请求体的编码进行改造

时间:2018-04-13 09:44:25

标签: java android json retrofit

我正在尝试传递一个字符串作为HTTP POST REQUEST的主体但是改装编码我的身体以便

  

/成为%2F

:// becomes %3A%2F%2F。我最终得到一个实际上看起来像这样的字符串:

**

/TRA became %2FTRA
://www.sample.com became %3A%2F%2Fwww.sample.com

**

我如何重新训练到/ TRA和://www.sample.com

我的改造休息api定义如下: -

public interface NetworkCall {
    @FormUrlEncoded
        @POST(HTTPPOSTREQUEST)
        void httppostrequest(@Header("Content-Type") String content_type, @Header("Authorization") String authorization,
                             @Field("grant_type") String grant_type, @Field("code") String code,
                             @Field("realm") String realm, @Field("redirect_uri") String redirect_uri,
                             Callback<SmartPassResponse> callback);
} 

这就是我制作

的方式
  

改造实例

 public static NetworkCall getInstanceSmartPass() {
        RequestInterceptor requestInterceptor = new RequestInterceptor() {
            @Override
            public void intercept(RequestInterceptor.RequestFacade request) {
                request.addHeader("Content-Type", "application/x-www-form-urlencoded");
                request.addHeader("Authorization", "Basic YWJzaGVyOkhxfjJhajpkfWtrNGRm");
            }
        };

        if (REST_CLIENT == null) {
            OkHttpClient.Builder b = new OkHttpClient.Builder();
            b.readTimeout(20000, TimeUnit.MILLISECONDS);
            b.writeTimeout(20000, TimeUnit.MILLISECONDS);
            OkHttpClient client = b.build();
            Gson gson = new GsonBuilder().disableHtmlEscaping().create();
            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setLogLevel(RestAdapter.LogLevel.FULL)
                    .setRequestInterceptor(requestInterceptor)
                    .setEndpoint(SMARTPASSURL)
                    .setClient(new Ok3Client(client))
                    .setConverter(new GsonConverter(gson))
                    .build();
            REST_CLIENT = restAdapter.create(NetworkCall.class);
        }

0 个答案:

没有答案