【发布时间】:2020-05-20 02:14:59
【问题描述】:
我正在尝试使用 volley 进行 POST 请求,以通过本地 API 与 MySQL 数据库进行交互。我正在关注一个小教程 (https://www.youtube.com/watch?v=M2KKIqrp8Y0) 一切似乎都很好,但是应用程序向我发送了一个超时错误,其中“E/LB:无法打开文件:没有这样的文件或目录”在 Android Studio 的运行中用红色写成.
这里有一些代码:
demovolley/post.php
<?php
include "dbConnect.php";
$ten = $_POST['TEN'];
$email = $_POST['EMAIL'];
try{
//print ("Erreur%");
$conn = dbConnect();
$query = $conn->prepare("INSERT INTO sinhvien VALUES (null, '$ten','$email')");
$query->execute();
/*if($ligne = $query->fetch(PDO::FETCH_ASSOC)){
print (json_encode($ligne));
}*/
}catch(PDOException $e){
print "Erreur: " .$e->getMessage();
die();
}
?>
此查询有效,我已经用 Insomnia 对其进行了测试,并且数据已插入我的表中
MainActivity.java
package com.mds.demovolley;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
EditText edtTen,edtMail;
Button btnGoi;
String URL_POST = "https://*my-ip*/demovolley/post.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtTen = (EditText) findViewById(R.id.editText);
edtMail = (EditText) findViewById(R.id.editText2);
btnGoi = (Button) findViewById(R.id.button);
btnGoi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
InsertSV();
}
});
}
private void InsertSV(){
StringRequest stringRequest = new StringRequest(Request.Method.POST,URL_POST, new Response.Listener<String>(){
@Override
public void onResponse(String response) {
Toast.makeText(getApplication(), response,Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error+"",Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
String Ten = edtTen.getText().toString();
String MAIL = edtMail.getText().toString();
params.put("TEN",Ten);
params.put("EMAIL",MAIL);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
我在论坛上读过一些东西,但我还没有找到解决办法 大家可以帮帮我吗? (PS:对不起,如果我的英语不太好)
【问题讨论】:
标签: java php android mysql api