gpt4 book ai didi

java - Android Volley - POST 参数未到达 PHP 脚本

转载 作者:行者123 更新时间:2023-12-02 13:42:32 25 4
gpt4 key购买 nike

我想从数据库获取信息。当我使用不带参数的查询时(例如“从用户中选择*;”我的脚本工作正常。

现在我有以下脚本,它返回一个空数组。当我用字符串值(例如“Group1”)替换脚本中的 $groupId 时,它会返回预期的项目。

<?php


$sql = "";
if($_SERVER['REQUEST_METHOD']== 'POST') {
$flag = array();

$groupId = $_POST['groupId'];



require_once('dbConnect.php');

$sql = "SELECT * FROM users where groupId = '$groupId'";

if($out = mysqli_query($con,$sql)){
//echo "Successfully Registered";

while($row = mysqli_fetch_array($out))
{

$flag[] = $row;

}
print(json_encode($flag));

}else{
echo "Could not register";

}
}else{
echo 'error';
}


//mysqli_close($con);
?>

我使用 HashMap 调用以下函数hashMapGetNames.put("groupId", "Group#1");

然后我得到一个空数组和信息:“不,组 ID 未设置”

public ArrayList<String> get(String url,  final HashMap<String, String> hashMap,  final Context context) {


//Download the items from DB
final ArrayList<String> items = new ArrayList<>();
RequestQueue queue = Volley.newRequestQueue(context);
JSONObject parameters = new JSONObject(hashMap);
final JsonRequest<JSONArray> jsonArrayRequest = new
JsonRequest<JSONArray>(Request.Method.GET, url, parameters.toString(),
new com.android.volley.Response.Listener<JSONArray>() {

@Override
public void onResponse(JSONArray jsonArray) {
if(jsonArray == null){
Log.d("Downloader", "FAIL: NO NAMES FOUND");
}
else {
for (int zähler = 0; zähler < jsonArray.length(); zähler++){
try {
Log.d("Downloader", "sind bei zähler " + zähler);
JSONObject object = jsonArray.getJSONObject(zähler);
String name = object.getString("name");
Log.d("Name", name);
items.add(name);
}
catch (JSONException e) {
Log.d("Downloader", "Catching exception");
e.printStackTrace();
}
}
}



}
}, new com.android.volley.Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.d("Downloader", "Error: " + error.toString());

}

})

{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Log.d("Downloader", "gettingParams");
return hashMap;
}

@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
Log.d("Downloader", "parsing Network response");
try {
String jsonString = new String(response.data,
HttpHeaderParser
.parseCharset(response.headers));
Log.d("Downloader", "Parsing success");
return Response.success(new JSONArray(jsonString),
HttpHeaderParser
.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
Log.d("Downloader", e.toString());
return Response.error(new ParseError(e));

} catch (JSONException je) {
Log.d("Downloader", je.toString());
return Response.error(new ParseError(je));
}
}
};

queue.add(jsonArrayRequest);

return items;

}

所以脚本看起来是正确的,但参数没有到达脚本。有什么想法可以隐藏错误吗?

编辑:我编辑了下面评论的教程中所示的代码,并使用相同的 dbConnect.php 文件。但它打印出错误 - 所以问题仍然存在。这意味着请求方法不是“Post”。Android 监视器打印出:

parsing Network response

Downloader: Parsing success

Error: com.android.volley.ParseError: org.json.JSONException: End of input at character 0

最佳答案

试试这个代码...

         @Override
protected Map<String, String> getParams() throws AuthFailureError {


//Creating parameters
Map<String,String> params = new Hashtable<String, String>();

//Adding parameters
params.put("groupId", "122");


//returning parameters
return params;
}

关于java - Android Volley - POST 参数未到达 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42670457/

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