我一直在尝试使用Retrofit客户端将数据传递到数据库。它说它成功传递了数据,但是当我检查MySQL数据库时,它没有显示我刚刚发送的数据。这是我的代码:
ApiProperties类:
public class APIProperties {
private static final String ROOT_URL = "http://10.30.40.32/KPAPI%20v.2/v1/";
public static UserService getUserService(){
return RetrofitClient.getClient(ROOT_URL).create(UserService.class);
}
}
UserInterface接口:
import com.telkomuniversity.iflab.spriflab.Model.BookingInfo;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Path;
public interface UserService {
@GET("api.php?apicall=getBookings")
Call<List<BookingInfo>> getBookings();
@POST("api.php?apicall=createBooking")
Call<BookingInfo> addBooking(@Body BookingInfo booking);
@PUT("api.php?apicall=updateStatus")
Call<BookingInfo> updateStatus(@Path("status") String status, @Body BookingInfo booking);
@DELETE("api.php?apicall=deleteBooking&={id}")
Call<BookingInfo> deleteBooking(@Path("id") int id);
}
RetrofitClient类:
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String url){
if(retrofit == null){
retrofit = new Retrofit.Builder().baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
在我的主类中添加数据方法:
private void addBooking(BookingInfo booking){
Call<BookingInfo> call = userService.addBooking(booking);
call.enqueue(new Callback<BookingInfo>() {
@Override
public void onResponse(Call<BookingInfo> call, Response<BookingInfo> response) {
if(response.isSuccessful()){
Toast.makeText(FormRuanganActivity.this, "Success!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<BookingInfo> call, Throwable t) {
Log.e("ERROR: ", t.getMessage());
Toast.makeText(FormRuanganActivity.this, "ERROR: " + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
我不确定我在哪里做错了,因为它说成功传递了数据(我相信是成功的response.isSuccessful,并且表示成功),仍然无法正常工作
提前谢谢
编辑:这是我的api的php代码
dboperation文件:
<?php
class dboperation{
private $con;
function __construct(){
require_once dirname(__FILE__) . '/dbconnect.php';
$db = new dbconnect();
$this->con = $db->connect();
}
function createBooking($nim, $start, $end, $nama, $alamat, $nohp, $image,
$dosen, $ruang, $organisasi, $alasan, $mulai, $selesai){
// converting string to date
$timestart = strtotime($start);
$newformatstart = date('Y-m-d', $timestart);
$timeend = strtotime($end);
$newformatend = date('Y-m-d', $timeend);
$stmt = $this->con->prepare("INSERT INTO events (title, start, end, nama, alamat, nohp, image, dosen, ruang, organisasi, alasan, mulai, selesai) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssssssssss", $nim, $newformatstart, $newformatend, $nama, $alamat, $nohp,
$image,$dosen, $ruang, $organisasi, $alasan, $mulai, $selesai);
if($stmt->execute())
return true;
return false;
}
function getBookings(){
$stmt = $this->con->prepare("SELECT title, start, end, nama, alamat, nohp, image, dosen, ruang, organisasi, alasan, status, mulai, selesai FROM events");
$stmt->execute();
$stmt->bind_result($nim, $start, $end, $nama, $alamat, $nohp, $image, $dosen, $ruang, $organisasi,
$alasan, $status, $mulai, $selesai);
$bookings = array();
while($stmt->fetch()){
$booking = array();
$booking['title'] = $nim;
$booking['start'] = $start;
$booking['end'] = $end;
$booking['nama'] = $nama;
$booking['alamat'] = $alamat;
$booking['nohp'] = $nohp;
$booking['image'] = $image;
$booking['dosen'] = $dosen;
$booking['ruang'] = $ruang;
$booking['organisasi'] = $organisasi;
$booking['alasan'] = $alasan;
$booking['status'] = $status;
$booking['mulai'] = $mulai;
$booking['selesai'] = $selesai;
array_push($bookings, $booking);
}
return $bookings;
}
function updateStatus($id, $status){
$stmt = $this->con->prepare("UPDATE events SET status = ? WHERE id = ?");
$stmt->bind_param("si", $status, $id);
if($stmt->execute())
return true;
return false;
}
function deleteBooking($id){
$stmt = $this->con->prepare("DELETE FROM events WHERE id = ?");
$stmt->bind_param("i", $id);
if($stmt->execute())
return true;
return false;
}
}
?>
这是api.php:
<?php
require_once '../includes/dboperation.php';
function isParametersAvailable($params){
$available = true;
$missingparams = "";
foreach ($params as $param){
if(!isset($_POST[$param]) || strlen($_POST[$param])<=0){
$available = false;
$missingparams = $missingparams . "," . $param;
}
}
if(!$available){
$response = array();
$response['error'] = true;
$response['message'] = 'Parameters ' . substr($missingparams, 1, strlen($missingparams)) . ' missing';
echo json_encode($response);
die();
}
}
$response = array();
if(isset($_GET['apicall'])){
switch($_GET['apicall']){
case 'createBooking':
// status dibiarin gaada
isParametersAvailable(array('title', 'start', 'end', 'nama', 'alamat', 'nohp', 'image', 'dosen',
'ruang', 'organisasi', 'alasan', 'mulai', 'selesai'));
$db = new dboperation();
$result = $db->createBooking(
$_POST['title'],
$_POST['start'],
$_POST['end'],
$_POST['nama'],
$_POST['alamat'],
$_POST['nohp'],
$_POST['image'],
$_POST['dosen'],
$_POST['ruang'],
$_POST['organisasi'],
$_POST['alasan'],
$_POST['mulai'],
$_POST['selesai']);
if($result){
$response['error'] = false;
$response['message'] = "Booking added!";
$response['bookings'] = $db->getBookings();
}
else{
$response['error'] = true;
$response['message'] = "Error occured!";
}
break;
case 'getBookings':
$db = new dboperation();
$response['error'] = false;
$response['message'] = "Requesting to get bookings completed!";
$response['bookings'] = $db->getBookings();
break;
case 'updateStatus':
isParametersAvailable(array('id', 'status'));
$db = new dboperation();
$stat = "";
if($_POST['status'] == "y"){
$stat = "ACCEPTED";
}
else if($_POST['status'] == "n"){
$stat = "REJECTED";
}
$result = $db->updateStatus(
$_POST['id'],
$stat
);
if($result){
$response['error'] = false;
$response['message'] = "Status updated!";
$response['bookings'] = $db->getBookings();
}
else{
$response['error'] = true;
$response['message'] = "Error occured!";
}
break;
case 'deleteBooking':
if(isset($_GET['id'])){
$db = new dboperation();
if($db->deleteBooking($_GET['id'])){
$response['error'] = false;
$response['message'] = "Booking deleted!";
$response['bookings'] = $db->getBookings();
}
else{
$response['error'] = true;
$response['message'] = "Error occured!";
}
}
else{
$response['error'] = true;
$response['message'] = "No NIM provided!";
}
break;
}
}
else{
$response['error'] = true;
$response['message'] = "Invalid API call";
}
echo json_encode($response);
?>