无法使用Java中的okHTTP多部分表单数据为我的Android应用程序重新创建cURL POST请求

问题描述 投票:0回答:1

我已经尝试了两天了解如何复制卷曲请求以将图像添加到我的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 Post Post邮件详细信息:


FORM / POST PARAMETERS项目:/ api / v0 / item / d05924dde1784787a3d6b815e2b1cd69 /

HEADERS Cf-Ray:3cdba305fb509b62-DFW

通过:1.1路

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:美国

用户代理:okhttp / 3.9.1

连接时间:1

RAW BODY --a11df5d0-f007-4d3a-9849-18fe334c532c Content-Disposition:form-data; name =“item”内容长度:46

/ api / v0 / item / d05924dde1784787a3d6b815e2b1cd69 / --a11df5d0-f007-4d3a-9849-18fe334c532c Content-Disposition:form-data; NAME = “文件”; filename =“kaytiandkristoffer.jpg”内容类型:application / octet-stream内容长度:3509467

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

¤¬' ¼| BIA 0100 P¢£¤¤¤¤¤ÌåÜd2015: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 [pnLsEX \ ?; I99:C9SHxVBS?@ 4 9?<>Þ¾ïTUNAIFJ“BFPMHERC = 7-> 98'VNC - / = E7AD35IECLI:P [aBDVabVTiZZ:@HHG [EH [LZ8RLLn <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[\~rhttcioxtne ^ ÿ{{R〜vmxr | rsweu {GW {IQS〜ozgvy | xxffwnyyZk | WWyvt{|s}wz{zw~|vvxsjquxyykdv}jtp{}{ryY~vv}tqx{pgjsolzq~ftxrkug{|Sjqn|w]kwlq}mssij~r|||zn|}w|u{j~tmHztMvoBZlVGt [AUP]>? ,306A7 $!BB13-

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

java android curl multipartform-data okhttp3
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.