5

從谷歌服務器驗證谷歌文檔後,我做了一個簡單的getResponse,但我得到了一個400錯誤的請求。我不明白我哪裏錯了。示例代碼,下面谷歌文檔的Google API,請求文檔列表 - 400錯誤請求

private void executeRefreshAlbums() { 
     HttpRequest request = transport.buildGetRequest(); 
     request.url = GoogleDocsUrl.forDefaultPrivateFull(); 
     System.out.println("URL = "+request.url); 
     try { 
      HttpResponse response = request.execute(); 
      System.out.println("Response = "+response.getContent()); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

的系統輸出打印正確的URL作爲

03-12 17:36:59.573: INFO/System.out(451): URL = https://docs.google.com/feeds/default/private/full 

但是當我這樣做,我得到

03-12 17:43:41.360: WARN/System.err(3958): com.google.api.client.http.HttpResponseException: 400 Bad Request 
03-12 17:43:41.415: WARN/System.err(3958):  at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:209) 
03-12 17:43:41.415: WARN/System.err(3958):  at com.example.Test.executeRefreshAlbums(Test.java:198) 
03-12 17:43:41.415: WARN/System.err(3958):  at com.example.Test.authenticated(Test.java:190) 
03-12 17:43:41.415: WARN/System.err(3958):  at com.example.Test.authenticatedClientLogin(Test.java:156) 
03-12 17:43:41.415: WARN/System.err(3958):  at com.example.Test.access$1(Test.java:153) 
03-12 17:43:41.415: WARN/System.err(3958):  at com.example.Test$2$1.run(Test.java:139) 
03-12 17:43:41.415: WARN/System.err(3958):  at android.os.Handler.handleCallback(Handler.java:587) 
03-12 17:43:41.415: WARN/System.err(3958):  at android.os.Handler.dispatchMessage(Handler.java:92) 
03-12 17:43:41.415: WARN/System.err(3958):  at android.os.Looper.loop(Looper.java:123) 
03-12 17:43:41.415: WARN/System.err(3958):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
03-12 17:43:41.415: WARN/System.err(3958):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-12 17:43:41.415: WARN/System.err(3958):  at java.lang.reflect.Method.invoke(Method.java:521) 
03-12 17:43:41.422: WARN/System.err(3958):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
03-12 17:43:41.422: WARN/System.err(3958):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
03-12 17:43:41.422: WARN/System.err(3958):  at dalvik.system.NativeStart.main(Native Method) 

任何幫助將非常感激。我一直在嘗試了一個小時,現在:-(

+0

同樣的問題也u得到任何解決方案plz幫助? – Harinder 2011-08-30 07:55:55

+1

@所有的解釋請參考http://stackoverflow.com/q/7230435/689853 – Harinder 2011-08-30 08:00:02

回答

2

在另一個線程user @Merlin provided解決方案解決了類似的問題。他的解決方案是在請求中指定API版本號3.0,如「Google Documents List Data API」doc ument:

重要:如果您在僅使用 中使用的功能在3版本的API請求,但忘了設置的版本,你會最 可能收到400錯誤的請求響應。由於版本2和3之間的URL結構不同,因此 API的新用戶通常會犯這個錯誤。

要指定版本號,請使用 GData-Version HTTP標頭。此標頭的值必須爲3.0。 十進制和零是必需的。

  GData-Version: 3.0

另外, 可以指定v=3作爲URL中的查詢參數。在處理從HTTP請求中去除HTTP頭的防火牆或 時,通常需要使用 。如果可能,建議使用HTTP標頭 。

  https://docs.google.com/feeds/default/private/full?v=3

0

不知道什麼transport你的使用對象是;

下面是一個簡單的請求HTTPGET:

 HttpGet getMethod = new HttpGet(URI); 
     HttpParams params = new BasicHttpParams();       
     HttpConnectionParams.setConnectionTimeout(params, 30000); 
     HttpConnectionParams.setSoTimeout(params, 30000); 

     DefaultHttpClient httpClient = new DefaultHttpClient(params); 
     HttpResponse response  = httpClient.execute(getMethod); 

     BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 
     String returnString = ""; 
     String line = ""; 
     do 
     { 
     returnString += line; 
     line = in.readLine(); 
     } while (line != null); 

也許使用這種方法,而不是你會得到更明顯的錯誤嗎?