dreamfly2016 2015-07-24 12:24
浏览 73

Android中的AutoSuggest来自jsonencoded的php数组

I want to use json encoded array which i am return from this link : http://sids.roundone.asia/suggest.json?data=soft as suggestions in android application.

(I have used json_encode($arr) function in php file and i am returning that as response for above link)

I have a problem in reading this response in java and storing it in an ArrayList.

My code is :

 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)));
        }
}
  • 写回答

2条回答 默认 最新

  • duandeng2011 2015-07-24 13:13
    关注

    As I could see on your link, you're returning a JSON Array, instead of a JSON Object, ( "[ ]" instead of "{ }") and then in your java code you're trying to create a JSONObject here:

    JSONObject jsonResponse = new JSONObject(line);
    

    Try changing that to:

    JSONArray jsonResponse = new JSONArray(line);
    
    评论

报告相同问题?