我已经编写了一个应用程序来从mysql数据库查询,我已经使用PHP语言与服务器进行了交互,还使用了Android Studio和Volley,并且程序没有错误,但是在运行应用程序时未得到结果< / p>
Java和PHP代码
主班
package com.example.medicatemobile;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button qbtn = (Button)findViewById(R.id.qbtn);
final TextView nametextview = (TextView) findViewById(R.id.carduser);
final TextView idtextview = (TextView) findViewById(R.id.cardid);
qbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nametextview.getText().toString();
String id = idtextview.getText().toString().trim();
Response.Listener<String> responeListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if(success){
Toast.makeText(MainActivity.this,"تم الاستعلام", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,data_avtivity.class));
}else{
Toast.makeText(MainActivity.this,"خطأ في الاستعلام يرجي التحقق من البيانات او المحاولة في وقت لاحق", Toast.LENGTH_LONG).show();
}
}catch (JSONException e){
e.printStackTrace();
}
}
};
send_data_login send_data = new send_data_login(name , id , responeListener);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(send_data);
}
});
}
}
sen数据类
package com.example.medicatemobile;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.ListIterator;
import java.util.Map;
public class send_data_login extends StringRequest {
private static final String SEND_DATA_URL = "http://192.168.1.101/be/chack.php";
private Map<String,String> MapData;
public send_data_login(String cardname , String cardid , Response.Listener<String> listener ){
super(Method.POST,SEND_DATA_URL,listener,null);
MapData = new HashMap<>();
MapData.put("cname",cardname);
MapData.put("cid",cardid);
}
@Override
public Map<String,String> getParams(){
return MapData;
}
}
Php代码连接到mysql数据库;提取数据并打印json代码以在android应用程序中接收
<?php
include("config.php");
$cname = $_POST["cname"];
$cid = $_POST["cid"];
$sql= ("SELECT * FROM user WHERE uname like '$cardname' AND idnumber LIKE '$cardid'");
$result = mysqli_query($con, $sql);
if ($result) {
$chack["success"] = true;
}else{
$chack["success"] = false;
}
echo json_encode($chack);
?>