2012-09-05 112 views
1

我有一個web服務,可以接受帶有多部分的文件(在c#中),當我通過鉻擴展名(高級休息擴展名)發送大文件(15MB)時,文件上傳正常,我可以看到身體反應:從httpmultipart請求獲得xml響應

<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:試圖從封閉流中讀取。

我如何獲得解析它的響應? 謝謝

回答

0

看起來像你不應該叫consumeContent(),直到信息被讀取(鑑於它關閉流)。如果您使用4.1或更高版本,此方法已被棄用,並且你應該使用EntityUtils.consume(HttpEntity)

從它的javadoc:

這個方法被調用,以表明該實體的內容不再需要。作爲此方法調用的結果,所有實體實現都應該釋放所有分配的資源。內容流媒體實體也有望處置剩餘的內容(如果有的話)。包裝實體應將此調用委託給包裝的實體。