gpt4 book ai didi

android - jsonArrayRequest 失败

转载 作者:行者123 更新时间:2023-11-29 02:26:46 24 4
gpt4 key购买 nike

我制作 jsonArrayRequest 的函数内容是:

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST,
FilesUsed.url_display_thought, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
JSONObject jsonObject1 = response.getJSONObject(0);
JSONObject jsonObject2 = response.getJSONObject(1);
if (jsonObject1.getBoolean("error")) {
ArrayList<String> list = new ArrayList<>();
list.add(jsonObject2.getString("message"));
displayThought(list);
} else {
int count = 0;
ArrayList<String> list = new ArrayList<>();
while (count < response.length()) {
try {
JSONObject jsonObject = response.getJSONObject(count);
String thought = jsonObject.getString("post");
list.add(thought);
count++;
} catch (JSONException e) {
e.printStackTrace();
}
}
displayThought(list);
}
} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
error.printStackTrace();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", "sainiakshay04");
return params;
}


};

RequestHandler.getInstance(this).addToRequestQueue(jsonArrayRequest);

它使用的php的内容是:

require_once '../app/display_personal_account.php';

$response=array();

if(isset($_POST['email'])){
$db = new display_personal_account($_POST['email']);

$res=$db->get_info_added("shared_content");
if($res==1){
$response=$db->display_shared_content();}
else{
$response[]['error']=true;
$response[]['message']="No Content To Display ";}

}

else{
$response[]['error']=true;
$response[]['message']="ERROR: INVALID REQUEST";
}




echo json_encode($response);

?>

但它显示的输出是 ERROR:INVALID REQUEST,尽管我使用 getParams 绑定(bind)电子邮件,但 php 的 isset 不工作并且将转到其他部分。帮助!

最佳答案

您正在检查 isset($_POST['email']) 但您在 JsonArrayRequest 中发送了 null 参数。

目前,JsonArrayRequest 仅支持 JsonArray 参数。

你需要做两件事:

  1. 在JsonArray参数中发送邮件

    JSONObject request = new JSONObject();
    request.put("email", userEmail);

    JSONArray arrayParam = new JSONArray();
    arrayParam.put(request);

    ....

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST,
    FilesUsed.url_display_thought, arrayParam, ...
  2. 在 PHP 中获取电子邮件

    require_once '../app/display_personal_account.php';

    $response=array();

    $inputJSON = file_get_contents('php://input');
    $input = json_decode($inputJSON, TRUE);

    if(isset($input[0]['email'])){
    $db = new display_personal_account($input[0]['email']);

    ....

在那之后,你应该可以正常工作了:)

关于android - jsonArrayRequest 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51830342/

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