从Java servlet向Android应用程序发送大量数据

时间:2012-06-29 06:09:40

标签: android servlets

我想从服务器向客户端发送非常大的数据,服务器是tomcat Java,客户端是android应用程序,我正在使用servlet

服务器servlet

protected void doPost(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException,
                                                           IOException {
    ServletOutputStream out = response.getOutputStream();
    CellDatabase cDB = new CellDatabase();
    String[] cells = cDB.getAllCells();
    for (int i = 0; i < cells.length; i++)
        out.write(cells[i].getBytes());
    out.flush();
}

我的问题是:我怎么能在android上获得这些数据,因为我找不到像

这样的东西
response.getOutputStream();

android

HttpClient client = new DefaultHttpClient();
website = new URI(
        "http://10.0.2.2:8080/LocalizedBasedComptitionServer/GetCells");
HttpPost request = new HttpPost();
request.setURI(website);
HttpResponse response = client.execute(request);

1 个答案:

答案 0 :(得分:3)

这可能会对你有所帮助

public static String getData(String url) {

    System.out.println("Connecting to service URL : " + url);
    InputStream is = null;
    String result = "";
    // http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {
    }

    // convert response to string
    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();
        result = sb.toString();
    } catch (Exception e) {
    }

    return result;
}