2022-12-10 遇到的一點小問題
之前的網(wǎng)絡(luò)請求方法是從網(wǎng)上找到,先設(shè)置baseUrl,然后設(shè)置路徑和請求參數(shù)。
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(StringConverterFactory.create())
.client(new OkHttpClient.Builder())
.build();
AppInterface app = retrofit.create(AppInterface.class);
Call<String> call = app.getInfo("python/test/", map);
call.enqueue(callback);
public interface AppInterface {
@GET("{param}")
Call<String> getInfo(@Path("param") String param, @QueryMap Map<String, String> map);
@GET("php/test")
Call<String> getInfo(@QueryMap Map<String, String> map);
}
不過最近有個接口遇到錯誤,才知道使用@Path()去設(shè)置路徑,會自動編碼,比如“python/test/”會變成“python%2Ftest”。提前把路徑寫好,路徑太多又很麻煩。
網(wǎng)上找了下,看到是時候客觀評價Retrofit了,這幾點你必須明白,找到了答案,記錄一下。
public interface AppInterface {
@GET
Call<String> getInfo(@Url String url);
@GET
Call<String> getInfo(@Url String url, @QueryMap Map<String, String> map);
@POST
Call<String> postInfo(@Url String url, @Body RequestBody map);
}
不使用@Path(),使用@Url。

public @interface Url
試了下,有些參數(shù)少的。在提交時還可以 路徑+?+key=value 就可以,不用特地寫個map。
.
順便記錄一下,個別接口需要參數(shù)按順序提交的,可以把HashMap改成LinkedHashMap。