0

我想从服务器向客户端发送非常大的数据,服务器是 tomcat Java,客户端是 android 应用程序,我正在使用 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();

安卓

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

1 回答 1

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;
}
于 2012-06-29T06:33:29.660 回答