The Content-Type for your POST is "application/x-www-form-urlencoded".
Instead of letting the servlet container parse it, you seem to be
wanting to read it yourself.
Don't do that. Let the servlet container take care of that. Use the
POST method instead of get. Define doPost() instead of doGet(). Call
request.getParameter("XML") instead of request.getReader(), getting a
string instead of a stream. Parse that.
On Apr 28, 1:38 am, Andres <[email protected]> wrote:
> hi all,
> My android app scan avaible wifi access points and generate a xml file
> with that.
> Then i send it trough a http post message to a servlet, which gets the
> message body with de xml content y insert its content to a DB.
>
> My problem is that I cant get the http post body in the servlet. I get
> something like this instead:
>
> XML=%3C%3Fxml+version%3D%271.0%27+encoding%3D%27UTF-8%27+standalone%3D
> %27yes%27+%3F%3E%3CRedesDetectadas%3E%3CPosicion+Nombre%3D%22RiverPlate
> %22+PuntoAcceso%3D%2200%3A18%3A9b%3A05%3A67%3Ae0%22+Senyal%3D
> %22-87%22+Empleado%3D%22EM1%22+Dia%3D%2228%2F3%2F2010%22+Hora%3D
> %2210%3A31%22+%2F%3E%3CPosicion+Nombre%3D%22Arquitectos%22+PuntoAcceso
> %3D%2200%3A1f%3Ac6%3Ad5%3A9d%3Add%22+Senyal%3D%22-93%22+Empleado%3D
> %22EM1%22+Dia%3D%2228%2F3%2F2010%22+Hora%3D%2210%3A31%22+%2F%3E%3C
> %2FRedesDetectadas%3E\n
>
> I think it's sth to be with the encoding, but i couldnt solve it.
>
> Here is the android app code(the part that makes the connection):
>
> public String executeHttpPost() throws Exception{
> BufferedReader in = null;
> String result="";
> try {
> String xml = XMLHelper.crearXML(results);
> HttpClient client = new DefaultHttpClient();
> HttpPost request =
> new HttpPost("http://192.168.0.10:8080/ServidorWebSupervisorV1/
> ServletSupervisorV1");
>
> List<NameValuePair> postParamaters = new
> ArrayList<NameValuePair>();
> postParamaters.add(new BasicNameValuePair("XML",
> xml));
> UrlEncodedFormEntity formEntity = new
> UrlEncodedFormEntity(postParamaters);
>
> request.setHeader("Host", "EM1");
>
> request.setHeader("Content-Type","application/x-www-form-
> urlencoded");
> request.setEntity(formEntity);
>
> String f = EntityUtils.toString(request.getEntity());
>
> HttpResponse response = client.execute(request);
> in = new BufferedReader(new
> InputStreamReader(response.getEntity().getContent()));
> StringBuffer sb = new StringBuffer("");
> String line = "";
> String NL = System.getProperty("line.separator");
> while((line = in.readLine())!= null)
> sb.append(line + NL);
> in.close();
>
> result = sb.toString();
> }
> catch(Exception e){
> String m = e.getMessage();
> }
> return result;
> }
>
> I debugged it and the xml string is well generated.
>
> The servlet code is to get the body is:
>
> protected void doGet(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>
> if(request.getHeader("Host").equals("EM1")){
> PrintWriter out = response.getWriter();
>
> System.out.println("tamano:
> "+request.getContentLength());
> System.out.println("metodo:"+request.getMethod());
> System.out.println("path:"+request.getContextPath());
>
> System.out.println("address:"+request.getRemoteAddr());
> System.out.println("metodo:"+request.getMethod());
>
> String body="", aux="";
> BufferedReader reader = request.getReader();
> while((aux = reader.readLine())!=null)
> body = body + aux + "\n";
>
> InputSource source = new InputSource(reader);
>
> try {
> SAXParserFactory spf =
> SAXParserFactory.newInstance();
> javax.xml.parsers.SAXParser sp =
> spf.newSAXParser();
> XMLReader xr = sp.getXMLReader();
> XMLHandler handler = new XMLHandler();
> xr.setContentHandler(handler);
> xr.parse(new
> InputSource(request.getReader()));
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
>
> thanks everybody!
>
> --
> 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
> athttp://groups.google.com/group/android-developers?hl=en
--
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