Totti
Totti

Reputation: 675

send huge data from Java servlet to Android application

i want to sent very large data from Server to client, the server is tomcat Java and the client is android application , i am working with servlets

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

my question is : how can i got that data on android , because i didn't find something like

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

Upvotes: 0

Views: 841

Answers (1)

sunil
sunil

Reputation: 6614

this may help you

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

Upvotes: 3

Related Questions