Android 4.0 ICS превращает запросы HttpURLConnection GET в запросы POST

Мой Galaxy Nexus прибыл сегодня, и одним из первых шагов, которые я сделал, было загрузить в него свое приложение, чтобы я мог продемонстрировать его своим друзьям. Часть его функций включает импорт RSS-каналов из Google Reader. Однако, попробовав это, я получал ошибку 405 Method Not Allowed.

Эта проблема связана с Ice Cream Sandwich. Код, который я прикрепил, отлично работает с Gingerbread и Honeycomb. Я проследил ошибку до момента установления соединения, когда запрос GET волшебным образом превращается в запрос POST.

/**
 * Get the authentication token from Google
 * @param auth The Auth Key generated in getAuth()
 * @return The authentication token
 */
private String getToken(String auth) {
    final String tokenAddress = "https://www.google.com/reader/api/0/token";
    String response = "";
    URL tokenUrl;

    try {
        tokenUrl = new URL(tokenAddress);
        HttpURLConnection connection = (HttpURLConnection) tokenUrl.openConnection();

        connection.setRequestMethod("GET");
        connection.addRequestProperty("Authorization", "GoogleLogin auth=" + auth);
        connection.setRequestProperty("Content-Type","application/x-www-form-urlendcoded");
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        Log.d(TAG, "Initial method: " + connection.getRequestMethod()); // Still GET at this point

        try {
            connection.connect();
            Log.d(TAG, "Connected. Method is: " + connection.getRequestMethod());  // Has now turned into POST, causing the 405 error
            InputStream in = new BufferedInputStream(connection.getInputStream());
            response = convertStreamToString(in);
            connection.disconnect();
            return response;

        }
        catch (Exception e) {
            Log.d(TAG, "Something bad happened, response code was " + connection.getResponseCode()); // Error 405
            Log.d(TAG, "Method was " + connection.getRequestMethod()); // POST again
            Log.d(TAG, "Auth string was " + auth);
            e.printStackTrace();
            connection.disconnect();
            return null;
        }
    }
    catch(Exception e) {
        // Stuff
        Log.d(TAG, "Something bad happened.");
        e.printStackTrace();
        return null;
    }
}

Есть ли что-нибудь, что могло вызвать эту проблему? Можно ли лучше закодировать эту функцию, чтобы избежать этой проблемы?

Заранее большое спасибо.

11
задан Michael Dodd 18 November 2011 в 18:26
поделиться