Hi,

I am using a urlconnection method to connect to a server (here
localhost) which has a servlet running (HelloServlet). Could some one
please tell me how do I receive the contents that the client wrote on
the outputstream.


Here is my client code
try{
                        URL url = new URL("http://10.0.2.2/HelloServlet";);
                    URLConnection conn = url.openConnection();
                    conn.setDoOutput(true);

                    Toast.makeText(getBaseContext(),
                    "connection set",
                    Toast.LENGTH_SHORT).show();
                    OutputStreamWriter writer = new
OutputStreamWriter(conn.getOutputStream());

                    writer.write("value=hello&anotherValue=imran");
                    writer.flush();
                    Toast.makeText(getBaseContext(),
                    "sent the data",
                    Toast.LENGTH_SHORT).show();
                    String line;
                    BufferedReader reader = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
                    while ((line = reader.readLine()) != null) {
                      System.out.println(line);
                      Toast.makeText(getBaseContext(),
                      "received from server"+line,
                      Toast.LENGTH_SHORT).show();
                    }
                    writer.close();
                    reader.close();
                }
                catch (Exception e){

                }

The server code is

protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String value = request.getParameter("value");
            String value2 = request.getParameter("anotherValue");
            System.out.println("The data recieved is:"+value+" and
"+value2);


            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet LoginServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("The data recieved is:"+value+" and
"+value2);
            out.println("</body>");
            out.println("</html>");

        } finally {
            out.close();
        }
    }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to