• 第二阶段:团队开发Fooks第四天


     

     

    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.Bookshelf;
    import net.sf.json.JSONArray;
    
    
    public class BookshelfServlet 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<Bookshelf> bookshelfs=new ArrayList();
            bookshelfs=dao.QueryShelf(username);
            
            PrintWriter out = response.getWriter();
            JSONArray jsonArray = JSONArray.fromObject(bookshelfs);
            
            out.print(jsonArray.toString());
        }
    
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    BookshelfServlet.java
    package com.example.fooks;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.Toast;
    
    import androidx.annotation.Nullable;
    
    import com.example.fooks.entity.Book;
    import com.example.fooks.entity.Bookshelf;
    import com.example.fooks.utils.BooksAdapter;
    import com.example.fooks.utils.ViewHolder;
    import com.loopj.android.http.AsyncHttpClient;
    import com.loopj.android.http.AsyncHttpResponseHandler;
    import com.loopj.android.http.FileAsyncHttpResponseHandler;
    import com.loopj.android.http.RequestParams;
    
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.io.File;
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    import java.util.ArrayList;
    
    import java.util.List;
    
    import cz.msebera.android.httpclient.Header;
    
    
    public class ReadActivity extends Activity {
        private String mUsername;
        private Button mHome;
        private Button mBook;
        private Button mList;
        private Button mPerson;
        private Button mUpload;
        private EditText editText;
        private EditText editText2;
        private ListView mListView;
        private List<Bookshelf> mShowBookShelfs  =new ArrayList<>();
        private List <ViewHolder> mViewHolder;
        private static final int FILE_SELECT_CODE = 0;
        private static String TAG="ReadActivity";
        //设置返回按钮:不应该退出程序---而是返回桌面
        //复写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 onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_read);
            //把输入框变成分割线 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);
    
            Intent intent =getIntent();
            mUsername =intent.getStringExtra("username");
    
            mListView=(ListView) this.findViewById(R.id.book_list) ;
            //初始化控件
            initView();
    
            initListener();
    
            initBook();
    
        }
    
        private void initBook() {
            ListView ShowBookList=(ListView)findViewById(R.id.book_list);
    
            //获取数据
            AsyncHttpClient client =new AsyncHttpClient();
            String url = "http://47.94.229.72:8080/Fooks/BookshelfServlet";//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);
                                    Bookshelf bookshelf =new Bookshelf();
                                    bookshelf.setId(jsonObject.getInt("id"));
                                    bookshelf.setBookName(jsonObject.getString("bookName"));
                                    bookshelf.setUsername(jsonObject.getString("username"));
                                    mShowBookShelfs.add(bookshelf);
                                }
                                BooksAdapter booksAdapter=new BooksAdapter(mShowBookShelfs,ReadActivity.this);
                                ShowBookList.setAdapter(booksAdapter);
    
                            } 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();
                }
            });
        }
    
        private void initListener() {
            mUpload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    upLoad();
    
                }
            });
            mHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ReadActivity.this,MainActivity.class);
                    intent.putExtra("username",mUsername);
                    startActivity(intent);
                    ReadActivity.this.overridePendingTransition(0, 0);
    
                }
            });
            mList.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ReadActivity.this,ListActivity.class);
                    intent.putExtra("username",mUsername);
                    startActivity(intent);
                    ReadActivity.this.overridePendingTransition(0, 0);
    
                }
            });
            mPerson.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ReadActivity.this,UserActivity.class);
                    intent.putExtra("username",mUsername);
                    startActivity(intent);
                    ReadActivity.this.overridePendingTransition(0, 0);
    
                }
            });
            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Bookshelf bookshelf=mShowBookShelfs.get(position);
                    String path=Environment.getExternalStorageDirectory()+"/Fooks/Book/"+bookshelf.getBookName();
                    File file =new File(path);
                    if(file.exists()){
                        Intent intent =new Intent(ReadActivity.this,BookActivity.class);
                        intent.putExtra("username",mUsername);
                        intent.putExtra("bookName",bookshelf.getBookName());
                        intent.putExtra("path",path);
                        startActivity(intent);
    
                    }else {
                        Toast.makeText(getApplicationContext(),"开始下载,请耐心等待",Toast.LENGTH_LONG).show();
                        Log.e(TAG,"book:"+bookshelf);
                        Log.e(TAG,"path:"+path);
                        DownLoadBook(bookshelf,path);
                    }
                }
            });
        }
        @Override
        protected void onPause() {
            overridePendingTransition(0,0);
            super.onPause();
        }
        private void DownLoadBook(Bookshelf bookshelf,String path) {
            AsyncHttpClient client =new AsyncHttpClient();
            String url = "http://47.94.229.72:8080/Fooks/Book/";//url组成:ip:端口 + 服务端工程名 + servlet名
            try {
                String URL=URLEncoder.encode(bookshelf.getBookName(),"utf-8");
                URL=URL.replaceAll("\+", "%20");
                Log.e(TAG,url+URL);
    
            client.post(url+URL, new FileAsyncHttpResponseHandler(new File(path)) {
                @Override
                public void onFailure(int i, Header[] headers, Throwable throwable, File file) {
                    Toast.makeText(getApplicationContext(),"下载失败,请检查网络",Toast.LENGTH_LONG).show();
                }
    
                @Override
                public void onSuccess(int i, Header[] headers, File file) {
    
                    Toast.makeText(getApplicationContext(),"下载成功,点击打开",Toast.LENGTH_LONG).show();
                }
            });
    
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
    
        }
    
        private void upLoad() {
            Intent intent = new Intent(ReadActivity.this,AddActivity.class);
            intent.putExtra("username",mUsername);
            startActivity(intent);
    
        }
    
    
    
    
    
        private void initView() {
            mBook=(Button) this.findViewById(R.id.btn_list);
            mHome=(Button) this.findViewById(R.id.btn_home);
            mList=(Button) this.findViewById(R.id.btn_list);
    
            mPerson=(Button) this.findViewById(R.id.btn_person);
            mUpload=(Button) this.findViewById(R.id.book_upload);
        }
    }
    ReadActivity
    package com.example.fooks.utils;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    import com.example.fooks.R;
    import com.example.fooks.ReadActivity;
    import com.example.fooks.entity.Book;
    import com.example.fooks.entity.Bookshelf;
    
    import java.util.List;
    
    public class BooksAdapter extends BaseAdapter {
        private List<Bookshelf> mList;
        private Context mContext;
        private LayoutInflater mLayoutInflater;
    
    
        public BooksAdapter(List<Bookshelf> 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();
            }
    
            Bookshelf bookshelf=mList.get(position);
            viewHolder.mBook.setText(bookshelf.getBookName());
    
            return convertView;
        }
    }
    BooksAdapter.java

    今天实现了书架的个人化,明天进行长按删除的功能。

  • 相关阅读:
    JVM源码分析 规格严格
    Smack 规格严格
    Java动态编译 规格严格
    RPM仓库地址 规格严格
    使用控制台程序测试DLL依赖
    TestNG 使用入门
    白羊座二:星星的一周
    路遇两骗子
    《落地,请开手机》里面最经典的一句台词
    今天明白的一个道理
  • 原文地址:https://www.cnblogs.com/yeyueweiliang/p/12989890.html
Copyright © 2020-2023  润新知