A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© mengxiang1993 中级黑马   /  2015-1-29 21:08  /  1155 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends Activity {
        private TextView tv_date;
        private TextView tv_awaydays;
        private TextView tv_weather;
        private ImageView img;
        private Button btn_ok;

        String dates;
        long days;

        InputStream is;

        BufferedReader br;

        StringBuffer sb = new StringBuffer();

        Handler mHandler = new Handler() {
                public void handleMessage(Message msg) {

                        switch (msg.what) {
                        case 0x01:
                                tv_weather.setText(sb);
                                break;

                        case 2:
                                tv_date.setText(dates);

                        default:
                                break;
                        }
                }

        };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.fragment_main);
                tv_date = (TextView) findViewById(R.id.tv_date);
                tv_awaydays = (TextView) findViewById(R.id.tv_awaydays);
                tv_weather = (TextView) findViewById(R.id.tv_weather);
                btn_ok = (Button) findViewById(R.id.btn_ok);
                days = awayDays();
                String s = String.valueOf(days);
                tv_awaydays.setText(s + "天");
                btn_ok.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
                                new Thread() {
                                        public void run() {
                                                synchronized (this) {
                                                        try {
                                                                // 准备网址
                                                                URL url = new URL(
                                                                                "http://www.weather.com.cn/data/sk/101010100.html");
                                                                // 打开连接
                                                                HttpURLConnection urlConn = (HttpURLConnection) url
                                                                                .openConnection();
                                                                // 建立连接
                                                                urlConn.connect();
                                                                if (urlConn.getResponseCode() == 200) {
                                                                        is = urlConn.getInputStream();
                                                                        br = new BufferedReader(
                                                                                        new InputStreamReader(is, "UTF-8"));
                                                                        String str;
                                                                        while ((str = br.readLine()) != null) {
                                                                                sb.append(str);
                                                                        }
                                                                        Log.i("sb", sb.toString());
                                                                        sleep(5000);
                                                                        mHandler.sendEmptyMessage(0x01);
                                                                }
                                                        } catch (MalformedURLException e) {
                                                                e.printStackTrace();
                                                        } catch (IOException e) {
                                                                e.printStackTrace();
                                                        } catch (InterruptedException e) {
                                                                e.printStackTrace();
                                                        }

                                                }

                                        }
                                }.start();
                        }
                });



               
               
               

        }

        public long awayDays() {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                long time1 = calendar.getTimeInMillis();
                calendar.set(2014, 11, 21);
                long time2 = calendar.getTimeInMillis();
                long awaydays = (time2 - time1) / (1000 * 60 * 60 * 24);

                return awaydays;

        }

}


6 个回复

倒序浏览
加油{:2_32:}
回复 使用道具 举报
怎么还要导那么多的包啊?
回复 使用道具 举报
边晓炎 来自手机 中级黑马 2015-2-1 00:23:50
板凳
不错额(⊙o⊙)…!!
回复 使用道具 举报
楼主好厉害。
回复 使用道具 举报
来参观游一下好厉害的说
回复 使用道具 举报
导了这么多包。。。看晕了:L
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马