Android的,发送并通过HTTP POST方法接收XML

问题描述:

还有一个相关的问题,但我无法得到答案清楚了。

There is a relevant question, but I could not get the answer clearly.

我想发布一个简短的XML code

I would like to POST a short xml code

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

要通过HTTP特定的URL地址。 Web服务会送我回一个XML code。最重要的是,我会解析接收到的XML,我想存储为一个文件。

to a specific URL address over HTTP. The Web service will send me back a XML code. The important part is that I will parse the received XML, and I want to store that as a file.

    // 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

有什么不对的接收响应。此外,我没有写任何接收到的XML保存为一个文件。一个人可以写一个code段?

there is something wrong with receiving the response. Besides, I did not write anything to save the received XML as a file. Can someone write a code snippet?

好吧,我已经想通了,不久后,我张贴了这个问题。 这code在这里工作得很好:

Ok, I have figured out soon after I posted this question. This code here works fine:

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