php - 使用 volley 从 localhost mysql 通过那里的 id 获取文本或图像
问题描述
描述:
- 我在 mysql 数据库中存储了一些文本和图像(以 url 形式)。
- 现在我想使用 volley request 方法在特定的文本视图中获取该文本,或者通过那里的 id 获取特定图像视图中的图像
- 我还想使用 Glide 在分配的 imageView 中显示该特定图像。
- 下面是我尝试仅获取文本视图但遇到一些问题的代码,如果有人知道如何获取图像也请帮忙。
爪哇:
@Override
public void onResponse(String response) {
try { JSONObject jsonobject = new JSONObject(response);
JSONArray jsonarray = jsonobject.getJSONArray("name");
JSONObject data = jsonarray.getJSONObject(0);
String firstName = data.getString("pricewithfuel");
textview.setText(firstName);
} catch (JSONException e) {
e.printStackTrace();
}}},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("id",editText.getText().toString());
return params;
}};
RequestQueue requestQueue = Volley.newRequestQueue(mActivity);
requestQueue.add(stringRequest);
PHP 代码
$connect=mysqli_connect($username,$hostname,$password,$dbname);
$id = $_POST["id"];
$query = "SELECT title FROM products WHERE id = '$id'";
$result = mysqli_query($connect, $query);
$number_of_rows = mysqli_num_rows($result);
$response = array();
if($number_of_rows > 0) {
while($row = mysqli_fetch_assoc($result)) {
$response[] = $row;
}
}
header('Content-Type: application/json');
echo json_encode(array("users"=>$response));
mysqli_close($connect);
解决方案
您只从数据库中获取标题,再次检查您的查询
SELECT title FROM products WHERE id = '$id'
从要显示数据的数据库中添加更多数据字段
推荐阅读
- java - 如何在 @NotBlank(message="...") 注释的自定义异常包装器中设置错误消息?
- node.js - Docker + nodejs - JavaScript堆内存不足
- c - 如何向后移动文件指针
- javascript - 通过 Ajax 显示来自返回 Json 数据的 PHP 查询的数据
- aws-lambda - 无服务器框架 AWS REST API Gateway - 403 CORS 错误
- python - 为什么无法通过 dazl python 连接到账本 Daml-on-Sawtooth?
- android - “flutter pub get”无法获取依赖插件
- c# - 检测车轮碰撞 Unity 3D
- java - 不包括来自用户输入的数字作为变量并让它增加另一个变量?
- java - apache activemq 到 artemis 的 Spring Boot 迁移