我正在尝试使用改造将图像上传到本地服务器。下面是我的 php 代码。
<?php
require 'init.php';
if ($con) {
$title = $_POST['title'];
$image = $_POST['image'];
$upload_path = "uploads/$title.jpg";
$sql = "insert into imageinfo(title,path) values('$title', '$upload_path');";
if (mysqli_query($con, $sql)) {
file_put_contents($upload_path, base64_decode($image));
echo json_encode(array('response' => "Image uploaded successfully."));
} else {
echo json_encode(array('response' => "Error! Image is not uploaded."));
}
mysqli_close($con);
}
?>
但我收到如下错误:com.google.gson.stream.MalformedJsonException 使用 JsonReader.setLenient(true) 接受第 1 行第 1 列路径 $ 处格式错误的 JSON。
然后我在初始化retrofit的类中添加了以下代码。
Gson gson = new GsonBuilder().setLenient().create();
retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).build();
现在我收到以下错误:com.google.gson.JsonSyntaxException: java.lang.illegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
哪里出了问题? php代码有什么问题吗?
最佳答案
Gson gson = new GsonBuilder().setLenient(true).create();
试试这个。
关于java - com.google.gson.stream.MalformedJsonException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44928881/