Android Studio中Retrofit调用Spring Boot PUT更新接口失败求助
密码管理器PUT接口Android调用失效问题
我正在开发一款基于MySQL数据库与Spring Boot后端的密码管理器应用。Spring Boot后端接口经Postman测试完全正常,Android Studio中用Retrofit调用大部分接口也能正常工作,但updateAccountById(PUT请求)接口在Postman中能正常运行,在Android应用里却无法生效,找不到问题原因。
后端代码
AccountDao 代码
public class AccountDao { @Autowired private AccountRepository accountRepository; /** 创建新账户 **/ public Account save(Account account){ return accountRepository.save(account); } /** 删除账户 **/ public void deleteById(int id){ accountRepository.deleteById(id); } /** 获取所有账户 **/ public List<Account> getAllAccounts() { List<Account> accounts = new ArrayList<>(); Streamable.of(accountRepository.findAll()).get().forEach(accounts::add); return accounts; } /** 按分类获取账户 **/ public List<Account> getAccountsByCategory(int id){ List<Account> accountsByCategory = new ArrayList<>(); Streamable.of(accountRepository.findAll()).get().filter(account -> account.getCategoryId() == id).forEach(accountsByCategory::add); return accountsByCategory; } /** 按ID获取账户 **/ public Account getAccountById(int id){ return accountRepository.findById(id).get(); } /** 获取收藏账户 **/ public List<Account> getFavoriteAccounts(){ List<Account> accounts = new ArrayList<>(); Streamable.of(accountRepository.findAll()).get().filter(Account::getFavorite).forEach(accounts::add); return accounts; }
Controller 中的REST接口代码
public class Controller { /** DAO初始化 **/ @Autowired CategoryDao categoryDao = new CategoryDao(); @Autowired AccountDao accountDao = new AccountDao(); /** REST路由 **/ /** 分类路由 **/ /* 按ID获取单个分类 */ @GetMapping("/category/{id}") public Category getCategoryById(@PathVariable int id) { return categoryDao.getCategoryById(id); } /* 获取所有分类 */ @GetMapping("/categories") public List<Category> getCategories(){ return categoryDao.getAllCategory(); } /* 创建新分类 */ @PostMapping("/category/save") public Category newCategory(@RequestBody Category category){ return categoryDao.save(category); } /* 删除分类 */ @DeleteMapping("/category/delete/{categoryId}") public void delCategory(@PathVariable int categoryId){ categoryDao.deleteById(categoryId); } /** 账户路由 **/ /* 按分类获取账户 */ @GetMapping("accounts/{categoryId}") public List<Account> getCategory(@PathVariable int categoryId){ return accountDao.getAccountsByCategory(categoryId); } /* 按ID获取单个账户 */ @GetMapping("/account/{accountId}") public Account getAccount(@PathVariable int accountId){ return accountDao.getAccountById(accountId); } @GetMapping("accounts/favorite") public List<Account> favorites(){ return accountDao.getFavoriteAccounts(); } /* 创建新账户 */ @PostMapping("/account/save") public Account newAccount(@RequestBody Account account){ return accountDao.save(account); } /* 编辑账户 */ @PutMapping("/update/{accountId}") public ResponseEntity<Account> updateAccount(@PathVariable int accountId, @RequestBody Account accountDetails){ Account account = accountDao.getAccountById(accountId); account.setCategoryId(accountDetails.getCategoryId()); account.setName(accountDetails.getName()); account.setUserName(accountDetails.getUserName()); account.setPassword(accountDetails.getPassword()); account.setUrl(accountDetails.getUrl()); account.setFavorite(accountDetails.getFavorite()); Account updateAccount = accountDao.save(account); return ResponseEntity.ok().body(updateAccount); } /* 删除账户 */ @DeleteMapping("/account/delete/{accountId}") public void delAccount(@PathVariable int accountId){ accountDao.deleteById(accountId); }
Android端代码
Retrofit的AccountApi接口代码
public interface AccountApi { @GET("/account/{categoryId}") Call<Account> getAccount(@Path("categoryId") int id); @GET("/accounts/{categoryId}") Call<List<Account>> getAccountsByCategory(@Path("categoryId") int id); @GET("/accounts/favorite") Call<List<Account>> getFavoriteAccounts(); @POST("/account/save") Call<Account> saveAccount(@Body Account account); @PUT("/update/{accountId}") Call<Account> updateAccount(@Path("accountId") int id, @Body Account account); @DELETE("/account/delete/{id}") Call<ResponseBody> deleteAccount(@Path("id") int id); }
OnClickListener中的调用代码(获取账户部分)
accountApi.getAccount(getArguments().getInt("accountID")).enqueue(new Callback<Account>() { @Override public void onResponse(Call<Account> call, Response<Account> response) { edit_name.setText(response.body().getName()); edit_username.setText(response.body().getUserName()); edit_password.setText(response.body().getPassword()); edit_url.setText(response.body().getUrl()); edit_favorite.setChecked(response.body().getFavorite()); } @Override public void onFailure(Call<Account> call, Throwable t) { } });
内容的提问来源于stack exchange,提问作者Lucas Schaffner
相关产品推荐
相关产品推荐

