gpt4 book ai didi

java - 当我尝试使用 Volley 将图像从 Android 上传到 PHP 服务器时出现错误(JSONException)

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:19 30 4
gpt4 key购买 nike

当我将图像从 Android 设备上传到 PHP Web 服务器时,出现无法修复的错误。更准确地说,我得到错误:

org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

老实说我不明白为什么。我附上了下面的 PHP 脚本,希望您能够找出错误。

<?php
// Path to move uploaded files
$target_path = dirname(__FILE__).'.';

if (isset($_FILES['image']['name'])) {
$target_path = $target_path . basename($_FILES['image']['name']);

try {
// Throws exception incase file is not being moved
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
// make error flag true
echo json_encode(array('status'=>'fail', 'message'=>'could not move file'));
}

// File successfully uploaded
echo json_encode(array('status'=>'success', 'message'=>'File Uploaded'));
} catch (Exception $e) {
// Exception occurred. Make error flag true
echo json_encode(array('status'=>'fail', 'message'=>$e->getMessage()));
}
} else {
// File parameter is missing
echo json_encode(array('status'=>'fail', 'message'=>'Not received any file'));
}
?>

而图片上传函数是这样定义的

private void imageUpload(final String imagePath) {

SimpleMultiPartRequest smr = new SimpleMultiPartRequest(Request.Method.POST, BASE_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Response", response);
try {
JSONObject jObj = new JSONObject(response);
String message = jObj.getString("message");

Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();

} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
});

smr.addFile("image", imagePath);
MyApplication.getInstance().addToRequestQueue(smr);

您认为我为什么会收到此错误?

最佳答案

已解决。

sudo chmod -R 777 /Users/matteo/.bitnami/stackman/machines/xampp/volumes/root/htdocs

关于java - 当我尝试使用 Volley 将图像从 Android 上传到 PHP 服务器时出现错误(JSONException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56693191/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com