Sections.class
private void loadSection() {
final String username = SharedPrefManager.getInstance(getActivity()).getUsername();
final String userSection_URL = Constants.USER_SECTION + "?username=" + username;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, userSection_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject sections = (JSONObject) jsonArray.get(i);
sectionList.add(new ListGradeData(
sections.getInt("id"),
sections.getString("section"),
sections.getString("year_level"),
sections.getString("school_year")
));
//creating adapter object and setting it to recyclerview
LatestGradeAdapter adapter = new LatestGradeAdapter(getActivity(), sectionList);
recyclerView.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getContext(), "No Data", Toast.LENGTH_LONG).show();
}
// Stopping swipe refresh
mSwipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Stopping swipe refresh
mSwipeRefreshLayout.setRefreshing(false);
headerSection.setVisibility(View.GONE);
noConnectionLayout.setVisibility(View.VISIBLE);
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(getActivity().getApplicationContext()).add(jsonObjectRequest);
}
userSection.php
require_once "../include/Constants.php";
$ conn =新的mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
if($ _ SERVER ['REQUEST_METHOD'] =='GET'){ $ uname = trim($ _ GET ['username']);
if(isset($uname)){
$query = "SELECT * FROM students WHERE s_id = '".$uname."' ";
$tbl = mysqli_query($conn, $query);
if(mysqli_num_rows($tbl)>0) {
$row = mysqli_fetch_array($tbl, MYSQLI_ASSOC);
$uid = $row['id'];
$stmt = $conn->prepare("SELECT students.id, sections.name AS section_name, years.name AS year_level, CONCAT(academic_years.start, '-', academic_years.end) AS school_year FROM students INNER JOIN section_student ON students.id = section_student.student_id INNER JOIN sections ON section_student.section_id = sections.id INNER JOIN years ON sections.year_id = years.id INNER JOIN academic_years ON sections.academic_year_id = academic_years.id WHERE students.id = ?");
$stmt->bind_param("s", $uname);
$stmt->execute();
$stmt->bind_result($id, $section, $year_level, $school_year);
$sections = array();
//traversing through all the result
while($stmt->fetch()){
$temp = array();
$temp['id'] = $id;
$temp['section'] = $section;
$temp['year_level'] = $year_level;
$temp['school_year'] = $school_year;
array_push($sections, $temp);
}
echo json_encode(array('data' => $sections));
}else {
echo json_encode(array('data' => "No Data"));
}
}
} 问题解决了!
我有一个问题,涉及到检索数据。
我想根据当前登录的用户名检索信息。
但这是行不通的。我已经尝试显示所有数据,但是它不是基于当前登录的用户。它将显示所有数据。主要问题是如何根据当前登录的用户检索数据。我当前登录的用户基于此代码final String username = SharedPrefManager.getInstance(getActivity()).getUsername();
答案 0 :(得分:0)
我建议使用JSONObjectRequest
。它将允许您检索对象。
String url = YOUR_OWN_URL + "?username=" + username;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
int id = jsonObject.getInt("id");
String section = jsonObject.getString("section");
String year_level = jsonObject.getString("year_level");
String school_year = jsonObject.getString("school_year");
// insert your own logic
}
} catch (JSONException e) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
然后,在PHP中,我将json_encode
部分更改为这样。
echo json_encode(array('data' => $sections));
答案 1 :(得分:0)
在您的PHP代码中,您正在username
处寻找一个$uname = trim($_GET['username']);
url参数。但是您不是从Volley StringRequest发送它的。您的网址由Constants.USER_SECTION
定义。如果这是请求的端点,则应使用类似以下内容的参数将其添加到url中:
String url = Constants.USER_SECTION + '?username=' + username; //considering that Constants.USER_SECTION doesn't have any url params
StringRequest stringRequest = new StringRequest(Request.Method.GET, url ,
new Response.Listener<String>() {
...
修复上述错误后,请按照以下步骤调试代码:
final String username
值。如果不正确,请确认将其保存到SharedPreferences的位置。url
的值。如果错误,请检查Constants.USER_SECTION
的定义位置。response
。如果不正确,甚至使用浏览器的REST客户端调试服务器端代码。