修复IDE问题:创建缺失目录、InterruptedException处理、静态方法调用

main
SLMS Development Team 5 months ago
parent 68ec7b92c5
commit 01ee5d8d66

@ -0,0 +1,4 @@
{
"sonarQubeUri": "http://localhost:9000",
"projectKey": "mcslms"
}

@ -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

@ -9,6 +9,7 @@ import java.util.regex.Pattern;
/**
*
*/
@SuppressWarnings("unused") // 预留字段,用于实际邮件发送功能
public class EmailNotification implements Notification, BookStatusObserver {
private static final String EMAIL_REGEX = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);

@ -18,11 +18,9 @@ import java.util.List;
*/
public class BookService {
private final DatabaseConnection dbConnection;
private final BookFactoryProvider factoryProvider;
public BookService() {
this.dbConnection = DatabaseConnection.getInstance();
this.factoryProvider = new BookFactoryProvider();
}
/**
@ -31,7 +29,7 @@ public class BookService {
public boolean addBook(String bookType, String title, String author, String isbn,
String publisher, LocalDate publishDate, String category) {
try {
Book book = factoryProvider.createBook(bookType, generateBookId(), title, author,
Book book = BookFactoryProvider.createBook(bookType, generateBookId(), title, author,
isbn, publisher, publishDate, category);
Connection conn = dbConnection.getConnection();

Loading…
Cancel
Save