如何在改造中传递内容类型x-www-form-urlencoded的原始字符串?

时间:2016-07-26 08:31:51

标签: android json urlencode retrofit2 android-webservice

     OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request.Builder requestBuilder = chain.request().newBuilder();
     requestBuilder.header("Content-Type", "application/x-www-form-urlencoded");
   requestBuilder.header("Accept", "text/json");
  requestBuilder.header("Authorization","Basic fh73hf78fhhf7at");     }).build();

 Retrofit retrofit = new    Retrofit.Builder().baseUrl(BASE_URL).client(client).addConverterFactory(GsonConv   erterFactory.create()).build();
    BetaAPI betaAPI = retrofit.create(BetaAPI.class);

所以在改造的界面 (params属于类型,“username = david& password = test123& scope = openid + email”)

  @POST("core/connect/userinfo")
   Call<ResponseBody> getLogin(@Body String params);

1 个答案:

答案 0 :(得分:2)

你可以这样做

@FormUrlEncoded
@POST("core/connect/userinfo")
Call<ResponseBody> getLogin(@Field("username") String username,@Field("password") String password, @Field("scope") String scope);

或使用fieldMap注释

@FormUrlEncoded
@POST("core/connect/userinfo")
Call<ResponseBody> getLogin(@FieldMap(encoded = true) Map<String, String> params);