首页 > 解决方案 > 我如何使用 volley 和 php 在 android studio 中上传图像和 2 个 textView 数据?

问题描述

我创建了一个用于上传图像和文本的应用程序,使用 volley 和 php。这里的upload.php


include_once "koneksi.php"; 
class emp{}

$image = $_POST['image'];
$name = $_POST['name'];

//ini kode yang saya tambah yaitu item
$item = $_POST['item']

if (empty($name)) { 
    $response = new emp();
    $response->success = 0;
    $response->message = "Please dont empty Name."; 
    die(json_encode($response));
} else {
    $random = random_word(20);

    $path = "images/".$random.".png";

    // sesuiakan ip address laptop/pc atau URL server
    $actualpath = "http://192.168.43.137/android/upload_image/$path";

    // saya tambah variabel item
    $query = mysqli_query($con, "INSERT INTO volley_upload (photo,name,item) VALUES ($actualpath','$name','$item')");

    if ($query){
        file_put_contents($path,base64_decode($image));

        $response = new emp();
        $response->success = 1;
        $response->message = "Successfully Uploaded";
        die(json_encode($response));
    } else{ 
        $response = new emp();
        $response->success = 0;
        $response->message = "Error Upload image";
        die(json_encode($response)); 
    }
}   

// fungsi random string pada gambar untuk menghindari nama file yang sama
function random_word($id = 20){
    $pool = '1234567890abcdefghijkmnpqrstuvwxyz';

    $word = '';
    for ($i = 0; $i < $id; $i++){
        $word .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
    }
    return $word; 
}

mysqli_close($con);

?>

此外,对于它的 .xml 部分,我使用 Fab 作为图像上传按钮和 2 个 TextView,即 editItem 和 editText。我还将 imageView 添加到要上传的图像中。在我在 XML 和 JAVA 中添加 textView editItem 项之前,应用程序运行良好,但是在我决定再添加一个 textView 上传(所以总共有 2 个 textview)之后,数据没有成功发送到数据库,而之前上传1 张图片和 1 个 textView 描述,但我的目标是上传 1 张图片和 2 个 textView 描述,但未能上传 1 张图片和 2 个 textView(editText,editItem)

我该如何解决?我想上传 1 张图片和 2 个 textView(editText、editItem)

MainActivity.java = https://drive.google.com/open?id=1uxKxGmpRoPOJiKFLkXxGMxYrMgxBO3se activity_main.xml = https://drive.google.com/open?id=1xPso2Z_s0CV078UUIzX1jjpfMG9k9_2

这是日志猫

标签: javaphpmysqluploadandroid-volley

解决方案


根据 logcat 日志,php 部分不正确。

$item = $_POST['item']; // <--- You should put a comma at the end. 

推荐阅读