【问题标题】:Retrofit @Url doesnt override BaseUrl改造 @Url 不会覆盖 BaseUrl
【发布时间】:2020-06-22 22:11:03
【问题描述】:

您好,我正在尝试从一个特定的 api 调用中覆盖 baseUrl,当使用 @Url 作为 api 方法的参数时,它似乎不起作用。

下面是我的Api类方法

@POST
    fun getUserDetails(@Body body: request, @Url authUrl : String): Single<Response<ResponseData>>

调用和发出请求的代码

 private fun getApi(): Api {
        val gson = GsonBuilder().setLenient().create()
        val httpClient = myNetworkHelper.createHttpClient()
        val retrofit = Retrofit.Builder()
                .baseUrl("http://defaultBaseUrl")
                .client(httpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()

        return retrofit.create(Api::class.java)
    }

 override fun getUserDetails(someRequestData, "http://dynamic/getuserDetails): Single<Response<ResponseData>> {
        return getApi().getUserDetails(body, url)
    }

上面的结果是向这个url发出请求

http://defaultBaseUrl/http://dynamic/getuserDetails

代替:

http://dynamic/getuserDetails

【问题讨论】:

    标签: android kotlin network-programming retrofit2 restful-architecture


    【解决方案1】:

    我在本地进行了测试,实际上我已经能够覆盖在 Retrofit 实例中设置的基本 URL。

    API 如下:

    public interface Api {
    
        @POST
        Call<Void> fakeService(@Url String url);
    }
    

    这里是“客户”:

    public class Main {
    
        public static void main(String[] args) throws IOException {
            // Use the HttpLogginInterceptor to check what's the real call
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
    
            OkHttpClient client = new OkHttpClient.Builder()
                    .addNetworkInterceptor(interceptor)
                    .build();
    
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://www.foo.com/")
                    .client(client)
                    .build();
    
            Api api = retrofit.create(Api.class);
            Call<Void> call = api.fakeService("http://www.example.com");
            call.execute();
        }
    }
    

    结果是:

    Jul 18, 2018 12:28:54 PM okhttp3.internal.platform.Platform log
    INFO: --> POST http://www.example.com/ (0-byte body)
    Jul 18, 2018 12:28:55 PM okhttp3.internal.platform.Platform log
    INFO: <-- 200 OK http://www.example.com/ (366ms, unknown-length body)
    

    【讨论】:

      【解决方案2】:

      这是来自 Retrofit @GET 注释的文档:

      @Documented 
      @Target(METHOD) 
      @Retention(RUNTIME) 
      public @interface GET {   
      /**
      * A relative or absolute path, or full URL of the endpoint. This value is optional if the first
      * parameter of the method is annotated with {@link Url @Url}.
      * <p>    
      * See {@linkplain retrofit2.Retrofit.Builder#baseUrl(HttpUrl) base URL} for details of how    
      * this is resolved against a base URL to create the full endpoint URL.
      */   
          String value() default ""; 
      }
      

      所以@Url应该作为第一个参数,所以我认为这样可以:

      @POST
      fun getUserDetails(@Url authUrl: String, @Body body: request): Single<Response<ResponseData>>
      

      【讨论】:

      • 不幸的是,移动参数不起作用。它仍然包括基本网址
      • 您是否尝试在基本网址末尾添加/,例如"http://defaultBaseUrl/"
      • 你能提供任何文件吗?也许是真的,因为它对我有用,我的也是一个 GET 请求
      • @TamHuynh 在帖子中试试看会发生什么?
      • @jonney 它基于this comment
      猜你喜欢
      • 2018-09-26
      • 1970-01-01
      • 2016-12-12
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多