Android - HTTP Get - Explicit Termination not call

2019-03-05 08:03发布

问题:

I am working on a fairly simple android app that should get the last modified/updated date for a webpage but it seems to crash with the following message

05-27 10:50:15.581 2638-2652/android.process.acore E/DictionaryBackupAgent﹕ Couldn't read from the cursor

05-27 10:50:20.658 2638-2647/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.

java.lang.Throwable: Explicit termination method 'close' not called

at dalvik.system.CloseGuard.open(CloseGuard.java:184)

at android.os.ParcelFileDescriptor.(ParcelFileDescriptor.java:180)

at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)

at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)

at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:57)

at android.os.Binder.execTransact(Binder.java:446)

A part of my code is below. The getLastModified() method seems to cause this crash, not sure why.

private class Connection extends AsyncTask<String, Void, Integer> {
   @Override
    protected String doInBackground(String... urls) {
        String response = "This";
        URL url = null;
        try {
            url = new URL(urls[0]);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        HttpURLConnection con = null;
        try {
            con = (HttpURLConnection) url.openConnection();
            con.connect();
            long date = con.getLastModified();
            response = Long.toString(date);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        mainText2.setText(result);
    }
}

@Override
public void onClick(View v) {
     Connection task = new Connection();
    task.execute(new String[] { "http://www.example.com/files/index.html" });
}

What am I missing here? Any help is appreciated!