Ion Android发送multipart / form-data

青春须早为,岂能长少年。这篇文章主要讲述Ion Android发送multipart / form-data相关的知识,希望能为你提供帮助。
我正在尝试通过以下代码将图像上传到我的服务器。

button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Ion.with(getContext()) .load("postAddress/account/upload/image") .setMultipartParameter("userPhoto", "xx") .setMultipartFile("upload", "image/jpeg", new File("/storage/emulated/0/Download/images.jpeg")) .asJsonObject() .setCallback(new FutureCallback< JsonObject> () { @Override public void onCompleted(Exception e, JsonObject result) { e.printStackTrace(); } }); } });

但是,我得到这个错误。
10-26 17:58:51.864 19292-19292/tr.example W/System.err: com.google.gson.JsonParseException: unable to parse json 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.ion.gson.GsonParser$1.transform(GsonParser.java:55) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.ion.gson.GsonParser$1.transform(GsonParser.java:41) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.future.TransformFuture.onCompleted(TransformFuture.java:14) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:107) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:141) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:128) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.parser.ByteBufferListParser$3.onCompleted(ByteBufferListParser.java:43) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.DataEmitterBase.report(DataEmitterBase.java:16) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.http.AsyncHttpResponseImpl.report(AsyncHttpResponseImpl.java:92) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.http.AsyncHttpClient$4.report(AsyncHttpClient.java:424) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.FilteredDataEmitter$1.onCompleted(FilteredDataEmitter.java:24) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.DataEmitterBase.report(DataEmitterBase.java:16) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.http.filter.ContentLengthFilter.report(ContentLengthFilter.java:16) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.http.filter.ContentLengthFilter.onDataAvailable(ContentLengthFilter.java:39) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.Util.emitAllData(Util.java:23) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.AsyncNetworkSocket.onReadable(AsyncNetworkSocket.java:152) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:821) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:658) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.AsyncServer.access$800(AsyncServer.java:44) 10-26 17:58:51.864 19292-19292/tr.example W/System.err:at com.koushikdutta.async.AsyncServer$14.run(AsyncServer.java:600)

看起来它不会向我的服务器发送任何数据。
我在服务器端使用php slim框架
【Ion Android发送multipart / form-data】这是代码;
use PsrHttpMessageServerRequestInterface as Request; use PsrHttpMessageResponseInterface as Response; require '../vendor/autoload.php'; $app = new SlimApp; $container = $app-> getContainer(); $container['upload_directory'] = __DIR__ . '/uploads'; $app-> post('/account/upload/image', function (Request $request, Response $response) { $uploadedFiles = $request-> getUploadedFiles(); $directory = $this-> get('upload_directory'); $uploadedFile = $uploadedFiles['userPhoto']; if ($uploadedFile-> getError() === UPLOAD_ERR_OK) { $filename = moveUploadedFile($directory, $uploadedFile); $response-> write('uploaded ' . $filename . '< br/> '); }}); $app-> run(); ?>

服务器端实现是由slim框架提供的示例多部分/表单数据代码。
答案您的PHP代码似乎没有响应为JSON。 但是你的android代码期待JSON。它可能会导致json解析异常。
因此,如果你想解决它的android方面,将代码asJsongObject()更改为asString()
Ion.with(getContext()) ..... .asJsonObject()//< -- before .setCallback(new FutureCallback< JsonObject> () {=> Ion.with(getContext()) ... .asString()//< -- after .setCallback(...)


    推荐阅读