zoukankan      html  css  js  c++  java
  • AsyncTask异步加载和Gson

    public class MainActivity extends Activity {
        private List<Goods> goods=new ArrayList<Goods>();

        private ListView lv;
        private SmartImageView smartImageView;

        private MyAdapter adapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            lv = (ListView) findViewById(R.id.lv);
            adapter = new MyAdapter(goods, this);
            lv.setAdapter(adapter);
            //获得数据
            huodeshuju();
            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    Intent intent=new Intent(MainActivity.this,TwoActivity.class);
                    startActivity(intent);
                    
                    
                }
            });
        }
        //获得数据
        private void huodeshuju() {
            //使用异步
            new AsyncTask<String, Void, String>(){

                @Override
                protected String doInBackground(String... params) {
                    
                    /*try {
                        URL url=new URL(params[0]);
                        HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
                        urlConnection.setConnectTimeout(5000);
                        urlConnection.setReadTimeout(5000);
                        urlConnection.setRequestMethod("GET");
                        urlConnection.connect();
                        int code=urlConnection.getResponseCode();
                        if (code==200) {
                            InputStream inputStream=urlConnection.getInputStream();
                            BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
                            String readerline;
                            StringBuffer buffer=new StringBuffer();
                            while ((readerline=reader.readLine())!=null) {
                                buffer.append(readerline);
                                
                            }
                            String str=buffer.toString();
                            return str;
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }*/
                    
                    
                    
                    
                    try {
                        HttpClient httpClient=new DefaultHttpClient();
                        HttpGet httpGet=new HttpGet(params[0]);
                        HttpResponse response = httpClient.execute(httpGet);
                        StatusLine statusLine=response.getStatusLine();
                        int code=statusLine.getStatusCode();
                        if (code==200) {
                            HttpEntity httpEntity=response.getEntity();
                            String str=EntityUtils.toString(httpEntity, "utf-8");
                            return str.toString();
                        }
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                    
                    return null;
                }
                protected void onPostExecute(String result) {
                    try {
                        JSONObject obj=new JSONObject(result);
                        JSONObject list=obj.getJSONObject("list");
                        JSONArray articles=list.getJSONArray("articles");
                        Type type=new TypeToken<List<Goods>>(){
                            
                        }.getType();
                        
                        Gson gson=new Gson();
                        List<Goods> goodss=gson.fromJson(articles.toString(), type);
                        for (Goods goods2 : goodss) {
                            
                            String title=goods2.getTitle();
                            String litpic=goods2.getLitpic();
                            String typeid=goods2.getTypeid();
                            Goods goods3=new Goods(title, litpic, typeid);
                            
                            goods.add(goods3);
                            
                            
                        }
                        //Log.i("++++++++", goods.toString());
                        //=======================
                        //必须刷新
                        adapter.notifyDataSetChanged();
                        //======================
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                };
                
            }.execute("http://101.200.142.201:8080/tqyb/newsList.json");
            
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }

  • 相关阅读:
    vue form dynamic validator All In one
    TypeScript api response interface All In One
    closable VS closeable All In One
    macOS 如何开启 WiFi 热点 All In One
    vue css inline style All In One
    vs2010里面 新建网站里面的 asp.net网站 和 新建项目里面的 asp.net Web应用程序 的区别 (下)
    牛腩新闻 59 整合添加新闻页 FreeTextBox 富文本编辑器,检测到有潜在危险的 Request.Form 值,DropDownList 的使用
    牛腩新闻 61尾声: error.aspx的使用 防止报错
    vs2010里面 新建网站里面的 asp.net网站 和 新建项目里面的 asp.net Web应用程序 的区别 (上)
    牛腩新闻 62:尾声续2 asp.net的编译和发布
  • 原文地址:https://www.cnblogs.com/changyiqiang/p/5732761.html
Copyright © 2011-2022 走看看