2011-11-18 155 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方法标题