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;
}
}
|
|