android 仿微信demo——微信消息界面實現(xiàn)(服務(wù)端)
上一篇實現(xiàn)了移動端微信消息界面功能,以此為基礎(chǔ)繼續(xù)完善服務(wù)端功能
服務(wù)端微信消息頁實現(xiàn)
微信消息界面的實現(xiàn),和登錄,注冊是類似的,無非就是接受客戶端數(shù)據(jù),然后通過這個數(shù)據(jù)去數(shù)據(jù)庫查找,如果查得到話,返回相應(yīng)值給客戶端。
在移動端中,當(dāng)用戶輸入表單后點擊登陸,如果登陸成功,則會把微信號通過Itent傳給主界面activity,而在微信主界面點擊微信消息界面時,會把微信號作為fragment的參數(shù)傳給微信消息界面,然后通過把微信號數(shù)據(jù)發(fā)送給服務(wù)器,服務(wù)器接受到這消息,便會在數(shù)據(jù)庫中查找,查得到得話便會返回所以列給客戶端,而客戶端接受到數(shù)據(jù)后便把數(shù)據(jù)顯示到相應(yīng)得組件上(這個功能在移動端已經(jīng)實現(xiàn)了)
創(chuàng)建Servlet WeixinInformation.java,實現(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-8
System.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ù)作頻繁的修改,則用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é)果返回給客戶端 ,將結(jié)果構(gòu)建成json數(shù)據(jù)返回給客戶端
JSONObject rjson = new JSONObject();
rjson.put("json", weixinList1);
response.getOutputStream().write(
rjson.toString().getBytes("UTF-8"));// 向客戶端發(fā)送一個帶有json對象內(nèi)容的響應(yīng)
}
}
}
上面代碼用到微信消息頁WeixinList實體類,下面將給出
實體類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) {
//sql
String 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");
//將查詢到的用戶封裝到一個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;
}
微信消息界面每一個列表至少有兩個圖片,而圖片不是存放在數(shù)據(jù)庫中的,數(shù)據(jù)庫存放得是圖片的地址,所以要在webapp目錄下創(chuàng)建存放圖片的目錄



在imgs目錄下創(chuàng)建單獨存放微信消息界面圖片的目錄,因為后面會有通訊錄,聊天,朋友圈圖片,這樣方便管理。


之后就可以把有關(guān)微信消息界面的圖片放在這個目錄下,啟動項目再瀏覽器進(jìn)行測試


如果404則進(jìn)行如下操作


如果把用戶每一個微信消息界面列表單獨在一個記錄里,則要查找很多次,而在客戶端主界面跳轉(zhuǎn)到微信消息界面時只會請求一次服務(wù)器(通過微信號),顯示這樣做是行不通的,所以要把每一個用戶的所有微信消息列表都存放在一個記錄里,這樣通過微信號查找就會得到所有微信消息界面列表,然后發(fā)送給客戶端,客戶端只要對其進(jìn)行解析分離即可(這個功能移動端已經(jīng)實現(xiàn)了)
下面給出我的表結(jié)構(gòu)以及表內(nèi)容


除了微信號number列只有一個(通過微信號查找用),其他列里面的行數(shù)據(jù)都要有對應(yīng)數(shù)據(jù)
測試
測試前,要給準(zhǔn)備登錄的賬號在數(shù)據(jù)庫添加數(shù)據(jù),啟動服務(wù)端和客戶端項目測試

總結(jié)
這篇關(guān)于微信demo的文章就到這里了,希望大家可以多多關(guān)注腳本之家的更多精彩內(nèi)容!
相關(guān)文章
Android開啟ADB網(wǎng)絡(luò)調(diào)試方法
今天小編就為大家分享一篇Android開啟ADB網(wǎng)絡(luò)調(diào)試方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
詳解Android WebView的input上傳照片的兼容問題
本篇文章主要介紹了詳解Android WebView的input上傳照片的兼容問題,非常具有實用價值,需要的朋友可以參考下2017-08-08
Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
這篇文章主要介紹了Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
Android圖片的Base64編碼與解碼及解碼Base64圖片方法
Base64是網(wǎng)絡(luò)上最常見的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個可打印字符來表示二進(jìn)制數(shù)據(jù)的方法。接下來通過本文給大家分享Android圖片的Base64編碼與解碼及解碼Base64圖片,需要的朋友參考下吧2017-12-12

