gpt4 book ai didi

java - JSONArrayRequest 和 PHP 对不起作用

转载 作者:行者123 更新时间:2023-11-30 22:40:06 24 4
gpt4 key购买 nike

我正在创建类,该类应使用带有特定参数的 volley 发送 JSONArrayRequest,然后找到等于这些参数的 mysql 行并将其发回。我编写了简单的 mysql 函数来接收 POST 参数和发送和接收 JSONObjects 的 Java 方法。但它不起作用,我什么也没收到。

这是我的 PHP 文件,名为 details.php。它从我的 Android 应用程序接收参数并发回一个数组。

require_once __DIR__ . '/db_connect.php'
$db = new DB_CONNECT();
$path= $_POST["path"];

$result = mysql_query("SELECT * FROM modx_ms2_product_files WHERE path = $path") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$json_response = array();

while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product ["file"] = $row["file"];

// push single product into final response array
array_push($json_response, $product);
}

// echoing JSON response
echo json_encode($json_response);

$conn->close();
}

这是我的方法 getArray(),它发送请求并接收响应:

 public void getarray() {
String path = "1578/";
final String URL = "http://myurl.io/details.php";
HashMap<String, String> params = new HashMap<String, String>();
params.put("path", path);

JsonArrayRequest customarray = new JsonArrayRequest(Request.Method.POST,URL,new JSONObject(params),new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
if (response.length() > 0) {
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject Obj = response.getJSONObject(i);

pics pic = new pics();
pic.setFile(Obj.getString("file"));
Log.i("cmon", Obj.getString("file"));
pics.add(pic);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});
AppController.getInstance().addToRequestQueue(customarray);
}

这是我的modx_ms2_product_files 表:

http://i.imgur.com/yl1f7Ze.png

http://i.imgur.com/9V6PrBU.png

最佳答案

您是否尝试过使用 StringRequest?出于某种原因,当我使用 JsonArrayRequest 时,我没有从服务器收到任何响应。如果您决定创建一个 StringRequest,您可以将您的响应转换为一个 JSONArray,如下所示:

StringRequest get = new StringRequest(Method.POST, URL, new Listener<String>() {

@Override
public void onResponse(String response) {
try {
JSONArray myArray = new JSONArray(response);

for(int i = 0; i < myArray.length(); i++)
{
JSONObject jObj = myArray.getJSONObject(i);
// get your data here

}

} catch (JSONException e) {
e.printStackTrace();
}
}
}

然后当然将您的请求添加到请求队列中。

关于java - JSONArrayRequest 和 PHP 对不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31368206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com