【问题标题】:AutoSuggest in Android from jsonencoded array of php来自 php 的 jsonencoded 数组的 Android 中的 AutoSuggest
【发布时间】:2015-07-24 12:24:12
【问题描述】:

我想使用从这个链接返回的 json 编码数组: http://sids.roundone.asia/suggest.json?data=soft 作为 android 应用程序中的建议。

(我在 php 文件中使用了 json_encode($arr) 函数,我将其作为上述链接的响应返回)

我在用 java 读取此响应并将其存储在 ArrayList 中时遇到问题。

我的代码是:

 try {
        String temp=sName.replace(" ", "%20");
        URL js = new URL("https://sids.roundone.asia/suggest.json?data="+temp);
        URLConnection jc = js.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
        String line = reader.readLine();
        JSONObject jsonResponse = new JSONObject(line);
        JSONArray jsonArray = jsonResponse.getJSONArray("results");
        for(int i = 0; i < jsonResponse.length(); i++){
            JSONObject r = jsonArray.getJSONObject(i);
            ListData.add(new SuggestGetSet(jsonResponse.get(String.vlaueOf(iss)));
        }
}

【问题讨论】:

    标签: java php android json


    【解决方案1】:

    正如我在您的链接上看到的,您返回的是 JSON 数组,而不是 JSON 对象(“[ ]”而不是“{ }”),然后在您的 java 代码中,您尝试创建一个JSONObject这里:

    JSONObject jsonResponse = new JSONObject(line);
    

    尝试将其更改为:

    JSONArray jsonResponse = new JSONArray(line);
    

    【讨论】:

      【解决方案2】:

      您直接返回 JSON 数组而不是 JSON 对象具有内部数组,因此将您的传入响应直接转换为 JSONArray

      JSONArray jsonResponse = new JSONArray(line);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多