使用Retrofit调用Laravel接口时遭遇IllegalArgumentException错误求助
问题解决:Retrofit 抛出 IllegalArgumentException 错误
你代码里的问题很明显:你定义了BASE_URL常量,但在Retrofit构建器的baseUrl()方法中,传入的是带引号的字符串"BASE_URL",而不是直接引用这个常量。这就导致Retrofit实际使用的URL是"BASE_URL"这个纯文本,完全不是你配置的http://tfg.test/,自然会触发缺少http/https协议的错误。
修正后的代码如下:
public class API { public static final String BASE_URL="http://tfg.test/"; private static Retrofit retrofit = null; public static Retrofit getApi(){ if (retrofit==null){ retrofit=new Retrofit.Builder() .baseUrl(BASE_URL) // 移除引号,直接引用常量 .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; } }
额外提醒两点:
- 保持
BASE_URL结尾的斜杠/,这能避免后续接口路径拼接时出现格式错误 - 确认
http://tfg.test/在你的测试设备上可以正常访问(比如用浏览器打开验证)
内容的提问来源于stack exchange,提问作者lewis04
相关产品推荐
相关产品推荐

