增加Retrofit+OkHttp 响应式网络请求框架

This commit is contained in:
BA7LZD 2020-03-11 16:20:39 +08:00
parent af06bcc1d7
commit d88443173f
5 changed files with 108 additions and 6 deletions

View File

@ -28,11 +28,14 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.7.1'
implementation 'com.squareup.okhttp:okhttp:2.7.5'
implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
}

View File

@ -0,0 +1,5 @@
package com.yuxihan.sdu;
public class Constant {
public static final String BASE_URL = "http://www.yuxihan.com/";
}

View File

@ -1,17 +1,45 @@
package com.yuxihan.sdu.data;
import android.util.Log;
import com.yuxihan.sdu.Constant;
import com.yuxihan.sdu.data.model.DataBean;
import com.yuxihan.sdu.data.model.LoggedInUser;
import java.io.IOException;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Class that handles authentication w/ login credentials and retrieves user information.
*/
public class LoginDataSource {
public Result<LoggedInUser> login(String username, String password) {
public Result<LoggedInUser> login(final String username, String password) {
try {
Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()).build();
UpdateService updateService = retrofit.create(UpdateService.class);
Call<DataBean> call = updateService.login(username,password);
call.enqueue(new Callback<DataBean>() {
@Override
public void onResponse(Call<DataBean> call, Response<DataBean> response) {
//请求成功返回是一个封装为DataBean的响应
String result = response.body().toString();
Log.e("TAG","Url ===== "+result);
}
@Override
public void onFailure(Call<DataBean> call, Throwable t) {
//请求失败
}
});
return new Result.Success<>(new LoggedInUser(username,username));
/*try {
// TODO: handle loggedInUser authentication
LoggedInUser fakeUser =
new LoggedInUser(
@ -20,7 +48,7 @@ public class LoginDataSource {
return new Result.Success<>(fakeUser);
} catch (Exception e) {
return new Result.Error(new IOException("Error logging in", e));
}
}*/
}
public void logout() {

View File

@ -0,0 +1,28 @@
package com.yuxihan.sdu.data;
import com.yuxihan.sdu.data.model.DataBean;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;
public interface UpdateService {
@POST("addUser")
Call<DataBean> addUser(
//内部是请求的参数
@Query("userName") String userName,
@Query("password") String password
);
@POST("login")
Call<DataBean> login(
//内部是请求的参数
@Query("userName") String userName,
@Query("password") String password
);
@GET("hi")
Call<DataBean> hi(
);
}

View File

@ -0,0 +1,38 @@
package com.yuxihan.sdu.data.model;
public class DataBean {
/**
* errCode : 1
* errMsg :
* result :
*/
private String errCode;
private String errMsg;
private String result;
public String getErrCode() {
return errCode;
}
public void setErrCode(String errCode) {
this.errCode = errCode;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}