在URL中使用特殊字符的Retrofit

3

有一些规定您应该尊重。您的最终网址应该是这样的: http://mysampledomain.com/login?userlogin=sample&param2=value2 (您可以根据需要添加或删除参数)。 - MatPag
你想通过Retrofit发起一个GET请求吗? - Samarth Kejriwal
是的,我知道,但后端是以那种方式开发的,无法更改。有没有办法让Retrofit与这些服务配合使用? - asanchezyu
4个回答

5
我找到了解决方案。
对于任何需要帮助的人,我使用了@Url注释,如下所示:
@GET
Call<ApiResponseModel> getUserDetails(@Header("Authorization") String token, @Url String fullUrl);

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

然后你将完整的URL传递给上述方法。 - Raja Jawahar

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.
});

是的,我知道几乎所有服务都不使用这种模式,但我必须让我的应用程序与这种服务一起工作。 :( - asanchezyu

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);

-1

如果你的API像这样,请检查你的API URL

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

然后

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

以及基本URL,例如:

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

问题在于URL需要像这样的引号和等号:http://mysampledomain.com/login?q='userlogin'="sample"我的API需要这种模式,但我无法解决它。 - asanchezyu
它不起作用。 - Aman Srivastava

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接