java - 我如何使用 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
解决方案
根据 logcat 日志,php 部分不正确。
$item = $_POST['item']; // <--- You should put a comma at the end.
推荐阅读
- python - 当您的训练/测试/验证数据未分类到文件夹中时如何使用 ImageDataGenerator
- ios - 在 Swift 中运行无头 WKWebView
- c# - WPF DataGrid - SelectedItem 在文本外的 FullRow 选择上没有改变
- r - dataExplorer::create_report 编译失败
- swift - 故事板中的 UIStackView 强制固有内容大小
- python - 将子列表放置到其他子列表
- javascript - 无法保存到数据库,字符串未被识别为有效的 DateTime MVC
- c# - C#如何创建一个单元测试来验证一个值是否在代码列表中?
- python - 查找和计算重复项
- sql - 如何在sql(oracle)中操作对象的属性?