Question

I have a webservice that receibes a files with multipart (In c#), when i send a big file(15MB) by a chrome extension(Advanced Rest extension) the file uploads ok and i can see the body response:

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

But when i call with android i get the headers with an 200 code OK response but i dont know how can i get the body response:

This is my code:

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

Crash with: java.io.IOException: Attempted read from closed stream.

How can i get the response to parse it? Thanks

Was it helpful?

Solution

Looks like you shouldn't be calling consumeContent() until the information is read (given that it closes the stream). If you're using 4.1 or later, this method has been deprecated, and you should be using EntityUtils.consume(HttpEntity)

From its 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top