【发布时间】:2019-03-25 23:33:08
【问题描述】:
我有一个使用 php 文件连接到 mysql 在线数据库的 android studio 项目。 问题是我没有从具有给定 idquestion 和类别的 php 文件中收到参数(idquestion、question、id)。 该类和 php 文件与其他的类似副本,可以正常工作但不能正常工作。
我尝试了调试器,发现我传递了正确的信息(idquestion 1 和类别“计算机”)。 另外,我尝试了另一个具有不同语法的 php 文件来打印数据并且它有效,但该 php 文件对我的课程没有帮助。
这是我的 php 文件
<?php
$con = mysqli_connect("localhost", "id8963226_user", "parola123", "id8963226_user");
$idquestion = @($_POST['idquestion']);
$category = @($_POST['category']);
$statement = mysqli_prepare($con, "select * from question where idquestion=? and category=?;");
mysqli_stmt_bind_param($statement, "is", $idquestion, $question);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $success, $idquestion, $question, $id);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["idquestion"] = $idquestion;
$response["question"] = $question;
$response["id"] = $id;
}
echo json_encode($response);
?>
这些是我的 java 类
package com.example.allrateform;
import android.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
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 Question extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
//CREATING ALL INSTANCES FROM TEXTS
final TextView CategoryTextView = findViewById(R.id.CategoryTextView);
final TextView QuestionTextView = findViewById(R.id.QuestionTextView);
final TextView IdTextView = findViewById(R.id.IdTextView);
CategoryTextView.setText(Categories.Category);
final String category = CategoryTextView.getText().toString();
final int idquestion = 1;
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
String question = jsonResponse.getString("question");
int id = jsonResponse.getInt("id");
QuestionTextView.setText(question);
IdTextView.setText(id);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(Question.this);
builder.setMessage("Failed")
.setNegativeButton("Ok", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
//CREATING RESPONSE
ListRequest listRequest = new ListRequest(idquestion, category, responseListener);
RequestQueue queue = Volley.newRequestQueue(Question.this);
queue.add(listRequest);
}
}
package com.example.allrateform;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class ListRequest extends StringRequest {
private static final String LIST_REQUEST_URL = "myUrl.com/myPhp.php";
private Map<String, String> params;
//METHOD TO PASS THE INFORMATION
public ListRequest(int idquestion, String category, Response.Listener<String> listener) {
super(Method.POST, LIST_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("idquestion", idquestion + "");
params.put("category", category);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
所以项目结果还是“失败”,这意味着 php 返回 false。
如果您有其他方法可以解决此问题,只要我能够将“计算机”类别中的所有问题存储在 java 字符串数组中即可。
请随时询问有关我的数据库或其他课程或任何其他问题的任何问题。 提前谢谢!
【问题讨论】:
-
在执行
printf("Error: %s.\n", mysqli_stmt_error($statement)); exit;之后放置以查看您是否有与语句相关的错误,如果存在则发布。或者将其存储到$response并传回调用 php.ini 的应用程序。例如这种方式$response = mysqli_stmt_error($statement);一旦你知道究竟是什么问题,你就在解决它的路上。您还可以获得一系列最新错误Documentation。可能你的 php 没有返回 false 但$response["success"] = false;并且没有执行 while 循环 -
不要使用
@错误抑制器 -
有一个documented way 来检查连接是否建立。请使用它
-
你是一个JAVA程序员,为什么不使用OO Mysql语法,它更容易阅读和理解
-
错误检查 但如果您不介意,请将
ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);添加到脚本顶部。这将强制任何 mysqli_ 错误生成您可以在浏览器上看到的异常以及正常的 PHP 错误。
标签: java php android json database