带有参数的 Volley jsonObjectRequest 不起作用
Volley jsonObjectRequest with parameter not working
你好,我正在尝试通过我的 volley 请求发送一个参数,然后获取一个 json 文件以用于我的 RecyclerView。我已经在没有参数的情况下对此进行了测试,我得到了预期的结果,但是使用参数我没有得到任何回报。
private void parseJSON() {
String url = "http://10.0.2.2/getThesis.php"; //URL OF THE PHP FILE USED
Map<String, String> params = new HashMap();
params.put("user", email);
JSONObject parameters = new JSONObject(params);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, parameters,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("assigned_thesis"); //THE NAME OF THE JSON FILE
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject thesis = jsonArray.getJSONObject(i);
String title = thesis.getString("thesis_title"); //THE NAME OF THE COLUMN IN THE JSON FILE
String fullName = thesis.getString("full_name");
mExampleList.add(new ExampleItem(title, fullName));
}
mExampleAdapter = new ExampleAdapter(PendingChoiceThesis.this, mExampleList); //USAGE OF THE CUSTOM ADAPTER, CHANGE CONTEXT TO BE THE SAME AS THE JAVA CLASS
mRecyclerView.setAdapter(mExampleAdapter);
mExampleAdapter.setOnItemClickListener(PendingChoiceThesis.this); //CHANGE CONTEXT TO BE THE SAME AS THE JAVA CLASS
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
我想弄清楚的是我的截击请求代码是否有错误,或者错误是否出在我的项目中。
(供参考,这是我想在 php 文件中使用参数的地方)
$email = $_POST['user'];
$dep_query = mysqli_query($con, "SELECT department_ID FROM user_accounts NATURAL JOIN users WHERE email = '$email'");
$result = mysqli_fetch_assoc($dep_query);
通过在 errorResponse
下添加这个解决了我的问题
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
并且还将我的 php 更改为此
$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON );
$var = $input->user; //user is from the parameters jsonobject
你好,我正在尝试通过我的 volley 请求发送一个参数,然后获取一个 json 文件以用于我的 RecyclerView。我已经在没有参数的情况下对此进行了测试,我得到了预期的结果,但是使用参数我没有得到任何回报。
private void parseJSON() {
String url = "http://10.0.2.2/getThesis.php"; //URL OF THE PHP FILE USED
Map<String, String> params = new HashMap();
params.put("user", email);
JSONObject parameters = new JSONObject(params);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, parameters,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("assigned_thesis"); //THE NAME OF THE JSON FILE
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject thesis = jsonArray.getJSONObject(i);
String title = thesis.getString("thesis_title"); //THE NAME OF THE COLUMN IN THE JSON FILE
String fullName = thesis.getString("full_name");
mExampleList.add(new ExampleItem(title, fullName));
}
mExampleAdapter = new ExampleAdapter(PendingChoiceThesis.this, mExampleList); //USAGE OF THE CUSTOM ADAPTER, CHANGE CONTEXT TO BE THE SAME AS THE JAVA CLASS
mRecyclerView.setAdapter(mExampleAdapter);
mExampleAdapter.setOnItemClickListener(PendingChoiceThesis.this); //CHANGE CONTEXT TO BE THE SAME AS THE JAVA CLASS
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
我想弄清楚的是我的截击请求代码是否有错误,或者错误是否出在我的项目中。
(供参考,这是我想在 php 文件中使用参数的地方)
$email = $_POST['user'];
$dep_query = mysqli_query($con, "SELECT department_ID FROM user_accounts NATURAL JOIN users WHERE email = '$email'");
$result = mysqli_fetch_assoc($dep_query);
通过在 errorResponse
下添加这个解决了我的问题 @Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
并且还将我的 php 更改为此
$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON );
$var = $input->user; //user is from the parameters jsonobject