我为我的应用程序提供了一个Web服务,在那里我查阅了必要的数据 . 返回表的所有数据的查询正常工作,但我需要根据android studio中变量的值显示数据 . 一切都在浏览器中正常工作(ULR中的参数),但在AS中没有 .
这是我与Volley的电话:
final String module = moduleContent; //this value is "coding"
StringRequest stringRequest = new StringRequest(Request.Method.GET, Constans.GET_SCHOOL_CONTENT,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "onResponse: " + response);
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject schoolContent = array.getJSONObject(i);
//adding the product to product list
schoolContentList.add(new SchoolContent(
schoolContent.getString("module"),
schoolContent.getString("title"),
schoolContent.getString("content_url")
));
}
//creating adapter object and setting it to recyclerview
SchoolAdapter adapter = new SchoolAdapter(SchoolModuleActivity.this, schoolContentList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
public Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("module", module);
return params;
}
};
VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);
这是我的PHP代码:
public static function getByModule($module){
$consulta = "SELECT module, title, content_url FROM " .self::table_name." WHERE module = ?";
//$consulta = "SELECT module, title, content_url FROM " .self::table_name." WHERE ".self::$module." = " .$module;
try {
$comando = DatabaseConnection::getInstance()->getDb()->prepare($consulta);
$comando->execute([$module]);
return $comando->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
return $consulta;
}
}
前一个是类,对函数的调用是这样的:
require '../data/school_content.php';
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$module = $_GET['module'];
$content = school_content::getByModule($module);
header('Content-Type: application/json');
print json_encode($content);
}
AS中的结果: