1

在我的 android 应用程序中,我使用以下代码复制特定表中的整个数据。但是当我检查 servlet 输出时,它似乎会生成重复值。您可以通过运行以下代码来验证这一点。我不知道为什么会这样。我需要将 servlet 中的值作为 JSON 对象传递给我的应用程序。

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
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 org.json.JSONObject;

public class vlgmas extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        // TODO output your page here
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet vlgmas</title>");  
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet vlgmas at " + request.getContextPath () + "</h1>");
        out.println("</body>");
        out.println("</html>");

    } finally {            
        out.close();
    }
}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
     try {

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet Pickvalue</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet Pick at " + request.getContextPath () + "</h1>");
        doPost(request,response);
        out.println("</body>");
        out.println("</html>");

    } finally
    {
        out.close();
    }
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //processRequest(request, response);
    String vname = null,vcode=null,divsec=null;
    JSONObject obj = new JSONObject();
    List list = new ArrayList();
    JSONObject obj2 = new JSONObject();
    PrintWriter out = response.getWriter();
    String vn[]=new String[2000],vc[]=new String[2000],ds[]=new String[2000];
    int t,r1=0,r2=0,r3=0;
    try
    {
        Class.forName("oracle.jdbc.driver.OracleDriver");
    java.sql.Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.200:1521:pdsm", "test", "test");
    Statement stmt=conn.createStatement();

        {
        ResultSet rs = stmt.executeQuery("select * from cn_vlgmas");
        while(rs.next())
        {
            //vn[r1++]=rs.getString ("vlgname");
            //vc[r2++]=rs.getString("vlgcode");
            //ds[r3++]=rs.getString("divsec");
            vname=rs.getString ("vlgname");
            vcode=rs.getString("vlgcode");
            divsec=rs.getString("divsec");
            obj.put("vname",vname);
            obj.put("vcode",vcode);
            obj.put("divsec",divsec);
            list.add(obj);
            obj2.accumulate("get", list);
            out.println(obj2);
        }
       /*for(t=0;t<r1;t++)
           {
            //out.println(vn[t]+","+vc[t]+","+ds[t]+" ");
            obj.put("vname",vn[t]);
            obj.put("vcode",vc[t]);
            obj.put("divsec",ds[t]);
            list.add(obj);
            //obj.append("rec", obj);
            //out.println(list);
            //obj.accumulate("get", list);
            //out.println(obj);
            obj2.accumulate("get", obj);
            //out.println(obj2);
            }*/


        }

    }catch(Exception e)
    {
        out.println("error"+e.toString());
    }
}


@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>
}

谁能帮助我。

4

2 回答 2

0

move this following statement out of while loop

  out.println(obj2);

OR

Instead of JSonObject use JSONArray

于 2013-01-31T09:01:24.700 回答
0

由用户更改2021114

添加

obj = new JSONObject();

在While循环中

while(rs.next())
    {
            obj = new JSONObject();
            vname=rs.getString ("vlgname");
            vcode=rs.getString("vlgcode");
            divsec=rs.getString("divsec");
            obj.put("vname",vname);
            obj.put("vcode",vcode);
            obj.put("divsec",divsec);
            list.add(obj);

    }
obj2.accumulate("get", list);
out.println(obj2); 
于 2013-01-31T09:12:48.520 回答