嘿,我在volley sql中使用数组获取Json数据,但发现错误,数据未在recycler view中加载。我正在获取具有相同 id 的所有行。但数据未加载。这是我的代码,你能告诉我错误在哪里吗?
private void LoadAllJobs() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, Urls.all_jobs,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String jobname = object.getString("jobname");
String jobdetails = object.getString("jobdetails");
String startdate = object.getString("startdate");
String enddate = object.getString("enddate");
String posts = object.getString("posts");
String govtfees = object.getString("govtfees");
String appfees = object.getString("appfees");
String documents = object.getString("documents");
String declartion = object.getString("declartion");
AllJobsModel alljobsName = new AllJobsModel(jobname, jobdetails, posts, startdate, enddate, govtfees, appfees, documents, declartion);
alljobsName.setJobName(jobname);
alljobsName.setJobDetails(jobdetails);
allJobs.add(alljobsName);
}
} catch (JSONException e) {
e.printStackTrace();
AndroidUtil.showToast(getApplicationContext(),e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}){
@Nullable
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("id","1");
return params;
}
};
RequestQueue queue= Volley.newRequestQueue(MainActivity.this);
queue.add(stringRequest);
}
<?php
require_once "conn.php";
require_once "validate.php";
$id = $_POST['id'];
$sql = "SELECT * from all_jobs WHERE id ='$id'";
$result=mysqli_query($conn, $sql);
$all_jobs= array();
while($row=mysqli_fetch_array($result)){
$index['jobname']=$row['jobname'];
$index['posts']=$row['posts'];
$index['startdate']=$row['startdate'];
$index['enddate']=$row['enddate'];
$index['govtfees']=$row['govtfees'];
$index['appfees']=$row['appfees'];
$index['documents']=$row['documents'];
$index['declartion']=$row['declartion'];
$index['jobdetails']=$row['jobdetails'];
array_push($all_jobs, $index);
}
echo json_encode($all_jobs)
?>
请帮我修复它,数据未加载,并且在应用程序中显示 json 错误无法将字符串转换为 json 数组 // 类似这样。帮助我,我非常接近完成我的应用程序
该错误表明您从 PHP 脚本收到的响应存在问题。当响应不是有效的 JSON 数组时,通常会发生此错误。在 PHP 代码中,您正在回显 JSON 编码的数据,但确保响应是有效的 JSON 非常重要。
这是 PHP 修正:
<?php
require_once "conn.php";
require_once "validate.php";
$id = $_POST['id'];
$sql = "SELECT * FROM all_jobs WHERE id = '$id'";
$result = mysqli_query($conn, $sql);
$all_jobs = array();
while ($row = mysqli_fetch_array($result)) {
$index['jobname'] = $row['jobname'];
$index['posts'] = $row['posts'];
$index['startdate'] = $row['startdate'];
$index['enddate'] = $row['enddate'];
$index['govtfees'] = $row['govtfees'];
$index['appfees'] = $row['appfees'];
$index['documents'] = $row['documents'];
$index['declartion'] = $row['declartion'];
$index['jobdetails'] = $row['jobdetails'];
array_push($all_jobs, $index);
}
header('Content-Type: application/json');
echo json_encode($all_jobs);
?>