2017-07-31 61 views
1

特殊字符在我目前的项目中,我对“和‘=一些问题’符号的URL。改造与URL

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

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

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

我怎么能做出这样的请求?

谢谢!

+0

有一些约定,你应该尊重。您最终的网址应该采用以下格式:'http://mysampledomain.com/login?userlogin = sample&param2 = value2'(您可以根据需要添加或删除参数) – MatPag

+0

您想通过Retrofit进行GET请求吗? –

+0

是的,我知道,但后端是以这种方式开发的,不能改变。有什么方法可以对这些服务进行改造吗? – asanchezyu

回答

1

我找到了解决办法。

的人,它可以帮助,我已经使用了@url注释是这样的:

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

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

1

正常情况下,通过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. 
}); 
+0

是的,我知道几乎所有的服务都不适用于这种模式,但我必须使我的应用程序适用于这类服务。 :( – asanchezyu

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

和基本URL如:

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

问题是,URL需要像这样的报价和等号: http://mysampledomain.com/login?q='userlogin'="sample“ 我的API需要这种模式,我不能去解决吧。 – asanchezyu

+0

可能是那个api的URL错了 –

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