在网址上使用特殊字符进行改造

时间:2017-07-31 08:52:30

标签: java android retrofit okhttp okhttp3

在我目前的项目中,我有一些关于URL的“和”=“符号的问题。

http://mysampledomain.com/login?q= '用户登陆'= “样品”

我已经尝试了一切。 “@Path”不起作用,当我尝试“@Query”时,字符“=”和“”被更改为ASCII并且它不起作用。

http://mysampledomain.com/login?q%3D%userlogin%27=%22sample%22

我怎么能提出这样的要求?

谢谢!

4 个答案:

答案 0 :(得分:3)

我找到了解决方案。

对于任何有帮助的人,我都使用了@Url这样的注释:

@GET
Call<ApiResponseModel> getUserDetails(@Header("Authorization") String token, @Url String fullUrl);

感谢所有回复我问题的人:)

答案 1 :(得分:2)

通常,通过url的参数以不同的方式传递。在你的情况下,它将是:

http://mysampledomain.com/login?userlogin=sample&otherVariable=otherValue

通常没有''字符。

例如,我使用node.js,所以它就像:

app.get('/login', function(req, res) {
 var userLogin = req.query.userLogin;
 var otherVariable = req.query.otherVariable;

 // Do whatever you want.
});

答案 2 :(得分:1)

使用此方法

public static final String BASE_URL + "http://mysampledomain.com/";

public interface RetrofitClient{

     @GET("login")
     Call<String> login(@QueryMap Map<String, String> map);

}

调用此

Map<String, String> map = new HashMap<>();
map.put("userlogin", "sample");
map.put("param2", "param2");

OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();

Gson gson = new GsonBuilder()
    .setLenient()
    .create();

Retrofit  r = new Retrofit.Builder()
       .baseUrl(BASE_URL)
       .addConverterFactory(ScalarsConverterFactory.create())
       .addConverterFactory(GsonConverterFactory.create(gson))
       .client(okBuilder.build())
       .build();

r.create(RetrofitClient.class).login(map).enqueue(this);

答案 3 :(得分:0)

检查你的api url,如果你的api喜欢

http://mysampledomain.com/login?userlogin=sample&otherVariable=otherValue

然后

  @GET("login")
  Observable<LoginResponse> getUserProductViewed(@Query("userlogin") String userlogin,@Query("otherVariable")String otherVariable);

和基本网址如:

public static String BASE_URL = "http://mysampledomain.com/";
相关问题