11

我的 Galaxy Nexus 今天到货了,我做的第一件事就是将我的应用程序加载到它上面,这样我就可以向我的朋友们展示它。它的部分功能涉及从 Google Reader 导入 RSS Feed。但是,在尝试此操作时,我收到了 405 Method Not Allowed 错误。

这个问题是冰淇淋三明治特有的。我附上的代码在 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;
    }
}

有什么可能导致这个问题吗?这个函数可以更好地编码以避免这个问题吗?

提前谢谢了。

4

5 回答 5

20

Android 开发人员中描述了此行为:HttpURLConnection

HttpURLConnection 默认使用 GET 方法。如果 setDoOutput(true) 已被调用,它将使用 POST。

奇怪的是,直到 4.0 才真正成为这种行为,所以我想它会破坏许多现有的已发布应用程序。

Android 4.0 将 GET 变成 POST时,还有更多内容。

于 2011-12-16T18:30:01.447 回答
10

删除这条线对我有用:

connection.setDoOutput(true);

4.0 认为这条线绝对应该是 POST。

于 2011-12-01T08:22:01.020 回答
8

摆脱这个:

connection.setRequestProperty("Content-Type","application/x-www-form-urlendcoded");

这告诉 API 这是一个 POST。

更新如何通过以下方式完成HttpClient

String response = null;
HttpClient httpclient = null;
try {
    HttpGet httpget = new HttpGet(yourUrl);
    httpget.setHeader("Authorization", "GoogleLogin auth=" + auth);
    httpclient = new DefaultHttpClient();
    HttpResponse httpResponse = httpclient.execute(httpget);

    final int statusCode = httpResponse.getStatusLine().getStatusCode();
    if (statusCode != HttpStatus.SC_OK) {
        throw new Exception("Got HTTP " + statusCode 
            + " (" + httpResponse.getStatusLine().getReasonPhrase() + ')');
    }

    response = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);

} catch (Exception e) {
    e.printStackTrace();
    // do some error processing here
} finally {
    if (httpclient != null) {
        httpclient.getConnectionManager().shutdown();
    }
}
于 2011-11-18T18:29:19.573 回答
3

这让我很着迷——基本上是通过设置 setDoOutput(true) 它在你建立连接时强制一个 POST 请求,即使你在 setRequestMethod 中指定这是一个 GET

HttpURLConnection 默认使用 GET 方法。如果 setDoOutput(true) 已被调用,它将使用 POST。其他 HTTP 方法(OPTIONS、HEAD、PUT、DELETE 和 TRACE)可以与 setRequestMethod(String) 一起使用。

这让我想起了一段时间 - 非常令人沮丧......

请参阅http://developer.android.com/reference/java/net/HttpURLConnection.html并转到 HTTP 方法标题

于 2012-08-28T08:45:52.353 回答
2

我发现 pre-ICS 可以在不提供 Content-Length 值的情况下制作无正文的 POST,但是在 ICS 之后,您必须设置 Content-Length: 0。

于 2012-01-10T06:02:35.460 回答