今天实现了书城的书籍列表
package com.example.fooks; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.OrientationHelper; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import com.example.fooks.entity.Book; import com.example.fooks.entity.Bookshelf; import com.example.fooks.utils.BooksAdapter; import com.example.fooks.utils.ShopsAdapter; import com.example.fooks.utils.ViewHolder; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import cz.msebera.android.httpclient.Header; public class ListActivity extends AppCompatActivity { private String mUsername; private Button mHome; private Button mBook; private Button mPerson; private RecyclerView mRecyclerView; private EditText editText; private EditText editText2; private List<Book> mShowBooks =new ArrayList<>(); private List <ViewHolder> mViewHolder; private static String TAG="ListActivity"; //设置返回按钮:不应该退出程序---而是返回桌面 //复写onKeyDown事件 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { Intent home = new Intent(Intent.ACTION_MAIN); home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); home.addCategory(Intent.CATEGORY_HOME); startActivity(home); return true; } return super.onKeyDown(keyCode, event); } @Override protected void onPause() { overridePendingTransition(0,0); super.onPause(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); Intent intent =getIntent(); mUsername =intent.getStringExtra("username"); // mBook=(Button) this.findViewById(R.id.btn_book); mHome=(Button) this.findViewById(R.id.btn_home); mPerson=(Button) this.findViewById(R.id.btn_person); //把输入框变成分割线 by:scf editText=this.findViewById(R.id.editText); editText2=this.findViewById(R.id.editText2); editText.setFocusableInTouchMode(false);//不可编辑 editText.setKeyListener(null); editText2.setFocusableInTouchMode(false);//不可编辑 editText2.setKeyListener(null); // mPerson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ListActivity.this,UserActivity.class); intent.putExtra("username",mUsername); startActivity(intent); ListActivity.this.overridePendingTransition(0, 0); } }); mHome.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ListActivity.this,MainActivity.class); intent.putExtra("username",mUsername); startActivity(intent); ListActivity.this.overridePendingTransition(0, 0); } }); mBook.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ListActivity.this,ReadActivity.class); intent.putExtra("username",mUsername); startActivity(intent); ListActivity.this.overridePendingTransition(0, 0); } }); initBook(); } private void initBook() { mRecyclerView=(RecyclerView)this.findViewById(R.id.shop_booklist); //获取数据 AsyncHttpClient client =new AsyncHttpClient(); String url = "http://47.94.229.72:8080/Fooks/BookListServlet";//url组成:ip:端口 + 服务端工程名 + servlet名 RequestParams params = new RequestParams(); params.put("username",mUsername); client.post(url, params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { if(i == 200) { try { String result = new String(bytes,"utf-8"); Log.e(TAG,"返回结果"+result); try { JSONArray jsonArray = new JSONArray(result); for(int j=0;j<jsonArray.length();j++){ JSONObject jsonObject=jsonArray.getJSONObject(j); Book book =new Book(); book.setId(jsonObject.getInt("id")); book.setBookName(jsonObject.getString("bookName")); book.setBookPath(jsonObject.getString("bookPath")); book.setCreateUser(jsonObject.getString("createUser")); book.setCreateDate(jsonObject.getString("createDate")); mShowBooks.add(book); } ShopsAdapter shopsAdapter=new ShopsAdapter(mShowBooks,ListActivity.this); mRecyclerView.setLayoutManager(new LinearLayoutManager(ListActivity.this)); mRecyclerView.setAdapter(shopsAdapter); } catch (JSONException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } @Override public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { Toast.makeText(getApplicationContext(),"请求失败,请检查网络",Toast.LENGTH_LONG).show(); } }); } }
package Servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; 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.User; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class BookListServlet 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 username=request.getParameter("username"); dao dao =new dao(); List<Book> books=new ArrayList(); books=dao.Query(); PrintWriter out = response.getWriter(); JSONArray jsonArray = JSONArray.fromObject(books); out.print(jsonArray.toString()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }