第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 android 中發(fā)布參數(shù)并將數(shù)據(jù)作為 JSONObject 返回?

如何在 android 中發(fā)布參數(shù)并將數(shù)據(jù)作為 JSONObject 返回?

FFIVE 2023-03-17 13:45:07
我必須發(fā)布用戶名、密鑰和另一個(gè)參數(shù)才能將數(shù)據(jù)作為 JSONObject 獲取并在 RecyclerView 中顯示它們。我在 android 中使用 Volley Library,嘗試了所有可能的方法,但沒有一個(gè)有幫助。即使通過 Postman 進(jìn)行測(cè)試似乎工作正常,并且當(dāng)我發(fā)布這三個(gè)參數(shù)時(shí)我得到了數(shù)據(jù),但我不知道我錯(cuò)過了什么安卓..PHP代碼:<?phprequire("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"];    }
查看完整描述

3 回答

?
MMMHUHU

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊

您已經(jīng)完成了所有代碼,并且在發(fā)送響應(yīng)之前設(shè)置了 json=["error"];


[不要忘記接受答案并在答案正確的情況下點(diǎn)贊或闡明(更多解釋)您的問題,以便回答者可以給出更相關(guān)的答案]


<?php


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

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

    //your code


    //remove below two lines from every if as it can be called commonly at last;

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

    //echo json_encode($json);

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

    //your code

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

    //your code

}else{

    $json = ["error"];

}


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

echo json_encode($json);

?>


查看完整回答
反對(duì) 回復(fù) 2023-03-17
?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊

我解決了這個(gè)問題,實(shí)際上這很荒謬。在第一個(gè) params.put 中,“用戶名”旁邊有一個(gè)小空間,這導(dǎo)致了這個(gè)問題,沒有獲取數(shù)據(jù),也導(dǎo)致了異常。即使我仔細(xì)檢查了,但沒有不知道我是怎么錯(cuò)過的……感謝所有花時(shí)間幫助我的人。我很感激。


@Override

            protected Map<String,String> getParams(){

                Map<String,String> params = new HashMap<String, String>();

       //the line below was causing the exception. right side of "username " (space)

                params.put("username ",user);

                params.put("key",api);

                params.put("getMainCategory",no);

                return params;

            }


查看完整回答
反對(duì) 回復(fù) 2023-03-17
?
慕哥6287543

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊

由于您期待 JSONArray,因此不要選擇 StringRequest,而是使用 volley 中可用的 JsonArrayRequest。請(qǐng)參閱此文檔: https: //developer.android.com/training/volley/request

您的錯(cuò)誤日志也表明

java.lang.String 無法轉(zhuǎn)換為 JSONObject

因此我建議你試試這個(gè):


JsonArrayRequest req = new JsonArrayRequest(Request.Method.POST, URL_PRODUCTS,

            new Response.Listener<String>() {

                @Override

                public void onResponse(JSONArray response) {

             //.............

因?yàn)槟闫诖粋€(gè) json 數(shù)組。


查看完整回答
反對(duì) 回復(fù) 2023-03-17
  • 3 回答
  • 0 關(guān)注
  • 193 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)