android 仿微信demo——微信消息界面實(shí)現(xiàn)(服務(wù)端)
上一篇實(shí)現(xiàn)了移動(dòng)端微信消息界面功能,以此為基礎(chǔ)繼續(xù)完善服務(wù)端功能
服務(wù)端微信消息頁實(shí)現(xiàn)微信消息界面的實(shí)現(xiàn),和登錄,注冊是類似的,無非就是接受客戶端數(shù)據(jù),然后通過這個(gè)數(shù)據(jù)去數(shù)據(jù)庫查找,如果查得到話,返回相應(yīng)值給客戶端。
在移動(dòng)端中,當(dāng)用戶輸入表單后點(diǎn)擊登陸,如果登陸成功,則會(huì)把微信號(hào)通過Itent傳給主界面activity,而在微信主界面點(diǎn)擊微信消息界面時(shí),會(huì)把微信號(hào)作為fragment的參數(shù)傳給微信消息界面,然后通過把微信號(hào)數(shù)據(jù)發(fā)送給服務(wù)器,服務(wù)器接受到這消息,便會(huì)在數(shù)據(jù)庫中查找,查得到得話便會(huì)返回所以列給客戶端,而客戶端接受到數(shù)據(jù)后便把數(shù)據(jù)顯示到相應(yīng)得組件上(這個(gè)功能在移動(dòng)端已經(jīng)實(shí)現(xiàn)了)
創(chuàng)建Servlet WeixinInformation.java,實(shí)現(xiàn)服務(wù)端和客戶端的數(shù)據(jù)交互
package com.example.controller;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.example.pojo.WeixinList;import com.example.service.UserServiceImpl;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.URLDecoder;@WebServlet(name = 'WeixinInformation', value = '/WeixinInformation')public class WeixinInformation extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//設(shè)置字符編碼,防止中文亂碼request.setCharacterEncoding('utf-8');response.setCharacterEncoding('UTF-8');//以json數(shù)據(jù)完成操作response.setContentType('application/json;charset=UTF-8');System.out.println(request.getContentType());// 得到客戶端發(fā)送過來內(nèi)容的類型,application/json;charset=UTF-8System.out.println(request.getRemoteAddr());// 得到客戶端的ip地址,BufferedReader br = new BufferedReader(new InputStreamReader(// 使用字符流讀取客戶端發(fā)過來的數(shù)據(jù)request.getInputStream()));String line = null;StringBuffer s = new StringBuffer();//StringBuffer String的區(qū)別,如果要對數(shù)據(jù)作?繁的修改,?t用StringBuffer// 以一行的形式讀取數(shù)據(jù)while ((line = br.readLine()) != null) { s.append(line);}// 關(guān)閉io流br.close();System.out.println(s.toString());// //JSON:這是json解析包,IDEA是沒有,要我們自己導(dǎo)入WeixinList weixinList = JSON.parseObject(s.toString(), WeixinList.class);//是用了反射機(jī)制?磽瓿啥韻蟮姆獗//以utf-8解碼操作String number = URLDecoder.decode(weixinList.getNumber(), 'utf-8');System.out.println(weixinList);// 去數(shù)據(jù)庫完成用戶登錄功能UserServiceImpl us = new UserServiceImpl();//調(diào)用登錄的方法WeixinList weixinList1 = us.informationUser(number);if(weixinList1 != null) { //將結(jié)果返回給客戶端,?⒔Y(jié)果??建成json???禱亟o客?舳 JSONObject rjson = new JSONObject(); rjson.put('json', weixinList1); response.getOutputStream().write( rjson.toString().getBytes('UTF-8'));// 向客戶端發(fā)送一個(gè)帶有json對象內(nèi)容的響應(yīng)} }}
上面代碼用到微信消息頁WeixinList實(shí)體類,下面將給出
實(shí)體類WeixinList.java
WeixinList.java
package com.example.pojo;public class WeixinList { private int id; private String titleimg; private String title; private String content; private String time; private String showcode; private String number; public int getId() {return id; } public void setId(int id) {this.id = id; } public String getTitleimg() {return titleimg; } public void setTitleimg(String titleimg) {this.titleimg = titleimg; } public String getTitle() {return title; } public void setTitle(String title) {this.title = title; } public String getContent() {return content; } public void setContent(String content) {this.content = content; } public String getTime() {return time; } public void setTime(String time) {this.time = time; } public String getShowcode() {return showcode; } public void setShowcode(String showcode) {this.showcode = showcode; } public String getNumber() {return number; } public void setNumber(String number) {this.number = number; } @Override public String toString() {return 'Information{' +'id=' + id +', titleimg=’' + titleimg + ’’’ +', title=’' + title + ’’’ +', content=’' + content + ’’’ +', time=’' + time + ’’’ +', showcode=’' + showcode + ’’’ +', number=’' + number + ’’’ +’}’; }}
在service層中的接口UserService.java添加處理微信消息界面數(shù)據(jù)業(yè)務(wù)邏輯處理的抽象方法
//微信消息列表 WeixinList informationUser(String number);
在service層中的類UserServiceImpl.java重寫上面接口剛添加的方法
public WeixinList informationUser(String number) {//調(diào)用dao層完成數(shù)據(jù)查詢操作WeixinList information = ud.findInformation(number);return information; }
在dao層中的接口UserDao .java添加處理微信消息界面數(shù)據(jù)并操作數(shù)據(jù)庫的的抽象方法
//查詢微信消息列表 WeixinList findInformation(String number);
在dao層中的類UserDaoImpl.java重寫上面接口剛添加的方法
@Override public WeixinList findInformation(String number) {//sqlString sql = 'select * from weixinlist where number=?;';ResultSet rs = JDBCUtil.executeQuery(sql, number);//判斷是否查詢到用戶try { if (rs.next()) {//如果查詢到用戶,將用戶封裝到User對象中int id = rs.getInt('id');String titleimg = rs.getString('titleimg');String title1 = rs.getString('title');String content = rs.getString('content');String time = rs.getString('time');String showcode = rs.getString('showcode');String number1 = rs.getString('number');//將查詢到的用戶封裝到一個(gè)User對象中WeixinList weixinList = new WeixinList();weixinList .setId(id);weixinList .setTitleimg(titleimg);weixinList .setTitle(title1);weixinList .setContent(content);weixinList .setTime(time);weixinList .setShowcode(showcode);weixinList .setNumber(number1);System.out.println('查詢到的用戶' + weixinList);return weixinList; }}catch (SQLException throwables) { throwables.printStackTrace();}return null; }
微信消息界面每一個(gè)列表至少有兩個(gè)圖片,而圖片不是存放在數(shù)據(jù)庫中的,數(shù)據(jù)庫存放得是圖片的地址,所以要在webapp目錄下創(chuàng)建存放圖片的目錄
在imgs目錄下創(chuàng)建單獨(dú)存放微信消息界面圖片的目錄,因?yàn)楹竺鏁?huì)有通訊錄,聊天,朋友圈圖片,這樣方便管理。
之后就可以把有關(guān)微信消息界面的圖片放在這個(gè)目錄下,啟動(dòng)項(xiàng)目再瀏覽器進(jìn)行測試
如果404則進(jìn)行如下操作
如果把用戶每一個(gè)微信消息界面列表單獨(dú)在一個(gè)記錄里,則要查找很多次,而在客戶端主界面跳轉(zhuǎn)到微信消息界面時(shí)只會(huì)請求一次服務(wù)器(通過微信號(hào)),顯示這樣做是行不通的,所以要把每一個(gè)用戶的所有微信消息列表都存放在一個(gè)記錄里,這樣通過微信號(hào)查找就會(huì)得到所有微信消息界面列表,然后發(fā)送給客戶端,客戶端只要對其進(jìn)行解析分離即可(這個(gè)功能移動(dòng)端已經(jīng)實(shí)現(xiàn)了)
下面給出我的表結(jié)構(gòu)以及表內(nèi)容
除了微信號(hào)number列只有一個(gè)(通過微信號(hào)查找用),其他列里面的行數(shù)據(jù)都要有對應(yīng)數(shù)據(jù)
測試測試前,要給準(zhǔn)備登錄的賬號(hào)在數(shù)據(jù)庫添加數(shù)據(jù),啟動(dòng)服務(wù)端和客戶端項(xiàng)目測試
這篇關(guān)于微信demo的文章就到這里了,希望大家可以多多關(guān)注好吧啦網(wǎng)的更多精彩內(nèi)容!
相關(guān)文章: