android 接收xml,Android,通过HTTP POST方法发送和接收XML

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

我想POST一个简短的xml代码

通过HTTP到特定的URL地址。 Web服务将向我发回XML代码。 重要的是我将解析收到的XML,并希望将其存储为文件。

// 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("",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作为文件。 有人可以编写代码片段吗?

好的,我在发布这个问题后不久就知道了。 此代码在这里工作正常:

HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://192.168.192.131/"); try { StringEntity se = new StringEntity( "", 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(); }

您可以使用以下方式获取响应的内容:

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

然后,您可以使用类似的东西将其写入文件。

收到回复有问题

既然你没有说接收响应有什么问题,那么就这一点帮助你有点困难。

为什么不在Spring中使用Spring RestTemplate for Android ?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值