我正在尝试通过以下代码将图像上传到我的服务器。
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框架
这是代码;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
$app = new \Slim\App;
$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框架提供的示例multipart / form-data代码。
答案 0 :(得分:0)
您的PHP代码似乎没有响应为JSON 但是你的android代码期待JSON。它可能会导致json解析异常。
因此,如果您想解决它的问题,请将代码asJsongObject()
更改为asString()
。
Ion.with(getContext())
.....
.asJsonObject() //<-- before
.setCallback(new FutureCallback<JsonObject>() {
=>
Ion.with(getContext())
...
.asString() //<-- after
.setCallback(...)