服务器端程序

Dao

复制代码
package com.example.hixin;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Dao {
    //驱动程序名
    private final static String DRIVER = "com.mysql.jdbc.Driver";
    //URL指向要访问的数据库名
    private final static String URL = "jdbc:mysql://127.0.0.1:3306/test";
    //MySql配置时的用户名密码
    private final static String USER = "root";
    private final static String PWD = "tiger";
    public Connection getCon() throws ClassNotFoundException, SQLException{
        Class.forName(DRIVER);
        Connection con = DriverManager.getConnection(URL,USER,PWD);
        System.out.println(con);
        return con;
    }
    
    public void closeAll(ResultSet rs , Statement st , Connection con) throws SQLException{
        if(rs != null){
            rs.close();
        }
        if(st != null){
            st.close();
        }
        if(con != null){
            con.close();
        }
    }
    
    
}
复制代码

User

复制代码
package com.example.hixin;

import java.io.Serializable;


public class User implements Serializable{
    private String shenfennum;
    private String name;
    private String regtime;
    private String address;
    private String birthdate;
    
    public User(){
        
    }    
    
    public String getShenfennum() {
        return shenfennum;
    }

    public void setShenfennum(String shenfennum) {
        this.shenfennum = shenfennum;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRegtime() {
        return regtime;
    }

    public void setRegtime(String regtime) {
        this.regtime = regtime;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }


    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "User [shenfennum=" + shenfennum + ", name=" + name + ", regtime=" + regtime + ", address=" + address + ", birthdate=" + birthdate + "]";
    }
    
    
}
复制代码

service

 

复制代码
package com.example.hixin;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


/**
 * Service
 * @author wzx
 * 是MVC中的控制层
 */

public class Service {    
    Dao dao = new Dao();
    /**根据设备号在数据库中查找,该设备号存在,则可以登录
     * @param zhongduan_id
     * @return true 可以登录,false 不可以登录
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public boolean canLogin (String id){        
        String str = "select zhongduan_id from zhongduan_zduser where zhongduan_id ="+id;
        try {            
            Connection con = dao.getCon();
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery(str);
            if (rs.next()) {
                return true;
            }
            
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }
        return false;
    }
    
    
    public boolean isUserRegistered(String userid){        
        String str = "select shenfennum from user2 where shenfennum ="+userid;
        try {            
            Connection con = dao.getCon();
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery(str);
            if (rs.next()) {
                return true;
            }
            
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }
        return false;
    }
    
    public void insrtDataToTable(BloodPre bloodpre) {
        try {
            Connection con = dao.getCon();
            Statement statement = con.createStatement();
            String  str = "INSERT INTO xueya2 VALUES("+"null"+","+bloodpre.getUserid()+","+bloodpre.getTime()+","+bloodpre.getHighp()+","+bloodpre.getLowp()+","+bloodpre.getPulse()+")";
            statement.execute(str);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    /**
     *  rs.getString(0);//取第一列数据 
     *  rs.getString("id");//取列名为"id"列的数据       
     * 
     * @param zhongduan_id 根据设备ID拉取使用该设备的所有人
     * @return 
     */
    public  List<User> getUser(String zhongduan_id){
        String strCount = "select count(*) as zhongduanuser_id from zhongduan_zduser where zhongduan_id ="+zhongduan_id;
        String str1 = "select zhongduanuser_id from zhongduan_zduser where zhongduan_id ="+zhongduan_id;
        String str = "select * from zhongduanuser where shenfennum in"+ "(select zhongduanuser_id from zhongduan_zduser where zhongduan_id ="+zhongduan_id+")";
        List<User>  userList = new ArrayList<User>();
        
        try {            
            Connection con = dao.getCon();
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery(str);
            while (rs.next()) {
                 User user = new User();
                 user.setShenfennum(rs.getString("shenfennum"));
                 user.setName(rs.getString("name"));
                 user.setRegtime(rs.getString("regtime"));
                 user.setAddress(rs.getString("address"));
                 user.setBirthdate(rs.getString("birthdate"));
                 userList.add(user);
            }
                        
            
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }
        return userList;                
    }

}
View Code
复制代码

 

servlet

复制代码
package com.example.hixin;

import java.io.IOException;

import java.io.PrintWriter;
import java.lang.reflect.Type;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


/**
 * @author Administrator
 *
 */
public class Servlet extends HttpServlet{
    /**
     * Constructor of the object.
     */
    private String zhongduan_id;
    private String userid;
    private String type;
    private String data;
    private List<User>  userList = new ArrayList<User>();
    Gson gson = new Gson();

    public Servlet() {
        super();
        System.out.println("Servlet()");
    }

    /*
       http://127.0.0.1/server/Servlet?zhongduan_id=11223344
       http://127.0.0.1/server/Servlet?userid=412728199005233676&type=bloodpre
    */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        this.zhongduan_id = req.getParameter("zhongduan_id");
        this.userid = req.getParameter("userid");
        this.type = req.getParameter("type");
        this.data = req.getParameter("data");
        System.out.println("打印Servlet中的设备ID:" + this.zhongduan_id);
        System.out.println("打印Servlet中的userid:" + this.userid);
        System.out.println("打印Servlet中的数据:" + this.data);
        Service service = new Service();
        
        PrintWriter out = resp.getWriter();

        
            
        if(service.canLogin(zhongduan_id)){
            String s1 = "success!!!";
            System.out.println("登陆验证成功");
            
            userList = service.getUser(zhongduan_id);
            String s2 = gson.toJson(userList);
            System.out.println("带泛型的list转化为json==" + s2);
            out.print(s1+s2);
           /* HashMap<String, String> infos = new HashMap<String, String>();
            infos.put("CODE", s1);
            infos.put("USER", s2);
            String s = gson.toJson(infos);
            System.out.println("infos = " + s);*/                                     
        }
        
        if(service.isUserRegistered(userid)) {
              String s1 = "userid!!!";
              out.print(s1);
                Gson gson = new Gson();
                Type type = new TypeToken<BloodPre>(){}.getType();
                BloodPre bloodpre = new BloodPre();
                bloodpre = gson.fromJson(data, type);
                service.insrtDataToTable(bloodpre);
        }
        
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);

    }

    
    
    
    
    
}
View Code
复制代码

 

bloodpre

复制代码
package com.example.hixin;

public class BloodPre {
        private String userid;  
        private String time;
        private int highp;  
        private int lowp;
        private int pulse;
          
        public BloodPre(){
            
        }

        public String getUserid() {
            return userid;
        }

        public void setUserid(String userid) {
            this.userid = userid;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public int getHighp() {
            return highp;
        }

        public void setHighp(int highp) {
            this.highp = highp;
        }

        public int getLowp() {
            return lowp;
        }

        public void setLowp(int lowp) {
            this.lowp = lowp;
        }

        public int getPulse() {
            return pulse;
        }

        public void setPulse(int pulse) {
            this.pulse = pulse;
        }
        
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "BloodPre [userid=" + userid + ", time=" + time + ", highp=" + highp + ", lowp=" + lowp + ", pulse=" + pulse + "]";
        }
       
}
View Code
复制代码

 

 

复制代码
package com.example.hixin;

public class BloodPre {
        private String userid;  
        private String time;
        private int highp;  
        private int lowp;
        private int pulse;
          
        public BloodPre(){
            
        }

        public String getUserid() {
            return userid;
        }

        public void setUserid(String userid) {
            this.userid = userid;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public int getHighp() {
            return highp;
        }

        public void setHighp(int highp) {
            this.highp = highp;
        }

        public int getLowp() {
            return lowp;
        }

        public void setLowp(int lowp) {
            this.lowp = lowp;
        }

        public int getPulse() {
            return pulse;
        }

        public void setPulse(int pulse) {
            this.pulse = pulse;
        }
        
        @Override
        public String toString() {
            // TODO Auto-generated method stub
//            return "BloodPre [userid=" + userid + ", time=" + time + ", highp=" + highp + ", lowp=" + lowp + ", pulse=" + pulse + "]";
            return "\'"+userid+"\',\'"+time+"\',\'"+highp+"\',\'"+lowp+"\',\'"+pulse+"\'";
        }
       
}
View Code
复制代码

 进一步优化版本:

service:

复制代码
package com.example.hixin;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


/**
 * Service
 * @author wzx
 * 是MVC中的控制层
 */

public class Service {    
    Dao dao = new Dao();
    /**根据设备号在数据库中查找,该设备号存在,则可以登录
     * @param zhongduan_id
     * @return true 可以登录,false 不可以登录
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public boolean canLogin (String id){        
        String str = "select zhongduan_id from zhongduan_zduser where zhongduan_id ="+id;
        try {            
            Connection con = dao.getCon();
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery(str);
            if (rs.next()) {
                return true;
            }
            
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }
        return false;
    }
    
    
    public boolean isUserRegistered(String userid){        
        String str = "select shenfennum from user2 where shenfennum like "+userid;
        try {            
            Connection con = dao.getCon();
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery(str);
            if (rs.next()) {
                return true;
            }
            
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }
        return false;
    }
    
    public boolean insrtDataToTable(BloodPre bloodpre) {
        try {
            Connection con = dao.getCon();
            Statement statement = con.createStatement();
//            String  str = "INSERT INTO xueya2(userid,regdate,shousuo,shuzhang,maibo) VALUES("+bloodpre.toString()+")";
            String  str = "INSERT INTO xueya2 VALUES("+null+","+bloodpre.toString()+")";
            statement.execute(str);
            return true;
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        
        
    }
    
    /**
     *  rs.getString(0);//取第一列数据 
     *  rs.getString("id");//取列名为"id"列的数据       
     * 
     * @param zhongduan_id 根据设备ID拉取使用该设备的所有人
     * @return 
     */
    public  List<User> getUser(String zhongduan_id){
        String strCount = "select count(*) as zhongduanuser_id from zhongduan_zduser where zhongduan_id ="+zhongduan_id;
        String str1 = "select zhongduanuser_id from zhongduan_zduser where zhongduan_id ="+zhongduan_id;
        String str = "select * from zhongduanuser where shenfennum in"+ "(select zhongduanuser_id from zhongduan_zduser where zhongduan_id ="+zhongduan_id+")";
        List<User>  userList = new ArrayList<User>();
        
        try {            
            Connection con = dao.getCon();
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery(str);
            while (rs.next()) {
                 User user = new User();
                 user.setShenfennum(rs.getString("shenfennum"));
                 user.setName(rs.getString("name"));
                 user.setRegtime(rs.getString("regtime"));
                 user.setAddress(rs.getString("address"));
                 user.setBirthdate(rs.getString("birthdate"));
                 userList.add(user);
            }
                    
            
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }
        
        return userList;                
    }

}
View Code
复制代码

Servlet:

复制代码
package com.example.hixin;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;


/**
 * @author Administrator
 *
 */
public class Servlet extends HttpServlet{
    /**
     * Constructor of the object.
     */
    private String zhongduan_id;
    private String userid;
    private String type;
    private String data;
    private List<User>  userList = new ArrayList<User>();
    Gson gson = new Gson();

    public Servlet() {
        super();
        System.out.println("Servlet()");
    }

    /*
       http://127.0.0.1/server/Servlet?zhongduan_id=11223344
       http://127.0.0.1/server/Servlet?userid=412728199005233676&type=bloodpre
    */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        this.type = req.getParameter("type");
        
        Service service = new Service();        
        PrintWriter out = resp.getWriter();
        
        if(type.equalsIgnoreCase("login")) {
            this.zhongduan_id = req.getParameter("zhongduan_id");
            System.out.println("打印Servlet中的设备ID:" + this.zhongduan_id);
             if(service.canLogin(zhongduan_id)){
                    String s1 = "login!!!";
                    System.out.println("登陆验证成功");
                    
                    userList = service.getUser(zhongduan_id);
                    String s2 = gson.toJson(userList);
                    System.out.println("带泛型的list转化为json==" + s2);
                    out.print(s1+s2);
                   /* HashMap<String, String> infos = new HashMap<String, String>();
                    infos.put("CODE", s1);
                    infos.put("USER", s2);
                    String s = gson.toJson(infos);
                    System.out.println("infos = " + s);*/                                     
              }
            
            
        }else if(type.equalsIgnoreCase("bloodpre")){
            this.userid = req.getParameter("userid");    
            this.data = req.getParameter("data");
            
            System.out.println("打印Servlet中的userid:" + this.userid);
            System.out.println("打印Servlet中的数据:" + this.data);
            if(service.isUserRegistered(userid)) {
               
                Gson gson = new Gson();
                BloodPre bloodpre = new BloodPre();
                bloodpre = gson.fromJson(data, BloodPre.class);
                System.out.println(bloodpre.toString());
                if(service.insrtDataToTable(bloodpre)){
                     String s1 = "bloodpre!!!";
                     out.print(s1);
                }else{
                     out.print("fail");
                }
            }
            
        }
                  
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);

    }

    
    
    
    
    
}
View Code
复制代码

 标准json封装:

复制代码
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainMeActivity extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            try {
                DatatoJSON(req, resp);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }

    public void DatatoJSON(HttpServletRequest request,
            HttpServletResponse response) throws IOException, JSONException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        request.setCharacterEncoding("UTF-8");
        JSONObject ret = new JSONObject();
        String type = request.getParameter("type");
        String id = request.getParameter("userID");
        long userID = Long.parseLong(id);
        if (type.equals("favoriate")) {
            List<Favoriate> problems = new GenData().gendata();
            if (problems != null) {
                JSONArray questonsArray = new JSONArray();
                for (Favoriate tempProblem : problems) {
                    JSONObject tempProblemJson = new JSONObject();
                    tempProblemJson.put("favoriateID", tempProblem.getId());
                    tempProblemJson.put("Title", tempProblem.getTitle());
                    tempProblemJson.put("question_content",
                            tempProblem.getQuestion_content());
                    tempProblemJson.put("Tag", tempProblem.getLable());                
                    tempProblemJson.put("problemID", tempProblem.getQuesID());
                    questonsArray.put(tempProblemJson);
                }

                if (questonsArray != null) {
                    ret.put("type", "favoriate");
                    ret.put("num", questonsArray.length());
                    ret.put("questions", questonsArray);
                    ret.put("Status", "success");
                } 
            }

        }
        
        response.getWriter().write(ret.toString());

    }

}
复制代码

如下:

{"Status":"success","num":20,"questions":[{"favoriateID":0,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":0,"problemID":2},{"favoriateID":1,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":1,"problemID":3},{"favoriateID":2,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":2,"problemID":4},{"favoriateID":3,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":3,"problemID":5},{"favoriateID":4,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":4,"problemID":6},{"favoriateID":5,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":5,"problemID":7},{"favoriateID":6,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":6,"problemID":8},{"favoriateID":7,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":7,"problemID":9},{"favoriateID":8,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":8,"problemID":10},{"favoriateID":9,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":9,"problemID":11},{"favoriateID":10,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":10,"problemID":12},{"favoriateID":11,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":11,"problemID":13},{"favoriateID":12,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":12,"problemID":14},{"favoriateID":13,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":13,"problemID":15},{"favoriateID":14,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":14,"problemID":16},{"favoriateID":15,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":15,"problemID":17},{"favoriateID":16,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":16,"problemID":18},{"favoriateID":17,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":17,"problemID":19},{"favoriateID":18,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":18,"problemID":20},{"favoriateID":19,"Title":"唐嫣山寨礼服","question_content":"上周末,上海电影节正式开幕。原本星光熠熠的红毯秀最后却被唐嫣一条山寨礼服抢占头条刷爆朋友圈","Tag":19,"problemID":21}],"type":"favoriate"}

 Json解析:

复制代码
private List<Answer> jsonToAns(String jsons){
        
        try {            
            JSONObject jsonObject = new JSONObject(jsons);
            String status = jsonObject.getString("Status");
            if(status.equals("success")){
                int num = jsonObject.getInt("num");
                if(num != 0) {
                    MyApplication.context().ansnum =num;
                    Intent broadcast = new Intent("myInfoNumChange");  
                    getActivity().sendBroadcast(broadcast);  
                    
                }
                String sanswers = jsonObject.getString("answers");
                Log.i("sanswers",sanswers);
                JSONArray ansArray = new JSONArray(sanswers);
                List<Answer> answers = new ArrayList<Answer>();
                for(int i=0; i< ansArray.length(); i++){
                    JSONObject ansObject = ansArray.getJSONObject(i);
                    String sques = ansObject.getString("question_content");
                    String sans = ansObject.getString("answer_content");
                    Log.i("sques", sques);
                    String stag = ansObject.getString("Tag");
                    int quesid  = ansObject.getInt("problemID");
                    int ansid = ansObject.getInt("answerID");
                    int lable = Integer.parseInt(stag);
                    Answer ans= new Answer(ansid, quesid, lable, sques ,sans);
                    Log.i("ans", ans.toString());
                    answers.add(ans);
                }
                Log.i("anslist", answers.toString());
                return answers;
            }                        
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }
        List<Answer> answers = new ArrayList<Answer>();
        return answers;
    }
复制代码

 

posted @   疾风剑  阅读(506)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示