源码

首页 » 归档 » 源码 » ANDROID,从Web服务器解析JSON数据并在ListView上…

ANDROID,从Web服务器解析JSON数据并在ListView上…


我试图从JSON url链接显示JSON结果.目前,当我加载时,它什么都不显示,只是空白页面. This is the source where I got information about JSON.

这是我的代码:

public class DVLAresult extends AppCompatActivity {


    public class DVLAlist extends ListActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.content_dvlaresult);


            setListAdapter(new ArrayAdapter(
                    this,android.R.layout.simple_list_item_2,
                    this.populate()));
        }


        private ArrayList populate() {
            ArrayList items = new ArrayList();

            TextView newtext = (TextView) findViewById(R.id.view_number);

            try {
                URL url = new URL
                        ("https://dvlasearch.appspot.com/DvlaSearch?licencePlate=mt09nks&apikey=DvlaSearchDemoAccount");
                HttpURLConnection urlConnection =
                        (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();
                // gets the server json data
                BufferedReader bufferedReader =
                        new BufferedReader(new InputStreamReader(
                                urlConnection.getInputStream()));
                String next;
                while ((next = bufferedReader.readLine()) != null) {
                    JSONArray ja = new JSONArray(next);

                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject jo = (JSONObject) ja.get(i);
                        items.add(jo.getString("text"));
                    }
                }
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return items;
        }
    }
}

这是我的XML文件simple_list_2.xml





    

    

    

... Continue up to text18, because I have 18 fields.


这是主要的XML ListView

 
(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/androidcongwebfuwuqijiexijsonshujubingzailistviewshang.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9月 26, 2019 at 03:20 上午

热评文章