mirror of
https://e.coding.net/mcontext/mContext/sdu.git
synced 2024-09-18 02:24:54 +08:00
增加Retrofit+OkHttp 响应式网络请求框架
This commit is contained in:
parent
af06bcc1d7
commit
d88443173f
@ -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'
|
||||
}
|
||||
|
5
app/src/main/java/com/yuxihan/sdu/Constant.java
Normal file
5
app/src/main/java/com/yuxihan/sdu/Constant.java
Normal file
@ -0,0 +1,5 @@
|
||||
package com.yuxihan.sdu;
|
||||
|
||||
public class Constant {
|
||||
public static final String BASE_URL = "http://www.yuxihan.com/";
|
||||
}
|
@ -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() {
|
||||
|
28
app/src/main/java/com/yuxihan/sdu/data/UpdateService.java
Normal file
28
app/src/main/java/com/yuxihan/sdu/data/UpdateService.java
Normal 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(
|
||||
|
||||
);
|
||||
}
|
38
app/src/main/java/com/yuxihan/sdu/data/model/DataBean.java
Normal file
38
app/src/main/java/com/yuxihan/sdu/data/model/DataBean.java
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user