如何在 android 中发布参数并将数据作为 JSONObject 返回?

我必须发布用户名、密钥和另一个参数才能将数据作为 JSONObject 获取并在 RecyclerView 中显示它们。我在 android 中使用 Volley Library,尝试了所有可能的方法,但没有一个有帮助。即使通过 Postman 进行测试似乎工作正常,并且当我发布这三个参数时我得到了数据,但我不知道我错过了什么安卓..


PHP代码:


<?php


require("db/Db.class.php");






if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['getMainCategory'])){


    $db = new DB();


    $username = $_POST['username'];


    $key = $_POST['key'];


    $query = $db->query("SELECT * FROM oc_api where `username` = '$username' and `key` = '$key' limit 1 ");




    if(count($query) == 0){


        $json = ["error don't have permittion"];


    }else{


        $catigory = $db->query("SELECT * FROM `oc_category_description`,oc_category WHERE oc_category_description.category_id = oc_category.category_id and oc_category.status = 1 and oc_category.parent_id = 0 and oc_category_description.language_id = 2");


        $json = $catigory;


    }




    header('Content-Type: application/json');


    echo json_encode($json);


    return;


}




if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['getSlideShow'])){


    $db = new DB();


    $username = $_POST['username'];


    $key = $_POST['key'];


    $slideShowId = (int)$_POST['getSlideShow'];


    $query = $db->query("SELECT * FROM oc_api where `username` = '$username' and `key` = '$key' limit 1 ");




    if(count($query) == 0){


        $json = ["error don't have permittion"];


    }else{


        $banderSlideShow = $db->query("SELECT * FROM `oc_banner_image` where `banner_id` = $slideShowId and language_id = 2");


        $json = $banderSlideShow;


    }


    header('Content-Type: application/json');


    echo json_encode($json);


    return;


}




if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['getAllNews'])){


    $db = new DB();


    $username = $_POST['username'];


    $key = $_POST['key'];


    $query = $db->query("SELECT * FROM oc_api where `username` = '$username' and `key` = '$key' limit 1 ");




    if(count($query) == 0){


        $json = ["error don't have permittion"];


    }



FFIVE
浏览 120回答 3
3回答

MMMHUHU

您已经完成了所有代码,并且在发送响应之前设置了 json=["error"];[不要忘记接受答案并在答案正确的情况下点赞或阐明(更多解释)您的问题,以便回答者可以给出更相关的答案]<?phprequire("db/Db.class.php");if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['getMainCategory'])){&nbsp; &nbsp; //your code&nbsp; &nbsp; //remove below two lines from every if as it can be called commonly at last;&nbsp; &nbsp; //header('Content-Type: application/json');&nbsp; &nbsp; //echo json_encode($json);}else if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['getSlideShow'])){&nbsp; &nbsp; //your code}else if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['getAllNews'])){&nbsp; &nbsp; //your code}else{&nbsp; &nbsp; $json = ["error"];}header('Content-Type: application/json');echo json_encode($json);?>

慕标5832272

我解决了这个问题,实际上这很荒谬。在第一个 params.put 中,“用户名”旁边有一个小空间,这导致了这个问题,没有获取数据,也导致了异常。即使我仔细检查了,但没有不知道我是怎么错过的……感谢所有花时间帮助我的人。我很感激。@Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected Map<String,String> getParams(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String,String> params = new HashMap<String, String>();&nbsp; &nbsp; &nbsp; &nbsp;//the line below was causing the exception. right side of "username " (space)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params.put("username ",user);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params.put("key",api);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params.put("getMainCategory",no);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return params;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

慕哥6287543

由于您期待 JSONArray,因此不要选择 StringRequest,而是使用 volley 中可用的 JsonArrayRequest。请参阅此文档: https:&nbsp;//developer.android.com/training/volley/request您的错误日志也表明java.lang.String 无法转换为 JSONObject因此我建议你试试这个:JsonArrayRequest req = new JsonArrayRequest(Request.Method.POST, URL_PRODUCTS,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Response.Listener<String>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(JSONArray response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//.............因为你期待一个 json 数组。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java