gpt4 book ai didi

android volley 发布 json ID 并从 PHP 服务器获取结果

转载 作者:搜寻专家 更新时间:2023-11-01 09:50:18 27 4
gpt4 key购买 nike

我正在努力完成这项工作,基本上我使用 Intent 从以前的 Activity 中获得了一个 ID,现在我想将这个 ID 发送到服务器,以便它返回与这个 ID 关联的所有数据。

java代码

final String URL = "URL";
// Post params to be sent to the server
HashMap<String, String> params = new HashMap<String, String>();
params.put("ID", "1");

JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v("Response:%n %s", response.toString(4));
print response in textview;
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
});

// add the request object to the queue to be executed
ApplicationController.getInstance().addToRequestQueue(req);

PHP 服务器

if (!empty($_POST)) {

$query = " SELECT * FROM table WHERE ID = :ID " ;

$query_params = array(
':ID' => $_POST['ID'],);

try {
$statement = $db->prepare($query);
$result = $statement->execute($query_params);
}
catch (PDOException $ex) {


$response["success"] = 0;
$response["message"] = "Database query error";
die(json_encode($response));

}

$result = $statement->fetchAll();

if($result){

$data =array();

foreach($result as $rows){
$json = array();
$json["Name"] = $rows["Name"];





array_push ($data,$json);



}
echo stripcslashes(json_encode($data, JSON_PRETTY_PRINT));

最佳答案

我用了这个例子 http://www.itsalif.info/content/android-volley-tutorial-http-get-post-put并将字符串响应转换为字符串数组,对我有用

url = "http://httpbin.org/post";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", response);
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("name", "Alif");
params.put("domain", "http://itsalif.info");

return params;
}
};
queue.add(postRequest);

关于android volley 发布 json ID 并从 PHP 服务器获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36505614/

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