实现了加入书架的功能
package Servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import dao.dao; import entity.Book; import entity.Bookshelf; import net.sf.json.JSONObject; public class AddBookshelfServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=utf-8"); String bookname =request.getParameter("bookName"); String username =request.getParameter("username"); Bookshelf bookshelf =new Bookshelf(bookname,username); dao dao =new dao(); PrintWriter out = response.getWriter(); JSONObject json=new JSONObject(); if(dao.addshelf(bookshelf)) { json.put("result", 1); out.print(json); }else { json.put("result", 0); out.print(json); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } AddBookshelfServlet.java
package com.example.fooks.utils; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.TextView; import androidx.recyclerview.widget.RecyclerView; import com.example.fooks.R; import com.example.fooks.entity.Book; import com.example.fooks.entity.Bookshelf; import java.util.List; public class ShopsAdapter extends BaseAdapter { private List<Book> mList; private Context mContext; private LayoutInflater mLayoutInflater; public ShopsAdapter(List<Book> mList, Context mContext) { this.mList = mList; this.mContext = mContext; this.mLayoutInflater = LayoutInflater.from(mContext); } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if(convertView==null) { viewHolder= new ViewHolder(); convertView=mLayoutInflater.inflate(R.layout.booklist,null); viewHolder.mBook= convertView.findViewById(R.id.book_show); convertView.setTag(viewHolder); }else { viewHolder=(ViewHolder)convertView.getTag(); } Book book=mList.get(position); viewHolder.mBook.setText(book.getBookName()); return convertView; } } ShopsAdapter.java