|
|
|
|
@ -27,18 +27,20 @@ import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
|
public class ApiService {
|
|
|
|
|
private static final String TAG = "ApiService";
|
|
|
|
|
private static final String BASE_URL = "http://10.0.2.2:8080/SLMS/api/"; // 模拟器访问本机地址
|
|
|
|
|
|
|
|
|
|
private static ApiService instance;
|
|
|
|
|
private Retrofit retrofit;
|
|
|
|
|
private LibraryApi libraryApi;
|
|
|
|
|
private Gson gson;
|
|
|
|
|
|
|
|
|
|
private static final String STATUS_AVAILABLE = "available";
|
|
|
|
|
private static final String STATUS_BORROWED = "borrowed";
|
|
|
|
|
|
|
|
|
|
private static volatile ApiService instance;
|
|
|
|
|
private final Retrofit retrofit;
|
|
|
|
|
private final LibraryApi libraryApi;
|
|
|
|
|
private final Gson gson;
|
|
|
|
|
|
|
|
|
|
private ApiService() {
|
|
|
|
|
// 配置Gson
|
|
|
|
|
gson = new GsonBuilder()
|
|
|
|
|
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
|
|
|
|
|
.create();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 配置OkHttpClient
|
|
|
|
|
OkHttpClient okHttpClient = new OkHttpClient.Builder()
|
|
|
|
|
.connectTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
@ -46,19 +48,19 @@ public class ApiService {
|
|
|
|
|
.writeTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
.addInterceptor(new HttpLoggingInterceptor())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 配置Retrofit
|
|
|
|
|
retrofit = new Retrofit.Builder()
|
|
|
|
|
.baseUrl(BASE_URL)
|
|
|
|
|
.client(okHttpClient)
|
|
|
|
|
.addConverterFactory(GsonConverterFactory.create(gson))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
libraryApi = retrofit.create(LibraryApi.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "ApiService实例已创建");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取单例实例
|
|
|
|
|
* @return ApiService单例实例
|
|
|
|
|
@ -69,7 +71,7 @@ public class ApiService {
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取所有图书
|
|
|
|
|
* @param callback 回调接口
|
|
|
|
|
@ -80,14 +82,20 @@ public class ApiService {
|
|
|
|
|
try {
|
|
|
|
|
// 模拟网络延迟
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟返回数据
|
|
|
|
|
List<Book> books = getMockBooks();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 在主线程回调
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onSuccess(books);
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
Log.e(TAG, "获取图书被中断", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onFailure("获取图书被中断: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "获取图书失败", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
@ -96,7 +104,7 @@ public class ApiService {
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取所有借阅记录
|
|
|
|
|
* @param callback 回调接口
|
|
|
|
|
@ -107,14 +115,20 @@ public class ApiService {
|
|
|
|
|
try {
|
|
|
|
|
// 模拟网络延迟
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟返回数据
|
|
|
|
|
List<Loan> loans = getMockLoans();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 在主线程回调
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onSuccess(loans);
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
Log.e(TAG, "获取借阅记录被中断", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onFailure("获取借阅记录被中断: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "获取借阅记录失败", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
@ -123,7 +137,7 @@ public class ApiService {
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加图书
|
|
|
|
|
* @param book 要添加的图书
|
|
|
|
|
@ -135,11 +149,17 @@ public class ApiService {
|
|
|
|
|
try {
|
|
|
|
|
// 模拟网络延迟
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 在主线程回调
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onSuccess(book);
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
Log.e(TAG, "添加图书被中断", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onFailure("添加图书被中断: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "添加图书失败", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
@ -148,7 +168,7 @@ public class ApiService {
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新图书
|
|
|
|
|
* @param book 要更新的图书
|
|
|
|
|
@ -160,11 +180,17 @@ public class ApiService {
|
|
|
|
|
try {
|
|
|
|
|
// 模拟网络延迟
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 在主线程回调
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onSuccess(book);
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
Log.e(TAG, "更新图书被中断", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onFailure("更新图书被中断: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "更新图书失败", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
@ -173,7 +199,7 @@ public class ApiService {
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除图书
|
|
|
|
|
* @param bookId 要删除的图书ID
|
|
|
|
|
@ -185,11 +211,17 @@ public class ApiService {
|
|
|
|
|
try {
|
|
|
|
|
// 模拟网络延迟
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 在主线程回调
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onSuccess(true);
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
Log.e(TAG, "删除图书被中断", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onFailure("删除图书被中断: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "删除图书失败", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
@ -198,7 +230,7 @@ public class ApiService {
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加借阅记录
|
|
|
|
|
* @param loan 要添加的借阅记录
|
|
|
|
|
@ -210,11 +242,17 @@ public class ApiService {
|
|
|
|
|
try {
|
|
|
|
|
// 模拟网络延迟
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 在主线程回调
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onSuccess(loan);
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
Log.e(TAG, "添加借阅记录被中断", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onFailure("添加借阅记录被中断: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "添加借阅记录失败", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
@ -223,7 +261,7 @@ public class ApiService {
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新借阅记录
|
|
|
|
|
* @param loan 要更新的借阅记录
|
|
|
|
|
@ -235,11 +273,17 @@ public class ApiService {
|
|
|
|
|
try {
|
|
|
|
|
// 模拟网络延迟
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 在主线程回调
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onSuccess(loan);
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
Log.e(TAG, "更新借阅记录被中断", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onFailure("更新借阅记录被中断: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "更新借阅记录失败", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
@ -248,7 +292,7 @@ public class ApiService {
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除借阅记录
|
|
|
|
|
* @param loanId 要删除的借阅记录ID
|
|
|
|
|
@ -260,11 +304,17 @@ public class ApiService {
|
|
|
|
|
try {
|
|
|
|
|
// 模拟网络延迟
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 在主线程回调
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onSuccess(true);
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
Log.e(TAG, "删除借阅记录被中断", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
callback.onFailure("删除借阅记录被中断: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "删除借阅记录失败", e);
|
|
|
|
|
if (callback != null) {
|
|
|
|
|
@ -273,24 +323,24 @@ public class ApiService {
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取模拟图书数据
|
|
|
|
|
* @return 模拟图书列表
|
|
|
|
|
*/
|
|
|
|
|
private List<Book> getMockBooks() {
|
|
|
|
|
List<Book> books = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加一些模拟图书数据
|
|
|
|
|
books.add(new Book("1", "Java编程思想", "Bruce Eckel", "9787111213826", "计算机", "available"));
|
|
|
|
|
books.add(new Book("2", "设计模式", "Erich Gamma", "9787111075756", "计算机", "available"));
|
|
|
|
|
books.add(new Book("3", "重构", "Martin Fowler", "9787115508645", "计算机", "borrowed"));
|
|
|
|
|
books.add(new Book("4", "代码整洁之道", "Robert C. Martin", "9787115216878", "计算机", "available"));
|
|
|
|
|
books.add(new Book("5", "算法导论", "Thomas H. Cormen", "9787111407928", "计算机", "borrowed"));
|
|
|
|
|
|
|
|
|
|
books.add(new Book("1", "Java编程思想", "Bruce Eckel", "9787111213826", "计算机", STATUS_AVAILABLE));
|
|
|
|
|
books.add(new Book("2", "设计模式", "Erich Gamma", "9787111075756", "计算机", STATUS_AVAILABLE));
|
|
|
|
|
books.add(new Book("3", "重构", "Martin Fowler", "9787115508645", "计算机", STATUS_BORROWED));
|
|
|
|
|
books.add(new Book("4", "代码整洁之道", "Robert C. Martin", "9787115216878", "计算机", STATUS_AVAILABLE));
|
|
|
|
|
books.add(new Book("5", "算法导论", "Thomas H. Cormen", "9787111407928", "计算机", STATUS_BORROWED));
|
|
|
|
|
|
|
|
|
|
return books;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取模拟借阅数据
|
|
|
|
|
* @return 模拟借阅记录列表
|
|
|
|
|
|