2011-02-16 10 views
8

Hay una pregunta relevante, pero no pude obtener la respuesta con claridad.Android, envíe y reciba XML a través del método HTTP POST

me gustaría publicar un código XML corta

<aaaLogin inName="admin" inPassword="admin123"/> 

a una dirección URL específica a través de HTTP. El servicio web me enviará un código XML. La parte importante es que voy a analizar el XML recibido, y quiero almacenar eso como un archivo.

// Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://192.168.192.131/"); //URL address 

    StringEntity se = new StringEntity("<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>",HTTP.UTF_8); //XML as a string 
    se.setContentType("text/xml"); //declare it as XML 
    httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8"); 
    httppost.setEntity(se); 

    BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient .execute(httppost); 
    tvData.setText(httpResponse.getStatusLine().toString()); //text view is expected to print the response 

hay algún error al recibir la respuesta. Además, no escribí nada para guardar el XML recibido como un archivo. ¿Alguien puede escribir un fragmento de código?

Respuesta

3

Usted puede obtener el contenido de la respuesta usando:

String responseXml = EntityUtils.toString(httpResponse.getEntity()); 

entonces usted puede escribir a un archivo usando algo like this.

hay algo mal con la recepción de la respuesta

Puesto que usted ha dicho havn't lo que está mal con la recepción de la respuesta es un tanto difícil para ayudarle con este punto.

13

Ok, me he dado cuenta poco después de haber publicado esta pregunta. Este código aquí funciona bien:

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://192.168.192.131/"); 

try { 
    StringEntity se = new StringEntity("<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>", HTTP.UTF_8); 
    se.setContentType("text/xml"); 
    httppost.setEntity(se); 

    HttpResponse httpresponse = httpclient.execute(httppost); 
    HttpEntity resEntity = httpresponse.getEntity(); 
    tvData.setText(EntityUtils.toString(resEntity));   
} catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
Cuestiones relacionadas