从httpsultipart请求获取xml响应

时间:2012-09-05 09:47:54

标签: java android web-services http multipartentity

我有一个webpartvice接收带有multipart的文件(在c#中),当我通过chrome扩展(Advanced Rest扩展)发送一个大文件(15MB)时,文件上传正常,我可以看到身体响应:

 <response xmlns="http://schemas.datacontract.org" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Descripcion>blabla</Descripcion>
<Resultado>-9999</Resultado>
<Servicio xmlns:a="http://schemas.datacontract.org" i:nil="true" />
 </reponse>

但是当我用android调用时我会得到带有200个代码的标题OK响应,但我不知道如何才能得到身体反应:

这是我的代码:

/****** Informacion exif *****/

                DefaultHttpClient mHttpClient;
                HttpParams params = new BasicHttpParams();
                params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
                mHttpClient = new DefaultHttpClient(params);
                HttpPost httppost = new HttpPost("HTTPS://SERVICIO.SVC");
                httppost.setHeader("Connection", "Keep-Alive");
                httppost.setHeader("SOAPAction", "http://tempuri.org/Service1");
                httppost.setHeader("identificadorGUID", Guid);
                httppost.setHeader("numeroServicio", codigoServicio);
                httppost.setHeader("coordenadaLatitud", latitud);
                httppost.setHeader("coordenadaLongitud", longitud);
                if (QUEES == 0) {
                    extension = "jpg";

                }
                if (QUEES == 1) {
                    extension = "mp4";

                }
                httppost.setHeader("cuando", cuando);
                httppost.setHeader("tipoMultimedia", String.valueOf(tipoMultimedia));
                httppost.setHeader("extension", extension);

                MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                FileBody fileBody = new FileBody(fichero, "multipart/form-data");
                multipartEntity.addPart("image", fileBody);
                httppost.setEntity(multipartEntity);
                try {
                    HttpResponse response = mHttpClient.execute(httppost);
                    HttpEntity resEntity = response.getEntity();
                    if (resEntity != null) {
                        resEntity.consumeContent();
                    }
                    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                    String sResponse;
                    StringBuilder s = new StringBuilder();
                    httppost.getAllHeaders();

                    while ((sResponse = reader.readLine()) != null) {
                        s = s.append(sResponse);
                    }
                    Log.i("Respuesta web service", sResponse);
                }
                catch (ClientProtocolException e1) {
                    e1.printStackTrace();
                }
                catch (IOException e1) {
                    e1.printStackTrace();
                }

崩溃:java.io.IOException:尝试从封闭流中读取。

我如何得到解析它的响应? 感谢

1 个答案:

答案 0 :(得分:0)

看起来你不应该在读取信息之前调用consumeContent()(假设它关闭了流)。如果您使用的是4.1或更高版本,则此方法已被弃用,您应该使用EntityUtils.consume(HttpEntity)

来自其javadoc:

  

调用此方法表示不再需要此实体的内容。由于此方法调用,所有实体实现都应该释放所有已分配的资源。如果有的话,内容流媒体实体也应该处理剩余的内容。包装实体应该将此调用委托给包装实体。