当我将图片上传到Google云端存储时,我得到了这个
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应用程序中,我添加了头文件Application / json,我尝试将图像作为multipart发送,因此第二种内容类型是image / png
来自android app的代码:
@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 :(得分:0)
所以,问题是,我得到了空身体,尽管200 Ok回应。 几个小时后,我注意到,我需要在Body
中使用特殊的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
)