AsyncTask非同步載入和HttpURLConnection網路請求資料

來源:互聯網
上載者:User

標籤:

//獲得網路資料
    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為網址
        
    }

AsyncTask非同步載入和HttpURLConnection網路請求資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.