0

我正在开发一个应用程序,我需要将一些数据从服务器数据库复制到 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 条记录中的每一条检索每个村庄名称、村庄代码和分区代码。任何人都可以帮助我做到这一点。

提前致谢

4

3 回答 3

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

在你的安卓

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();
    }

希望这真的对你有帮助

于 2013-01-31T09:45:49.243 回答
0

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

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

developer.android 的一个小教程

于 2013-01-31T06:24:23.333 回答
0

只需传递JSON 对象。

创建一个while循环并将值添加到列表中,然后您只需获得一个数组

于 2014-10-14T17:36:58.337 回答