java - 从 httpmultipart 请求获取 xml 响应

标签 java android web-services http multipartentity

我有一个网络服务,它接收多部分文件(在c#中),当我通过chrome扩展(高级休息扩展)发送一个大文件(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 code OK 响应的 header ,但我不知道如何获得正文响应:

这是我的代码:

/****** 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:尝试从关闭的流中读取。

我怎样才能得到响应来解析它? 谢谢

最佳答案

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

来自其 javadoc:

This method is called to indicate that the content of this entity is no longer required. All entity implementations are expected to release all allocated resources as a result of this method invocation. Content streaming entities are also expected to dispose of the remaining content, if any. Wrapping entities should delegate this call to the wrapped entity.

关于java - 从 httpmultipart 请求获取 xml 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12278651/

相关文章:

java - ObjectInputStream readObject 导致 RoundingMode.ordinal NullPointerException

android - 如何在 Android 上重复使用 AlertDialog 表示是/否?

android - ListView 中的图像产生内存不足

java - Jax-WS 接口(interface)作为参数

web-services - 在单个 WSDL 文件中定义多个服务是什么意思

c# - Asp.Net Ajax WebService 和内部错误 500

java - Fisher-Yates 洗牌时 Java 堆栈中留下的元素?

java - 在 mongodb spring-data 中使用 dbref 保存文档

java - 处理我的应用程序中的网站页面

android - 如何通过 Intent 发送对象