2016-04-05 51 views
-1

當我上傳圖片到谷歌雲存儲,我得到這個爲什麼我得到html/text內容類型而不是json?

HTTP/1.1 200 OK 
X-GUploader-UploadID AEnB2UrQyfCePM_5kYDFy1sJchgXCTkmRH8sU4S8NrWa-KzVoovFtD5iz8CIAUjegqBfBTK8ACiid0XazBRKqpZRvmUE03JNQg 
X-AppEngine-Estimated-CPM-US-Dollars $0.000000 
X-AppEngine-Resource-Usage ms=130 cpu_ms=38 
Date Tue, 05 Apr 2016 14:01:11 GMT 
Pragma no-cache 
Expires Fri, 01 Jan 1990 00:00:00 GMT 
Cache-Control no-cache, must-revalidate 
Content-Length 0 
Server UploadServer 
Content-Type text/html; charset=UTF-8 

取而代之的是

HTTP/1.1 200 OK 
X-GUploader-UploadID AEnB2UqEWk0UEztkHlBDHW5x49RYWkkIfPoHCZ_2g0YpZgvXke7blE7VM8FCJOjoAng6x5kySCLcsoccZVNyS9PdG6UU1F9Q1A 
Content-Type application/json 
Content-Encoding gzip 
X-AppEngine-Estimated-CPM-US-Dollars $0.000041 
X-AppEngine-Resource-Usage ms=1256 cpu_ms=359 
Vary Accept-Encoding 
Date Tue, 05 Apr 2016 13:44:20 GMT 
Pragma no-cache 
Expires Fri, 01 Jan 1990 00:00:00 GMT 
Cache-Control no-cache, must-revalidate 
Content-Length 307 
Server UploadServer 

在我的Android應用程序,我加了頭應用/ JSON和我嘗試發送圖像作爲多,所以第二內容類型是圖像/ PNG

這裏代碼從Android應用:

@Override 
     protected Void doInBackground(File... params) { 


      File file = params[0]; 

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 
      httppost.setHeader("X-Requested-With","XMLHttpRequest"); 
      httppost.setHeader("Accept","application/json"); 

      FileBody filebody = new FileBody(file, ContentType.create("image/jpeg"), file.getName()); 

      MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create(); 
      multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
      multipartEntity.addPart("file", filebody); 
      httppost.setEntity(multipartEntity.build()); 
      System.out.println("executing request " + httppost.getRequestLine()); 
      try { 
       HttpResponse response = httpclient.execute(httppost); 
       Log.i("response", response.getStatusLine().toString()); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      httpclient.getConnectionManager().shutdown(); 

      return null; 
     } 

爲什麼我得到html/text內容類型而不是json?服務器響應是200,但我沒有得到任何JSON。請幫幫我。 THX

響應:

HTTP/1.1 200 OK 
X-GUploader-UploadID: AEnB2UqF16tZOCnavA58S1qxTXXopXz5ESh3YIU3ksEv9UsQ6Ro4Oyw03i1CVF7M7GpsLi8_p9ua-agn9upJND_mrXCTGMO-nA 
X-AppEngine-Estimated-CPM-US-Dollars: $0.000000 
X-AppEngine-Resource-Usage: ms=1164 cpu_ms=28 
Date: Tue, 05 Apr 2016 14:29:54 GMT 
Pragma: no-cache 
Expires: Fri, 01 Jan 1990 00:00:00 GMT 
Cache-Control: no-cache, must-revalidate 
Content-Length: 0 
Server: UploadServer 
Content-Type: text/html; charset=UTF-8 
+0

我想你應該設置Content-Type的標題,而不是接受頭 –

+0

我試過,但有eror,像此內容類型對此URL不正確。我認爲這是因爲我有多部分(「內容類型」,「多部分/表單數據」) –

+0

你從服務器回來了哪些數據?您只顯示了標題,但是如果您返回了html,那麼您可能實際上正在獲取一些html錯誤頁面,而不是您預期的JSON響應 –

回答

0

所以,問題是,我得到空的身體,是200 OK響應inspite。 了幾個小時,我注意到,我需要特別name在身體

Content-Disposition: form-data; name="news_file"; filename="news.txt"\r\n 

在此之前,我使用的隨機名字,它是測試,ABC等,但。這個名稱是表單的名稱,所以這應該是表單的名稱,而不是你想要的名稱。希望這有助於某人。

在android中,這應該看起來像這樣。我使用改造。

@Multipart 
    @POST("/_ah/upload/{key}") 
    void imageUpload(@EncodedPath("key") String key, @Part("news_file") TypedFile photoFile,Callback<List<ImageUploadResponse>> callback); 

看一看,那@Part相同的名稱(例如news_file

相關問題