2011-11-18 157 views
11

我的Galaxy Nexus今天抵達,我做的第一件事之一是將我的應用程序加載到它上面,以便我可以向我的朋友展示它。其部分功能涉及從Google閱讀器導入RSS源。但是,嘗試此操作後,我得到了405方法不允許的錯誤。Android 4.0 ICS將HttpURLConnection GET請求轉變爲POST請求

這個問題是特定於冰淇淋三明治。我附上的代碼在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; 
    } 
} 

是否有任何可能導致此問題的原因?這個函數可以更好地編碼以避免這個問題嗎?

非常感謝提前。

回答

8

擺脫這樣的:

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

這告訴API,這是一個POST。

UPDATE它如何能夠通過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(); 
    } 
} 
+0

沒有。刪除該行完全沒有區別。 –

+1

好吧,那麼你也可以嘗試去與HttpClient。 –

+0

@邁克爾多德:檢查更新部分 –

10

刪除這條線的工作對我來說:

connection.setDoOutput(true); 

4.0認爲這條線也絕對應該是POST。

+2

感謝Fedor,它也爲我工作。我有與GET方法相同的問題。當我使用setDoOutPut(true)調用GET方法時,我得到了Http 405:Method Not Allowed消息被返回。再次感謝。 – PrashantAdesara

2

我發現,預ICS人能逃脫使身體無支柱沒有提供Content-Length值,但後期ICS必須設置的Content-Length:0

3

這是一個讓我 - 基本設置setDoOutput(真),它迫使一個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方法標題