2013-04-03 8 views
9

Vedo un errore StrictMode e non riesco a vedere cosa c'è che non va in quello che sto facendo. Il fallimento è:Android StrictMode Throwable: metodo di terminazione esplicita 'end' non chiamato

java.lang.Throwable: Explicit termination method 'end' not called 
    E/StrictMode(26875): at dalvik.system.CloseGuard.open(CloseGuard.java:184) 
    E/StrictMode(26875): at java.util.zip.Inflater.<init>(Inflater.java:82) 
    E/StrictMode(26875): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:96) 
    E/StrictMode(26875): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81) 
    E/StrictMode(26875): at libcore.net.http.HttpEngine.initContentStream(HttpEngine.java:528) 
    E/StrictMode(26875): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:836) 
    E/StrictMode(26875): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274) 
    E/StrictMode(26875): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486) 
    E/StrictMode(26875): at com.mycompany.MyClass.sendJson(MyClass.java:267) 

configura StrictMode:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() 
       .detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog() 
        .penaltyDeath().build()); 
} 

Il codice simile a quanto segue:

HttpURLConnection connection = null; 
OutputStream outputStream = null; 
InputStream in = null; 

try { 
     connection = (HttpURLConnection)new URL("http://our.server/some/path").openConnection(); 

     connection.setRequestProperty("Accept", "application/json"); 
     connection 
       .setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
     connection.setDoOutput(true); 

     outputStream = connection.getOutputStream(); 

     String content = "{\"some\":\"json data\"}"; 
     byte bytes[] = content.getBytes("UTF-8"); 
     outputStream.write(bytes); 
     outputStream.flush(); 

     responseCode = connection.getResponseCode(); // This is line 267 in the stack trace 

     if (HttpURLConnection.HTTP_OK == responseCode 
       || HttpURLConnection.HTTP_CREATED == responseCode) { 

      // do something with a successful response 
     } 
    } 

} catch (IOException e) { 
    // report the exception 
} finally { 
    if (null != connection) { 
     connection.disconnect(); 
    } 
    if (null != outputStream) { 
     try { 
      outputStream.close(); 
     } catch (IOException e) { 
     } 
    } 
    if (null != in) { 
     try { 
      in.close(); 
     } catch (IOException e) { 
     } 
    } 
} 

ho trovato lamentarsi di vedere lo stesso fallimento StrictMode quando si utilizza Google qualcuno AdMob SDK (https://groups.google.com/forum/?fromgroups=#!topic/google-admob-ads-sdk/yVss4ufdPp4) ma non sono stato in grado di trovare alcuna soluzione finora.

+0

Provato a (sulla risposta HTTP gziped), ma non è possibile riprodurre l'eccezione. Stai impostando una [politica StrictMode] esplicita (http://developer.android.com/reference/android/os/StrictMode.html)? Qual è la tua versione di Android SDK? – minipif

+0

Ho modificato la domanda per mostrare come sto configurando StrictMode –

+0

Hai mai trovato una soluzione a questo? Per me sembra un bug in GZIPInputStream di Android, dal momento che crea un Inflater, registra una guardia stretta per "end", ma in realtà non chiama mai end. – Matthias

risposta

3

Cosa succede se si riceve il flusso di risposta, lo si legge completamente e lo si chiude (anche se il codice di risposta non è OK).
Credo questo richiamerà HttpEngine.release(), e quindi evitare l'eccezione quando il GzipInputStream viene successivamente finalizzato (come l'eccezione viene preparata nel costruttore ma gettato in finalize())

+1

Darò quel via ... –

+0

Questo sembrava promettente, ma non ha funzionato per me, in un pezzo simile a Jonathan. Testato con 4.1.2 su un Nexus S. –

+0

È interessante notare [suggerimento e correzione simili] (https://github.com/square/okhttp/pull/24/files#diff-a717786a862ad3d4b9846a5168428cc9R96) in [okhttp] (https://github.com/square/okhttp). –

2

Credo connection.getResponseCode() apre un flusso di input. Quindi dovrai recuperare lo stream di input e chiuderlo.

+1

Questo ha risolto il mio problema StrictMode simile! Grazie!! Snippet di codice: connection.getInputStream(). Close() –

+0

@PaniniLuncher grazie funziona per me. Sto ottenendo questo simile a causa di 'connection.getContentLength()'. Questo. – Dharmishtha

Problemi correlati