导入包
implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
加入网络权限
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
嗯。。第二个忘了是不是必须加的了,偷个懒先不测试了嘻嘻
接口 HttpService
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;
public interface HttpService {
//个人推测,retrofit2在找到这个接口时,自动把这个当成接口地址的根目录,且以/结尾
String BASE_URL = "http://xxx/xxx/xxx/";
@Headers({
"version:1.11"
})
@POST("captcha")
Call<ResultCallbackReply> getResultCallback(@Body RequestBody body);
}
@Headers里放服务器需要的参数,若只有一个参数可以使用@Headers("version:1.11"