从Servlet中检索多个数组

时间:2013-01-31 05:39:15

标签: java android servlets

我正在开发一个应用程序,我需要将一些数据从服务器数据库复制到android中的本地SQLite数据库表。该表包含超过1000条记录。首先,我为此编写了servlet代码。我使用三个数组(用于村名,村代码和分区代码)。我的代码是

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
    {
       //   JSONObject obj1=new JSONObject();

        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");

        }
        for(t=0;t<r1;t++)
            {
                out.println(vn[t]+","+vc[t]+","+ds[t]);
            }

            /*obj.put("vname",vname);
            obj.put("vcode", vcode);
            obj.put("divsec", divsec);
            list.add(obj);
            obj2.accumulate("get", list);
            out.println(obj2);*/
        }

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

/** 
 * Returns a short description of the servlet.
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>
}

它显示了我所期望的所有细节。但我不知道如何在我的Android代码中单独检索这三个数组。我需要检索1000条记录中每条记录的每个村名,村代码和分区代码。任何人都帮助我这样做。

提前致谢

3 个答案:

答案 0 :(得分:1)

您不需要创建数组。只需通过JSON对象。

删除for循环并更改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); 

在你的android

try
{
HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost("servlet path here");
         HttpResponse response = httpclient.execute(httppost);
         HttpEntity entity = response.getEntity();
          is = entity.getContent();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();

    } catch (Exception e) {
    Toast.makeText(getBaseContext(), "Buffer Error"+"Error converting result"+e.toString(), Toast.LENGTH_LONG).show();
    }

    try {
        jObj = new JSONObject(json);
        contacts = jObj.getJSONArray("get");
        for(int i = 0; i < contacts.length(); i++){
        JSONObject c = contacts.getJSONObject(i);
            vcode = c.getString("vcode");
            vname = c.getString("vname");
            divsec = c.getString("divsec");
        }
        ac1.setText(""+vname);
        ac2.setText(""+vcode);
        ac3.setText(""+divsec);

    } catch (JSONException e) {
        Toast.makeText(getBaseContext(), "Error"+e.toString(), Toast.LENGTH_LONG).show();
    }

希望这真的会对你有所帮助

答案 1 :(得分:0)

您是否编写过任何用于检索这些值的webService? 因为在Android中,它是您调用Web服务并获得所需内容的唯一结构。

从DB获取数据后,只需准备一个JSON obj。并写回来。 在Android中,您需要解析该JSON对象。

A small tutorial from developer.android

答案 2 :(得分:0)

只需通过 JSON对象。

创建一个while循环并将值添加到list然后你只需要一个数组