【问题标题】:Error (JSONException) when i try to upload image from Android to PHP server with Volley当我尝试使用 Volley 将图像从 Android 上传到 PHP 服务器时出现错误(JSONException)
【发布时间】:2019-11-03 16:48:02
【问题描述】:

当我将图像从我的 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);

你认为我为什么会收到这个错误?

【问题讨论】:

  • 你得到什么回应?
  • 没什么,我只将该错误视为 toast 通知。我还在控制台中遇到了另一个错误:未能打开流:Permission denied in &lt;b&gt; /opt/lampp/htdocs/api_demo/upload.php
  • 已解决。 sudo chmod -R 777 /Users/matteo/.bitnami/stackman/machines/xampp/volumes/root/htdocs
  • @mrmarra98x 如果这是对您原始问题的质量解决方案,您应该将其作为答案。

标签: java php android json


【解决方案1】:

解决了。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    相关资源
    最近更新 更多