Allereerst overschrijft u de afbeeldingsgegevens in uw doInBackground
lus.
Ten tweede zit de PHP-uploadcode niet in de lus
JAVA
Je zou maar één lus moeten hebben, als je je JSON bouwt, zet dan alles wat je nodig hebt daar
for (ImageAndText i : listItems) {
JSONObject object = new JSONObject();
String type = i.getType();
String[] Type = type.split(":");
String amount = i.getAmount();
String[] Amount = amount.split(":");
String description = i.getDescription();
String[] Description = description.split(":");
//Image
String image = i.getImage().toString()
Uri imageUri = Uri.parse(image);
object.put("amount", Amount[1]);
object.put("type", Type[1]);
object.put("description", Description[1]);
object.put("ts_id", id);
object.put("image", image);
object.put(Configs.KEY_IMAGE, getStringImage(imageUri));
jsonArray.put(object);
}
Plaats vervolgens de JSON
in je hashmap
verzenden
@Override
protected String doInBackground(String... params) {
try {
HashMap<String, String> data = new HashMap<String, String>();
data.put("listItems", jsonArray.toString());
RequestHandler rh = new RequestHandler();
String result = rh.sendPostRequest(Configs.STAFF_BENEFIT, data);
return result;
} catch (Exception e) {
return "";
}
}
PHP
De php zou veranderen, je hebt $image = $_POST['image'];
niet nodig
U krijgt de afbeeldingsgegevens van de json $listItems = json_decode( $_POST['listItems'], true );
Je zou de uploadcode in de lus plaatsen en alleen invoegen bij een succesvolle upload
foreach( $listItems as $item ){
$path=time()."$id.png";
$actualpath="http://192.168.107.115:80/Android/CRUD/PhotoUpload/$path";
$bytes=file_put_contents( $savepath, base64_decode( $item['image'] ) );
if( !$bytes ){
echo 'Error saving image';
}else{
$stmt->bind_param('sssss',
$item['type'],
$item['amount'],
$item['description'],
$actualpath,
$item['ts_id'] );
$res=$stmt->execute();
if( !$res ) echo 'Query failed with code: '.$stmt->errno;
}
}
BEWERKEN:
Volledig PHP-script
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
if( !empty( $_POST['listItems'] ) ){
$listItems = json_decode( $_POST['listItems'], true );
$mysqli = new mysqli("127.0.0.1:3307", "root", "", "androiddb");
if( $mysqli->connect_errno ) echo "Failed to connect to MySQL";
$sql="INSERT INTO `staff_benefit`
( `type`, `amount`, `description`, `image`, `ts_id` )
VALUES ( ?, ?, ?, ?, ? )";
if($stmt=$mysqli->prepare($sql )){
$url="http://192.168.107.115:80/Android/CRUD/PhotoUpload/";
foreach( $listItems as $item ){
$image_name = time().".png";
$save_path = 'PhotoUpload/'.$image_name;
$image_url = $url.$image_name;
$bytes=file_put_contents($save_path, base64_decode($item['image']));
if( !$bytes ){
echo 'Error saving image';
}else{
$stmt->bind_param('sssss',
$item['type'],
$item['amount'],
$item['description'],
$image_url,
$item['ts_id'] );
if( !$res=$stmt->execute()){
echo 'Query failed with code: '.$stmt->errno;
}
}
}
}
$mysqli->close();
}
}
?>