我正在尝试将图片上传到我的应用中的Google Cloud Storage。 我有一个有效的网络解决方案,所以我可以看到所有的请求信息。 这里是工作网络解决方案的原始请求
POST /_ah/upload/AMmfu6YbDgVL5B8ZYIBs9VQcZElIKyvHBOG0g-Y3PDsBiVgQLFZNKdzmV-G9gYF3e9SnFUNWCOSP6KDgO5f9bAxkDyNiMeY5EzXW9uWK9ULqXoQostoWySSQkKSzbizJbmjOH1x6mHOkYyWe3kKCn_84qpc1UhUNkw/ALBNUaYAAAAAVwOltAcWY8y-iHPujyHYUBbhmQlwJyi-/ HTTP/1.1
Host: taxi2deal.appspot.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: */*
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
Referer: http://taxi2deal.appspot.com/
Content-Length: 2718
Content-Type: multipart/form-data; boundary=---------------------------5607231118094935911642858770
Connection: keep-alive
-----------------------------5607231118094935911642858770
Content-Disposition: form-data; name="myFile[]"; filename="ic_uploadphoto.png"
Content-Type: image/png
�PNG
IHDR*$�ɨ+tEXtSoftwareAdobe ImageReadyq�e<(iTXtXML:com.adobe.xmp<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.6-c067 79.157747, 2015/03/30-23:40:42 "> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" xmlns:stRef="http://ns.adobe.com/xap/1.0/sType/ResourceRef#" xmp:CreatorTool="Adobe Photoshop CC 2015 (Macintosh)" xmpMM:InstanceID="xmp.iid:3B13B96FF1F811E599F4BCD9FCA860E7" xmpMM:DocumentID="xmp.did:3B13B970F1F811E599F4BCD9FCA860E7"> <xmpMM:DerivedFrom stRef:instanceID="xmp.iid:3B13B96DF1F811E599F4BCD9FCA860E7" stRef:documentID="xmp.did:3B13B96EF1F811E599F4BCD9FCA860E7"/> </rdf:Description> </rdf:RDF> </x:xmpmeta> <?xpacket end="r"?>KB3�'IDATx��XkHW7�#h��k��V��i�D��BHZ��H�X�%�Ph�
-----------------------------5607231118094935911642858770--
顺便说一句,这是我的原始回应
POST /_ah/upload/AMmfu6b-E_pSrXGteBOj-3LAyVfKs7h24BRJALESmv0km6cecTXCOQCU5r9r8nPszlGtM3zEbyKMcElhP219WqYKYeQCfy6lpOkifBB9HsBiQEOjP10kJyBXyTAQA9P3oWcGjMltZrFgjwAc5vP6g08ssDcj-mvzWw/ALBNUaYAAAAAVwOo2tdzrOgOCiGMq-Gjy7yuyKL063r2/ HTTP/1.1
Content-Type: multipart/form-data; boundary=69ea02ab-ac17-49a7-9a8b-8be58caf9f61
Content-Length: 66726
Host: 6.taxi2deal.appspot.com
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/2.3.0
--69ea02ab-ac17-49a7-9a8b-8be58caf9f61
Content-Disposition: form-data; name="photo"; filename="call_taxi1459857026278.png"
Content-Type: image/*
Content-Length: 66474
Content-Transfer-Encoding: binary
这是我使用Retrofit的Request API
@Multipart
@POST("/_ah/upload/{key}")
void imageUpload(@EncodedPath("key") String key,@Part("photo") TypedFile photoFile,Callback<List<ImageUploadResponse>> callback);
这是请求实现
TypedFile photoTypedFile = new TypedFile("image/*", imageFileName);
TypedString name = new TypedString("testName");
service.imageUpload(key, photoTypedFile, new Callback<List<ImageUploadResponse>>() {
@Override
public void success(List<ImageUploadResponse> imageUploadResponses, Response response) {
Log.d("SignUpActivity", "Status Code = " + response.getStatus());
Log.d("SignUpActivity", "response = " + new Gson().toJson(response));
if (response.getStatus() < 400) {
// request successful (status code 200, 201)
ImageUploadResponse result = imageUploadResponses.get(0);
Log.d("SignUpActivity", "response = " + new Gson().toJson(result));
Toast.makeText(getApplicationContext(), result.imageUrl, Toast.LENGTH_SHORT).show();
Log.d("SignUpActivity", "ImagUrl = " + result.imageUrl);
}
}
@Override
public void failure(RetrofitError error) {
Log.d("SignUpActivity", error.toString());
}
});
答案 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
)