3 回答
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);
?>
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;
}
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ù)組。
添加回答
舉報(bào)
