簡體   English   中英

在網址上使用特殊字符進行改造

[英]Retrofit with special chars at url

在當前的項目中,我對URL中的“和” =“符號有一些疑問。

http://mysampledomain.com/login?q='userlogin'= “ sample”

我已經嘗試了一切。 “ @Path”不起作用,當我嘗試“ @Query”時,字符“ =”和“”更改為ASCII,並且不起作用。

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

我如何提出這種要求?

謝謝!

我找到了解決方案。

對於任何可能有所幫助的人,我都使用了@Url注釋,如下所示:

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

感謝所有回答我的問題的人:)

通常,通過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.
});

使用這種方法

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

檢查您的api網址(如果您的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/";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM