在上一篇已经完成了发现页的分页加载功能,接下来就开始实现查找页的功能,在这个模块会分成找人和找文两个查找方向,初步设计是通过查找相关用户昵称的关键字以及文章标题和简述的关键字进行查找;另外在进入该查找页时希望会展示前十的热门文章,其中热门的判定标准是点赞数
- 在myEclipse的myServlet.data包下创建查询热门文章的servlet——QueryHotInfo.java,该文件主要就是查询点赞数前十的文章内容并返回所有相关信息,和上一篇发现页的查询类似,具体代码如下:
- 注:为方便后期扩展,虽然不做分页但仍然将返回的总记录数封装到返回的Json
package myServlet.data;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import javax.servlet.ServletConfig;
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 org.utils.JdbcUtils;
import org.utils.JsonUtils;import com.sun.rowset.CachedRowSetImpl;
import net.sf.json.JSONArray;
/**
* Servlet implementation class QueryHotInfo
*/
@WebServlet("/QueryHotInfo")
public class QueryHotInfo extends HttpServlet {
private static final long serialVersionUID = 1L;
CachedRowSetImpl rowSet = null; //存储表中全部记录的行集对象
int pageSize=10; //每页加载数量
int pageNum=1; //第几页
int totalRecord; //总记录数
int totalPage; //总页数
/**
* @see HttpServlet#HttpServlet()
*/
public QueryHotInfo() {
super();
// TODO Auto-generated constructor stub
}/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
//将传过来的字符串转为Boolean
/*public Boolean stringToBoolean(String str)
{
switch(str.toLowerCase())
{
case "true":case "1": return true;
case "false":case "0": return false;
default: return false;
}
}*/
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String condition = "select * from info order by info_support desc"; //按点赞数降序,查询热门
Connection connection = null;
Statement sql = null;
ResultSet rs = null;try {
connection = JdbcUtils.getConnection();
sql = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery(condition);
rowSet = new CachedRowSetImpl(); //创建行集对象
rowSet.populate(rs);
//按查询页数返回结果
returnByPage(request,response,rowSet);
} catch (Exception e) {
out.println(condition+"异常:"+e.toString());
e.printStackTrace();
}finally {
//5.释放资源 connection prepareStatement
JdbcUtils.statementClose(connection, sql, rs);
}
}/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
public void returnByPage(HttpServletRequest request, HttpServletResponse response,CachedRowSetImpl rowSet)throws ServletException, IOException{
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
JSONArray TotaljsonArray = new JSONArray();//存放返回的jsonOjbect数组
//将rowSet的数据提取到Map
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
//将rowSet的数据提取到Map
List<Map<String,Object>> Totaldata = new ArrayList<Map<String,Object>>();
try {
PrintWriter out = response.getWriter();
try {
//ResultSetMetaData metaData = rowSet.getMetaData();
//int columnCount = metaData.getColumnCount(); //返回总列数
rowSet.last(); //移到随后一行
totalRecord = rowSet.getRow();
/*out.println("全部记录数"+totalRecord); //全部的记录数*/
if(totalRecord%pageSize==0){
totalPage = totalRecord/pageSize; //总页数
}else{
totalPage = totalRecord/pageSize+1;
}
int index = (pageNum-1)*pageSize+1;
rowSet.absolute(index); //查询位置移动到查询页的起始记录位置
boolean boo = true;
for(int i=1; i<=pageSize&&boo;i++){
int infoId = rowSet.getInt(1); //内容ID
String infoTitle = rowSet.getString(2); //内容标题
String infoDescribe = rowSet.getString(3); //内容简述
String infoDetail = rowSet.getString("info_detail"); //内容详情
String type = rowSet.getString(5); //类型:0表示日记,1表示趣事
String support = rowSet.getString(6); //点赞数
String infoAuthor = rowSet.getString(7); //作者
Map<String,Object> map = new HashMap<String,Object>();
map.put("infoId", infoId);
map.put("infoTitle", infoTitle);
map.put("infoDescribe", infoDescribe);
map.put("infoDetail", infoDetail);
map.put("infoType", type);
map.put("infoSupport", support);
map.put("infoAuthor", infoAuthor);
data.add(map);
boo = rowSet.next();
}
jsonArray = JsonUtils.formatRsToJsonArray(data);
Map<String,Object> map = new HashMap<String,Object>();
map.put("totalRecord", totalRecord);
map.put("RecordDetail", jsonArray);
Totaldata.add(map);
TotaljsonArray = JsonUtils.formatRsToJsonArray(Totaldata);
out.println(TotaljsonArray.toString()); //返回json
}catch(Exception e) {
}
}catch(IOException e) {
}
}
}
2.接下来在同一个包下面创建按关键字查找文章的servlet——QueryInfoByKey.java
- 其实这部分的代码与上面都是大同小异,不过是这次返回的相关内容是一次性返回所有的记录,具体代码如下:
package myServlet.data;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import javax.servlet.ServletConfig;
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 org.utils.JdbcUtils;
import org.utils.JsonUtils;import com.sun.rowset.CachedRowSetImpl;
import net.sf.json.JSONArray;
/**
* Servlet implementation class QueryInfoByKey
*/
@WebServlet("/QueryInfoByKey")
public class QueryInfoByKey extends HttpServlet {
private static final long serialVersionUID = 1L;
CachedRowSetImpl rowSet = null; //存储表中全部记录的行集对象
int pageSize=10; //每页加载数量
int pageNum=1; //第几页
int totalRecord; //总记录数
int totalPage; //总页数
/**
* @see HttpServlet#HttpServlet()
*/
public QueryInfoByKey() {
super();
// TODO Auto-generated constructor stub
}/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String keyWord = request.getParameter("key");
if(keyWord == null||keyWord.length()==0) {
return;
}
String condition ="select * from info where info_title like '%"+keyWord+"%' or info_describe like '%"+keyWord+"%'" ; //按关键词查找
Connection connection = null;
Statement sql = null;
ResultSet rs = null;try {
connection = JdbcUtils.getConnection();
sql = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery(condition);
rowSet = new CachedRowSetImpl(); //创建行集对象
rowSet.populate(rs);
//按查询页数返回结果
returnByPage(request,response,rowSet);
} catch (Exception e) {
out.println(condition+"异常:"+e.toString());
e.printStackTrace();
}finally {
//5.释放资源 connection prepareStatement
JdbcUtils.statementClose(connection, sql, rs);
}
}/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
public void returnByPage(HttpServletRequest request, HttpServletResponse response,CachedRowSetImpl rowSet)throws ServletException, IOException{
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
JSONArray TotaljsonArray = new JSONArray();//存放返回的jsonOjbect数组
//将rowSet的数据提取到Map
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
//将rowSet的数据提取到Map
List<Map<String,Object>> Totaldata = new ArrayList<Map<String,Object>>();
try {
PrintWriter out = response.getWriter();
try {
//ResultSetMetaData metaData = rowSet.getMetaData();
//int columnCount = metaData.getColumnCount(); //返回总列数
rowSet.last(); //移到随后一行
totalRecord = rowSet.getRow();
/*out.println("全部记录数"+totalRecord); //全部的记录数*/
if(totalRecord%pageSize==0){
totalPage = totalRecord/pageSize; //总页数
}else{
totalPage = totalRecord/pageSize+1;
}
int index = (pageNum-1)*pageSize+1;
rowSet.absolute(index); //查询位置移动到查询页的起始记录位置
boolean boo = true;
for(int i=1; i<=totalRecord&&boo;i++){
int infoId = rowSet.getInt(1); //内容ID
String infoTitle = rowSet.getString(2); //内容标题
String infoDescribe = rowSet.getString(3); //内容简述
String infoDetail = rowSet.getString("info_detail"); //内容详情
String type = rowSet.getString(5); //类型:0表示日记,1表示趣事
String support = rowSet.getString(6); //点赞数
String infoAuthor = rowSet.getString(7); //作者
Map<String,Object> map = new HashMap<String,Object>();
map.put("infoId", infoId);
map.put("infoTitle", infoTitle);
map.put("infoDescribe", infoDescribe);
map.put("infoDetail", infoDetail);
map.put("infoType", type);
map.put("infoSupport", support);
map.put("infoAuthor", infoAuthor);
data.add(map);
boo = rowSet.next();
}
jsonArray = JsonUtils.formatRsToJsonArray(data);
Map<String,Object> map = new HashMap<String,Object>();
map.put("totalRecord", totalRecord);
map.put("RecordDetail", jsonArray);
Totaldata.add(map);
TotaljsonArray = JsonUtils.formatRsToJsonArray(Totaldata);
out.println(TotaljsonArray.toString()); //返回json
}catch(Exception e) {
}
}catch(IOException e) {
}
}}
3.然后就是处理按关键字查找用户,同样的在同一包下面创建一个servlet——QueryPeopleByKey.java
- 这边的处理也不用多讲了,就和按关键字查找文章的处理一样,只是sql和Json的封装有点差别,直接上代码:
package myServlet.data;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import javax.servlet.ServletConfig;
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 org.utils.JdbcUtils;
import org.utils.JsonUtils;import com.sun.rowset.CachedRowSetImpl;
import net.sf.json.JSONArray;
/**
* Servlet implementation class QueryPeopleInfo
*/
@WebServlet("/QueryPeopleInfoByKey")
public class QueryPeopleInfoByKey extends HttpServlet {
private static final long serialVersionUID = 1L;
CachedRowSetImpl rowSet = null; //存储表中全部记录的行集对象
int pageSize=10; //每页加载数量
int pageNum=1; //第几页
int totalRecord; //总记录数
int totalPage; //总页数
/**
* @see HttpServlet#HttpServlet()
*/
public QueryPeopleInfoByKey() {
super();
// TODO Auto-generated constructor stub
}/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String keyWord = request.getParameter("nameKey");
if(keyWord == null||keyWord.length()==0) {
return;
}
String condition ="select * from user where username like '%"+keyWord+"%'" ; //按关键词查找
Connection connection = null;
Statement sql = null;
ResultSet rs = null;try {
connection = JdbcUtils.getConnection();
sql = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery(condition);
rowSet = new CachedRowSetImpl(); //创建行集对象
rowSet.populate(rs);
//按查询页数返回结果
returnByPage(request,response,rowSet);
} catch (Exception e) {
out.println(condition+"异常:"+e.toString());
e.printStackTrace();
}finally {
//5.释放资源 connection prepareStatement
JdbcUtils.statementClose(connection, sql, rs);
}
}/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
public void returnByPage(HttpServletRequest request, HttpServletResponse response,CachedRowSetImpl rowSet)throws ServletException, IOException{
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
JSONArray TotaljsonArray = new JSONArray();//存放返回的jsonOjbect数组
//将rowSet的数据提取到Map
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
//将rowSet的数据提取到Map
List<Map<String,Object>> Totaldata = new ArrayList<Map<String,Object>>();
try {
PrintWriter out = response.getWriter();
try {
rowSet.last(); //移到随后一行
totalRecord = rowSet.getRow();
if(totalRecord%pageSize==0){
totalPage = totalRecord/pageSize; //总页数
}else{
totalPage = totalRecord/pageSize+1;
}
int index = (pageNum-1)*pageSize+1;
rowSet.absolute(index); //查询位置移动到查询页的起始记录位置
boolean boo = true;
for(int i=1; i<=totalRecord&&boo;i++){
String userName = rowSet.getString(1); //用户昵称
//String passWord = rowSet.getString(2); //用户密码
String signature = rowSet.getString(3); //用户签名
String userLogImage = rowSet.getString(4); //用户头像
Map<String,Object> map = new HashMap<String,Object>();
map.put("userName", userName);
/*map.put("passWord", passWord);*/
map.put("signature", signature);
map.put("userLogImage", userLogImage);
data.add(map);
boo = rowSet.next();
}
jsonArray = JsonUtils.formatRsToJsonArray(data);
Map<String,Object> map = new HashMap<String,Object>();
map.put("totalRecord", totalRecord);
map.put("RecordDetail", jsonArray);
Totaldata.add(map);
TotaljsonArray = JsonUtils.formatRsToJsonArray(Totaldata);
out.println(TotaljsonArray.toString()); //返回json
}catch(Exception e) {
}
}catch(IOException e) {
}
}}
4.有了上述三个接口,我们就可以快乐地写Android端查找页的代码了
- 首先当然是要有查找页的页面布局,准备是在顶部放一个搜索框和两个搜索按钮以及一个清除文本框输入内容的清除按钮,而热门内容列表和查询结果列表就套用之前发现页中的做法,使用listView
- 去图标网站下载搜索和删除的小图标,命名就按下面代码文件中所示,将图标放到drawable中
——在layout下 修改原来的find_tab_content.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff"> <LinearLayout android:gravity="center_vertical" android:orientation="horizontal" android:id="@+id/title_bar" android:background="@drawable/title_bg_night" android:layout_width="fill_parent" android:layout_height="35.0dip"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/app_icon" /> <LinearLayout android:gravity="center_vertical" android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_weight="1.0"> <TextView android:textSize="16.0sp" android:textColor="@color/titleTextColor" android:singleLine="true" android:id="@+id/myTitle" android:layout_width="fill_parent" android:ellipsize="end" android:layout_height="wrap_content" android:layout_marginLeft="2.0dip" android:text="@string/app_name" /> </LinearLayout> <ImageButton android:id="@+id/back_button" android:layout_gravity="center" android:background="@color/transparent" android:layout_width="60.0dip" android:layout_height="29.0dip" android:layout_marginRight="5.0dip" android:src="@drawable/exit" /> </LinearLayout> <!--搜索框--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/find_search_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#CCFFFF" android:paddingBottom="30dp" android:orientation="vertical" android:gravity="center" android:layout_below="@+id/title_bar"> <LinearLayout android:layout_width="fill_parent" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="5dp" android:background="@drawable/small_bc" android:orientation="horizontal"> <Button android:layout_width="20dp" android:layout_height="20dp" android:layout_gravity="right|center_vertical" android:layout_margin="30dp" android:background="@drawable/search" /> <!-- 输入的搜索信息 --> <EditText android:id="@+id/et_seek_search" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="4" android:background="@null" android:gravity="center_vertical" android:hint="输入想要搜索的关键字" android:imeOptions="actionSearch" android:singleLine="true" android:textColor="#0e0e0e" android:textColorHint="#b0c6ce" android:textSize="17sp" /> //清除查询 <Button android:id="@+id/seek_bt_clear" android:layout_width="25dp" android:layout_height="25dp" android:layout_gravity="right|center_vertical" android:layout_margin="30dp" android:background="@drawable/delete" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <Button android:id="@+id/find_search_by_name" android:layout_width="70dp" android:layout_height="30dp" android:background="@drawable/button_radius" android:text="找人" android:gravity="center" /> <Button android:id="@+id/find_search_by_keyWord" android:layout_width="70dp" android:layout_height="30dp" android:layout_marginLeft="10dp" android:background="@drawable/button_radius" android:text="找文" android:gravity="center" /> </LinearLayout> </LinearLayout> <!-- <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/tab_icon_three" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:padding="10dp" android:text="这是查找界面" android:textSize="20sp" /> </LinearLayout>--> <LinearLayout android:layout_below="@+id/title_bar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical" android:paddingTop="140dp"> <TextView android:id="@+id/seek_list_hint_info" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="6.0dip" android:gravity="center_vertical" android:paddingLeft="10.0dip" android:text="大家都在看:" android:textColor="#DF3154" android:textSize="16.0sp" /> <ListView android:id="@+id/find_listView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:dividerHeight="0dp" /> </LinearLayout> </RelativeLayout>
5.除了上面的布局设计,我们还需要一个放查询结果的列表项布局文件
- 在layout下创建文件activity_seek_list_item.xml,具体代码如下:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TableLayout android:id="@+id/seek_list_tableLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="1.0dip" android:shrinkColumns="0" android:stretchColumns="0" android:background="#ebebeb"> <TableRow android:id="@+id/seek_list_tableRow" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/small_bc" android:paddingBottom="12.0dip" android:paddingTop="12.0dip" > <TextView android:id="@+id/seek_list_textView" android:layout_width="wrap_content" android:layout_height="fill_parent" android:drawableLeft="@drawable/hot_key" android:drawablePadding="16.0dip" android:gravity="center_vertical" android:includeFontPadding="false" android:text="热门标题" android:textColor="#ff333333" android:textSize="16.0sp" android:paddingLeft="20dp"/> <TextView android:id="@+id/seek_list_textView2" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center_vertical" android:includeFontPadding="false" android:text="12345" android:textColor="#424202" android:textSize="16.0sp" android:paddingRight="20dp"/> <!--android:adjustViewBounds="true"图片自适应--> <ImageView android:adjustViewBounds="true" android:id="@+id/seek_list_imgView" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_gravity="right" android:gravity="center_vertical" android:src="@drawable/next_right" android:paddingRight="30dp"/> </TableRow> </TableLayout> </LinearLayout> </LinearLayout> </LinearLayout>
6.有了页面布局就可以开始做数据处理了,在原来创建好的FindTabFragment.java文件中进行修改
- 首先是关联页面组件
- 因为打开页面就可以显示热门内容的Top10,所以要先从后台获取数据
/** * 查询数据库中的数据 */ private JSONArray loadDataFromDataBase(String QueryInfoUrl){ //Toast.makeText(getActivity(),"保存",Toast.LENGTH_SHORT).show(); StringBuilder stringBuilder = new StringBuilder(); BufferedReader buffer = null; HttpURLConnection connGET = null; try { URL url = new URL(QueryInfoUrl); connGET = (HttpURLConnection) url.openConnection(); connGET.setConnectTimeout(5000); connGET.setRequestMethod("GET"); if (connGET.getResponseCode() == 200) { buffer = new BufferedReader(new InputStreamReader(connGET.getInputStream())); for (String s = buffer.readLine(); s != null; s = buffer.readLine()) { stringBuilder.append(s); } //返回测试信息 JSONArray jsonArray = new JSONArray(stringBuilder.toString()); /* testTxt.setText(baseUrl+"QueryDiscover?page="+page+"&count="+count);*/ //获取到的数据,对Json进行解析 page = page+1; //一次成功请求后更新请求页面 buffer.close(); return jsonArray; }else{ Toast.makeText(getActivity(),"非200", Toast.LENGTH_LONG).show(); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(getActivity(), "get 提交 err.." + e.toString(), Toast.LENGTH_LONG).show(); } return null; }
- 然后和发现页的处理一样将数据存到bean中 以及初始化listView适配器
//初始化将详情设置到FindInfo bean中 public List<FindInfo> initSetDataToBean(String detail){ List<FindInfo> findInfo = new ArrayList<FindInfo>(); try { JSONArray detailJsonArray = new JSONArray(detail); for (int i = 0; i < detailJsonArray.length(); i++) { FindInfo items = new FindInfo(); JSONObject temp = (JSONObject) detailJsonArray.get(i); Integer infoId = temp.getInt("infoId"); //内容ID String infoTitle = temp.getString("infoTitle"); //内容标题 String infoDescribe = temp.getString("infoDescribe"); //内容简述 String infoDetail = temp.getString("infoDetail"); //内容详情 Integer type = temp.getInt("infoType"); //类型:0表示日记,1表示趣事 Integer support = temp.getInt("infoSupport"); //点赞数 String infoAuthor = temp.getString("infoAuthor"); //作者 items.setInfoId(infoId); items.setInfoTitle(infoTitle); items.setInfoDescribe(infoDescribe); items.setInfoDetail(infoDetail); items.setType(type); items.setSupport(support); items.setInfoAuthor(infoAuthor); findInfo.add(items); } return findInfo; }catch (JSONException e){ Toast.makeText(getActivity(), "initSetDataToBean异常 err.." + e.toString(), Toast.LENGTH_LONG).show(); return null; } }
/** * 初始化ListView的适配器,即打开页面展示的数据 */ private void initializeAdapter(){ // 设置线程策略 setVersion(); String QueryHotInfoUrl = baseUrl+"QueryHotInfo"; JSONArray jsonArray = loadDataFromDataBase(QueryHotInfoUrl); try { JSONObject totalObject = (JSONObject)jsonArray.get(0); dataSize = totalObject.getInt("totalRecord"); //总记录数 String detail= totalObject.getString("RecordDetail"); //详情 if(initSetDataToBean(detail)!=null) { adapter = new PaginationAdapter(initSetDataToBean(detail)); //将详情设置到bean中 } }catch (JSONException e){ Toast.makeText(getActivity(), "initializeAdapter异常 err.." + e.toString(), Toast.LENGTH_LONG).show(); } }
——今天比较事多,就到这先吧,这部分文件的所有代码在下一篇会提供