我对android很新。
我要做的是将一些数据插入到本地主机托管的MySQL服务器上。
我正在尝试使用Retrofit插入3个字段,即日期,时间和位置。
我创建了一个Retrovit接口,下面是我的代码
public interface RegisterAPI {
@FormUrlEncoded
@POST("/lantas_minut/insert.php")
public void insertLokasi(
@Field("tanggal") String tanggal,
@Field("waktu") String waktu,
@Field("lokasi") String lokasi,
Callback<Response> callback);
}
我还创建了插入数据的方法,如下所示
private void insertLokasi(){
//Here we will handle the http request to insert user to mysql db
//Creating a RestAdapter
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(ROOT_URL) //Setting the Root URL
.build(); //Finally building the adapter
//Creating object for our interface
RegisterAPI api = adapter.create(RegisterAPI.class);
//Defining the method insertuser of our interface
api.insertLokasi(
//Passing the values by getting it from editTexts
textViewTanggal.getText().toString(),
textViewWaktu.getText().toString(),
editTextLokasi.getText().toString(),
//Creating an anonymous callback
new Callback<Response>() {
@Override
public void success(Response result, Response response) {
//On success we will read the server's output using bufferedreader
//Creating a bufferedreader object
BufferedReader reader = null;
//An string to store output from the server
String output = "";
try {
//Initializing buffered reader
reader = new BufferedReader(new InputStreamReader(result.getBody().in()));
//Reading the output in the string
output = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//Displaying the output as a toast
Toast.makeText(MainActivity.this, output, Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
//If any error occured displaying the error as toast
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}
);
}
我已成功将其安装在我的设备上并顺利运行,但是,当我尝试单击按钮以插入其显示的数据&#34;未找到协议时:192.168.1.8/lantas_minut/insert.php"
下面的代码是我的insert.php脚本。
<?php
require_once('connection.php');
if($_SERVER['REQUEST_METHOD']=='POST') {
//Getting post data
$tanggal = $_POST['tanggal'];
$waktu = $_POST['waktu'];
$lokasi = $_POST['lokasi'];
//Checking if the received values are blank
if($tanggal=='' || $waktu=='' || $lokasi=='') {
//Show message to fill all values
echo 'please fill all values';
}else{
//if the values are not blank than connect to database
//sql query to insert into database
$sql = "INSERT INTO tbl_macet (tanggal,waktu,lokasi) VALUES ('$tanggal','waktu','lokasi')";
if(mysqli_query($con,$sql)) {
echo 'Data berhasil disimpan';
}else{
echo 'Terjadi kesalahan';
}
}
//mysqli_close('$con');
}else{
echo 'Error';
}
?>
请有人帮助我..我陷入了这种情况..
非常感谢任何反馈..
谢谢..