準(zhǔn)備工作
在編寫代碼前,需要先弄清楚登錄請(qǐng)求的基本信息,利用PostMan確認(rèn)接口是可用的,再開始編寫代碼。登錄接口需要的信息如下:
1、接口的URL:https://xx/xx/login
2、接口請(qǐng)求的方式: POST

3、需要的參數(shù):用戶名、密碼
4、服務(wù)器返回的數(shù)據(jù)格式:Boolean(注意:不同的服務(wù)器返回的值是不一樣的,我們這里的例子返回的是Boolean)
在拿到上面這些信息后,可以在PostMan進(jìn)行配置&驗(yàn)證。下圖是登錄成功后的運(yùn)行結(jié)果:

如果輸入一個(gè)錯(cuò)誤的密碼,服務(wù)器會(huì)返回false

如果Reponse窗口中,沒有返回正確的結(jié)果,說明你拿到的接口信息是有問題的

代碼實(shí)現(xiàn)
Retrofit版本
1、在app/build.gradle中加入依賴
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
2、定義請(qǐng)求的接口LoginAPI
public interface LoginAPI {
@POST("login")
Call<Boolean> login(@Field("username") String name, @Field("password") String password);
}
@POST表示請(qǐng)求方式,傳入的參數(shù)login表示url中的method,url前面的那一串在創(chuàng)建Retrofit類時(shí)會(huì)進(jìn)行設(shè)置,后面會(huì)講到一個(gè)
Call類型的方法表示一個(gè)請(qǐng)求,返回值為Boolean類型登錄請(qǐng)求需要2個(gè)參數(shù),使用
@Field設(shè)置字段的名稱,具體參數(shù)值,在調(diào)用login方法時(shí)傳入-
注意: 如果運(yùn)行時(shí)遇到錯(cuò)誤:
@Field parameters can only be used with form encoding. (parameter #1)需要加上
@FormUrlEncoded注解public interface LoginAPI { @FormUrlEncoded @POST("login") Call<Boolean> login(@Field("username") String name, @Field("password") String password); }
3、創(chuàng)建retrofit類
其中的BASE_URL就是https://xx/xx/login的前面這一串:https://xx/xx/
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
4、創(chuàng)建LoginAPI
final LoginAPI loginAPI = retrofit.create(LoginAPI.class);
5、創(chuàng)建請(qǐng)求
Call<Boolean> call = loginAPI.login(name, password);
6、執(zhí)行請(qǐng)求
call.enqueue(new Callback<Boolean>() {
@Override
public void onResponse(Call<Boolean> call, Response<Boolean> response) {
Log.i(TAG, "onResponse: " + response.body());
}
@Override
public void onFailure(Call<Boolean> call, Throwable t) {
Log.w(TAG, "onFailure: " + t.getMessage() );
}
});
7、AndroidManifest.xml中加入權(quán)限
<uses-permission android:name="android.permission.INTERNET"/>
8、運(yùn)行程序,如果遇到crash報(bào)錯(cuò):java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method。需要在app/build.gradle加入如下配置:
compileOptions {
targetCompatibility = "8"
sourceCompatibility = "8"
}
9、查看執(zhí)行結(jié)果
-
輸入正確的用戶名和密碼:
2021-03-18 12:07:10.693 8804-8804/com.example.retrofitdemo01 I/RetrofitDemo: onResponse: true -
輸入一個(gè)錯(cuò)誤的用戶名或密碼
2021-03-18 12:07:48.545 8906-8906/com.example.retrofitdemo01 I/RetrofitDemo: onResponse: false -
輸入的url有問題
2021-03-18 12:08:15.842 9006-9006/com.example.retrofitdemo01 W/RetrofitDemo: onFailure: Unable to resolve host "xx": No address associated with hostname
Retrofit + RxJava2.0 版本
1、在app/build.gradle中加入依賴
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.21'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
2、定義請(qǐng)求的接口RxLoginAPI
public interface RxLoginAPI {
@FormUrlEncoded
@POST("login")
Observable<Response<Boolean>> login(@Field("username") String name, @Field("password") String password);
}
3、創(chuàng)建Retrofit類
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
4、創(chuàng)建請(qǐng)求&調(diào)用
retrofit.create(RxLoginAPI.class)
.login(name, password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Response<Boolean>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Response<Boolean> booleanResponse) {
Log.i(TAG, "onNext: " + booleanResponse.body());
}
@Override
public void onError(Throwable e) {
Log.w(TAG, "onError: " + e.getMessage());
}
@Override
public void onComplete() {
}
});
5、查看運(yùn)行結(jié)果
-
輸入正確的用戶名和密碼
2021-03-18 12:41:47.299 9434-9434/com.example.retrofitdemo01 I/RetrofitDemo: onNext: true -
輸入錯(cuò)誤的用戶名或密碼
2021-03-18 12:43:31.643 9552-9552/com.example.retrofitdemo01 I/RetrofitDemo: onNext: false -
輸入錯(cuò)誤的URL
2021-03-18 12:44:09.265 9655-9655/com.example.retrofitdemo01 W/RetrofitDemo: onError: Unable to resolve host "xx": No address associated with hostname