问题描述
我的Galaxy Nexus的今天到了,我做的第一件事就是加载我的应用程序上,所以我可以证明它给我的朋友。它的功能包括部分进口RSS订阅,从谷歌阅读器。然而,在尝试此,我得到405不允许的方法错误。
My Galaxy Nexus arrived today, and one of the first things I did was to load my app onto it so I could demonstrate it to my friends. Part of its functionality involves importing RSS Feeds from Google Reader. However, upon trying this, I was getting 405 Method Not Allowed errors.
这个问题是冰淇淋三明治专用。在code我已经连接正常工作的姜饼和蜂窝。我跟踪的错误下来的那一刻建立连接,当GET请求神奇地变成了POST请求。
This problem is Ice Cream Sandwich-specific. The code I've attached works fine on Gingerbread and Honeycomb. I've traced the error down to the moment the connection is made, when the GET request magically turns into a POST request.
/**
* 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;
}
}
这有什么可能会造成这个问题?难道这个功能会更好codeD,以避免这个问题?
Is there anything that could be causing this problem? Could this function be better coded to avoid this problem?
在预先感谢。
推荐答案
此行为在 Android开发人员:HttpURLConnection的
HttpURLConnection的默认使用GET方法。它将使用后,如果 setDoOutput(真)被调用。
有什么奇怪的是,虽然这并没有实际上是直到4.0的行为,所以我会想象它会破坏许多现有的应用程序发布的
What's strange though is that this has not actually been the behaviour until 4.0, so I would imagine it's going to break many existing published apps.
还有更多关于这个在 Android 4.0的轮流进入POST 。
这篇关于安卓4.0 ICS转向HttpURLConnection的GET请求到POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!