userSection.php
<?php
require_once '../include/DbOperations.php';
$response = array();
if ($_SERVER['REQUEST_METHOD']=='GET'){
$uname = trim($_GET['username']);
if (isset($uname)){
$db = new DbOperations();
if ($db->studentLogin($uname)){
$sections = $db->studentSection($uname);
$response['error'] = false;
$response['id'] = $sections['id'];
$response['lastname'] = $sections['lastname'];
$response['section'] = $sections['section'];
$response['year_level'] = $sections['year_level'];
$response['school_year'] = $sections['school_year'];
}else{
$response['error'] = true;
$response['message'] = "No Data";
}
}else{
$response['error'] = true;
$response['message'] = "Required fields are missing";
}
}
echo json_encode($response);
sectionList.class
StringRequest stringRequest = new StringRequest(Request.Method.GET, Constants.USER_GRADE,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//it should display the data of current user id
//On this part it doesn't display anything.
//example `id`=`1`. it should display all `id` equals to `1`
//converting the string to json array object
JSONArray array = new JSONArray(response);
if (sectionList!=null) {
sectionList.clear();
}
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject sections = array.getJSONObject(i);
if (!sections.getBoolean("error")) {
//adding the product to product list
sectionList.add(new ListGradeData(
sections.getInt("id"),
sections.getString("section"),
sections.getString("level"),
sections.getString("schoolyear")
));
}
}
//creating adapter object and setting it to recyclerview
LatestGradeAdapter adapter = new LatestGradeAdapter(getActivity(), sectionList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(getActivity().getApplicationContext()).add(stringRequest);
Как отобразить все данные, которые id = 1.
Мое текущее имя пользователя id - 1. но он не отображает ничего пустого каждый раз, когда я запускаю приложение.
Я тоже пытался поставить это,
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("username", username);
return params;
}
};
но все равно у меня это не работает.






Параметры запроса ПОЛУЧАТЬ в залпе передаются как часть url, поэтому измените URL-адрес на
String url = "http://youraddress.com/userSection.php?username = " + username;
Передача параметров методом getParams() осуществляется в запросах ПОЧТА.
Я поставлю комментарий.
Ответ - не JSONArray, а нормальный JSONObject . Чтобы убедиться в этом, попробуйте указать URL-адрес с параметром в своем браузере, и вы увидите, что в результате вы получите обычный json.
Спасибо, сэр. У меня все еще есть ошибка. он не отображает данные.