无法使用Java中的okHTTP多部分表单数据为我的Android应用程序

时间:2017-12-15 19:43:53

标签: java android curl multipartform-data okhttp3

我已经尝试了两天了解如何复制卷曲请求以将图像添加到我的Craftar图像识别集合中。我能够创建一个Item,但是设置multipart-data代码的方法让我失望了。 Craftar仅提供如何发布图像文件以将其附加到集合Item的cURL语句。

我已经尝试过使用RequestB.in和Postman来查看我在消息传输中可以看到的内容,但是没有okHTTP方法的组合似乎可以解决问题。我很难过。我已尝试将“image / png”和“application / octet-stream”作为我的MediaTypes。当我使用Postman向其发送测试请求时,RequestB.in显示“application / octet-stream”。以下是它显示的消息信息。

这是我试图用Java复制的cURL语句。

curl -X POST'https://my.craftar.net/api/v0/image/api_key=123456789abcdefghijk123456789abcdefghijk'-F“item = / api / v0 / item / d05924dde1784787a3d6b815e2b1cd69 /” - F“file=@back_cover.jpg”

这是我的代码:

        public class OkHttpHandler extends AsyncTask<String, Void, String> {

        OkHttpClient client = new OkHttpClient();

        @Override
        protected String doInBackground(String... params) {

            File storageDir = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            File file = new File("/storage/emulated/0/Pictures/BlissBotSenderImages/kaytiandkristoffer.jpg");
            RequestBody requestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("item", "/api/v0/item/d05924dde1784787a3d6b815e2b1cd69/")
                    .addFormDataPart("file","kaytiandkristoffer.jpg",
                    (RequestBody.create (MEDIA_TYPE_PNG, file)))
                    .build();

            Request request = new Request.Builder()
                    .url(postImageUrl)
                    .post(requestBody)
                    .build();

            try {
                Response response = client.newCall(request).execute();
                return response.body().string();
            } catch (Exception e) {
                e.printStackTrace();
            }

以下是 RequestB.in 成功邮寄邮递员的消息详情:

FORM / POST参数 item:/ api / v0 / item / d05924dde1784787a3d6b815e2b1cd69 /

头 Cf-Ray:3cdba305fb509b62-DFW

Via:1.1 vegur

Cf-Visitor:{“scheme”:“https”}

内容类型:multipart / form-data;边界= a11df5d0-f007-4d3a-9849-18fe334c532c

Accept-Encoding:gzip

内容长度:3509853

总路线时间:0

Cf-Connecting-Ip:2001:5b0:42c4:2ae8:2954:59ea:a3e8:4787

X-Request-Id:766063f7-c368-4467-b5ed-42776ac6f528

主持人:requestb.in

连接:关闭

Cf-Ipcountry:美国

User-Agent:okhttp / 3.9.1

连接时间:1

RAW BODY --a11df5d0-f007-4d3a-9849-18fe334c532c 内容处理:表格数据; NAME = “项” 内容长度:46

/ API / V0 /项目/ d05924dde1784787a3d6b815e2b1cd69 / --a11df5d0-f007-4d3a-9849-18fe334c532c 内容处理:表格数据; NAME = “文件”;文件名= “kaytiandkristoffer.jpg” Content-Type:application / octet-stream 内容长度:3509467

ÿØÿáExifII*
|°¸(1 À2ÎiânSAMSUNGSPH-L720THHL720TVPUBNG52015:04:05 21:22:25 \ d“'20220l

¤ ¬'
¼|bîÄ0100P¢¤¤¤¤¤¤!ÌåÜ2015:04:05 21:22:252015:04:05 21:22:25ìäd2 äd¤dASCIINVMU^^; YgZHXgmGum {wsxÞ¾ï2ì2åÞ¾ïso|Þ¾ïÞ¾ïs|Þ¾ïUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU〜Þ¾ïxt¡vuÞ¾ïCALCur| 0xDD70utvp {vVigcjijHrimoaJi [PNL sEX \ ?; I99:C9SHxVBS @ 4? “BFPMHERC = 7-> 98'VNC - / = E7AD35IECLI:P [aBDVabVTiZZ:@HHG [EH [LZ8RLLn&lt; 8a = FD”GONLNZTNGBQQLPGR _Þ­¾ï®®®®Fèþ»)q5èêÈìµ×2þJ +'#ÿê%:pF}-©}½-©.^2@E×û »'%('%$.!!$$#&*@Þ­¾ï "# E-&3>-JR>GSnxfZcZ[U]R]]W^SOXM¨a~ziqkfeheUY[nzzvra~eh[Rh\belL[NGI>RNbddqmIAIEIGNRNAAD59<-4':ZD/#.BA8$$$@8JG\qgZ~{qmcy[\~rhttcio xtne ^ y {{r~ vmxr | rsweu {GW {IQS〜ozgvy | xxffwnyyZk | WW yvt{|s}wz{zw~|vvxsjquxyykdv}jtp{}{ryY~vv}tqx{pgjsolzq~ftxrkug{|Sjqn|w]kwlq}mssij~r|||zn|}w|u{j~tmHztMvoBZlV亿吨[AUP] GT ;? ,306A7 $!BB13-

(60,!%6H @ ELEB @ 6%KVJ @ t; 04 OFC4- “# $ * 71。; ^ M @ IVTMF @ L&gt; dkL,:KE7&lt;%$?MxIu®crZs/ Y7Z / DDS_WVRgT ^ VGPb^gTORN4%Lo |p«¼ÇÓÎÄÅÃô¬°¦­qg?NOa\rl\T N; XL“e5F = H = ASlOjTwd \ dclSVXsnXXWYcG7B-XTK:cabfgfiitnW / $ 5 )E28512 * 1 $ 1M + 4G @ 8 / BC3,4'&lt;,12 ='/ + - 3%2,A()')&amp;。+'6.&amp; C?),1 @ / 52 $ - %8; 93( - +),,#。= +%** + '6 $ 51(“@'K /,#!%+!(“#&amp; $,+($)!%88 !.- 0&amp;&amp; $'!#+'&amp; $(5 / ,); --- 0 5,2!+&amp; “0 /(&amp;”)$ + &lt;&amp;(#$“ + /&amp;”#9 *#$&amp; $“%”$$ !!#&amp; / 0 *($$ ('$&安培;!“#1和

1 个答案:

答案 0 :(得分:0)

有时会发生问题。沿着这条线的某处我意外地在端点地址中插入了一个无关的字符。我解决了这个问题,上面的代码只是找到了。