AsyncTask异步加载和HttpURLConnection网络请求数据

//获得网络数据
    private void huodeshuju() {

//这里是使用线程,已注释掉
        /*new Thread(){
            public void run() {
                
                try {
                    URL url=new URL(urlPath);
                    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 line;
                        StringBuffer buffer=new StringBuffer();
                        while ((line=reader.readLine())!=null) {
                            buffer.append(line);
                            
                        }
                        String str=buffer.toString();
                        Message message=new Message();
                        message.what=0;
                        message.obj=str;
                        handler.sendMessage(message);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            };
        }.start();*/
        
       //----------下面是使用的异步加载-----------------------------


        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();
                }
                                
                return null;
                                                
            }
            
            protected void onPostExecute(String result) {
                
                try {
                    JSONObject obj=new JSONObject(result);
                    JSONArray data=obj.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject json=data.getJSONObject(i);
                        String id=json.getString("id");
                        String goods_name=json.getString("goods_name");
                        String shop_price=json.getString("shop_price");
                        String market_price=json.getString("market_price");
                        String is_coupon_allowed=json.getString("is_coupon_allowed");
                        String is_allow_credit=json.getString("is_allow_credit");
                        String goods_img=json.getString("goods_img");
                        String sales_volume=json.getString("sales_volume");
                        String efficacy=json.getString("efficacy");
                        Goods goodss=new Goods(id, goods_name, shop_price, market_price, is_coupon_allowed, is_allow_credit, goods_img, sales_volume, efficacy);
                       goods.add(goodss);
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            };
            
            
        }.execute(urlPath);//urlpath为网址
        
    }

转载于:https://www.cnblogs.com/changyiqiang/p/5809579.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值