标签:
之前实现过了天气预报的功能 但是真的好丑 真的只是实现功能 所以上一篇博客也没有贴出图片 这次 相对于第一个 首先是界面做了调整 其次就是 之前那个只能查看实时天气 这个天气预报我还加入了未来天气
先 看一下对比图
因为这是给我杨凌一个闺蜜写的逗她开心也练练技术 所以中间那部分是自己定义的一些话 当然可以替换成穿衣建议什么的
不管之前那个天气预报这个重新来一遍
在你刚刚创建好这个项目的时候 因为需要联网操作 所以我们需要先导入网络包和在manifests文件中进行网络权限的申请
1 导入网络包
dependencies {
compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
testCompile ‘junit:junit:4.12‘
compile ‘com.android.support:appcompat-v7:23.2.1‘
compile files(‘libs/httpclient-4.4.1.jar‘)
compile files(‘libs/httpcore-4.4.1.jar‘)
}
那个http的两个就是这次需要的网络包
要注意的是在加入网络包的同时 我们还需要在上面加上这样一段代码
packagingOptions {//导入http包的时候必须要加的以下这段话
exclude ‘META-INF/DEPENDENCIES.txt‘
exclude ‘META-INF/LICENSE.txt‘
exclude ‘META-INF/NOTICE.txt‘
exclude ‘META-INF/NOTICE‘
exclude ‘META-INF/LICENSE‘
exclude ‘META-INF/DEPENDENCIES‘
exclude ‘META-INF/notice.txt‘
exclude ‘META-INF/license.txt‘
exclude ‘META-INF/dependencies.txt‘
exclude ‘META-INF/LGPL2.1‘
}
Manifests文件中是这样的
在application之外加上
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
基本准备工作就完成了这样在编码的工程中会减少很多错误
现在就开始要入手这个项目的第一个关键的部分就是layout
我的布局写的很烂基本都是最简单的控件组成的显示大部分用的TextView
先把代码贴上来 因为id这个在后面有些不知道的还要查这个代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/back"
android:background="@mipmap/sunny_overcast"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
/>
<TextView
android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="杨凌"
android:gravity="center"
android:textSize="40dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/weather_day"
android:text="happy"
android:textSize="20dp"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/wendu"
android:text="15"
android:textSize="90dp"
android:gravity="center"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#ffffff"/>
<TextView
android:id="@+id/remind"
android:textSize="20dp"
android:layout_width="fill_parent"
android:layout_height="80dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#ffffff"/>
<LinearLayout
android:id="@+id/la"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"/>
<TextView
android:id="@+id/day_one"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="星期"
android:gravity="center"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/image_one"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/high_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="high" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/low_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="low"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
</LinearLayout>
<LinearLayout
android:id="@+id/lb"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"/>
<TextView
android:id="@+id/day_two"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="星期"
android:gravity="center"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/image_two"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/high_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="high" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/low_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="low"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
</LinearLayout>
<LinearLayout
android:id="@+id/lc"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"/>
<TextView
android:id="@+id/day_three"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="星期"
android:gravity="center"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/image_three"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/high_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="high" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/low_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="low"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ld"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"/>
<TextView
android:id="@+id/day_four"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="星期"
android:gravity="center"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/image_four"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/high_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="high" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/low_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="low"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
</LinearLayout>
<LinearLayout
android:id="@+id/le"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"/>
<TextView
android:id="@+id/day_five"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="星期"
android:gravity="center"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/image_five"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/high_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="high" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/low_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="low"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
</LinearLayout>
<LinearLayout
android:id="@+id/lf"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"/>
<TextView
android:id="@+id/day_six"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="星期"
android:gravity="center"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/image_six"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/high_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="high" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/low_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="low"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
</LinearLayout>
<LinearLayout
android:id="@+id/lg"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"/>
<TextView
android:id="@+id/day_seven"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="星期"
android:gravity="center"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/image_seven"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/high_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="high" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
<TextView
android:id="@+id/low_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="low"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#ffffff"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="5dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"/>
<EditText
android:text="杨凌"
android:background="@drawable/shape"
android:id="@+id/serach_city"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textSize="20dp"
android:hint="Input City"
android:textColorHint="#ee9999"
android:layout_weight="3"
/>
<Button
android:id="@+id/change"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:background="@drawable/btn"
android:text="切换城市"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
哦 那个SwipeRefreshLayout是我准备做下拉刷新的 但是后面其实没有用到 所以把这个控件去掉也可以
因为闺蜜是杨凌的所以一开始的edittext就初始的是杨凌
这个项目只有一个activity
先把代码贴上来
package com.example.katherine_qj.dayweather;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
private TextView city;
private TextView weather_day;
private TextView wendu;
private TextView remind;
private EditText search_city;
private Button change;
private TextView day_one;
private TextView image_one;
private TextView high_one;
private TextView low_one;
private LinearLayout bei;
private LinearLayout la;
private LinearLayout lb;
private LinearLayout lc;
private LinearLayout ld;
private LinearLayout le;
private LinearLayout lf;
private LinearLayout lg;
private TextView day_two;
private TextView image_two;
private TextView high_two;
private TextView low_two;
private TextView day_three;
private TextView image_three;
private TextView high_three;
private TextView low_three;
private TextView day_four;
private TextView image_four;
private TextView high_four;
private TextView low_four;
private TextView day_five;
private TextView image_five;
private TextView high_five;
private TextView low_five;
private TextView day_six;
private TextView image_six;
private TextView high_six;
private TextView low_six;
private TextView day_seven;
private TextView image_seven;
private TextView high_seven;
private TextView low_seven;
private static Handler handler;
private static Handler handler_now;
private SwipeRefreshLayout swipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
if(!isNetworkConnected())
{
Toast toast = Toast.makeText(getApplicationContext(), "宝宝你手机没有网", Toast.LENGTH_LONG);//显示时间较长
toast.setGravity(Gravity.CENTER, 0, 0);// 居中显示
toast.show();
}
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Weather_Thread weather_thread =new Weather_Thread();
Weather_now_Thread weather_now_thread = new Weather_now_Thread();
new Thread(weather_thread).start();
new Thread(weather_now_thread).start();
handler = new Myhandler();
handler_now = new Myhandler_now();
swipeRefreshLayout.setRefreshing(false);
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
swipeRefreshLayout.setRefreshing(false);
}
}, 10000);
}
});
Weather_Thread weather_thread =new Weather_Thread();
Weather_now_Thread weather_now_thread = new Weather_now_Thread();
new Thread(weather_thread).start();
new Thread(weather_now_thread).start();
handler = new Myhandler();
handler_now = new Myhandler_now();
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Weather_Thread weather_thread =new Weather_Thread();
Weather_now_Thread weather_now_thread = new Weather_now_Thread();
new Thread(weather_thread).start();
new Thread(weather_now_thread).start();
handler = new Myhandler();
handler_now = new Myhandler_now();
}
});
}
public static String getURLConnection(String path){
String xml = "";
try{
HttpClient client = new DefaultHttpClient();//创建一个httpclient对象
HttpGet get = new HttpGet(path);
HttpResponse response = client.execute(get);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {//如果返回200才算成功才可以继续执行
InputStream reader = response.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(reader));
String list = buffer.readLine();//读一行
while (list != null) {
xml += list;
list = buffer.readLine();
}
}
}catch (Exception e) {
e.printStackTrace();
}
return xml;
}
public class Weather_now_Thread implements Runnable{
public void run(){
String first = "http://api.k780.com:88/?app=weather.today&weaid=";
String city = search_city.getText().toString().trim();
String last="&&appkey=18276&sign=3f3716462bb4ed68bd082761c3602509&format=json";
String Weather_now=getURLConnection(first+city+last);
System.out.println("sept2"+Weather_now);
Message msg_now = new Message();
Bundle bundle = new Bundle();
bundle.putString("Weather_now", Weather_now);
msg_now.setData(bundle);
handler_now.sendMessage(msg_now);
}
}
public class Weather_Thread implements Runnable{
public void run(){
String first = "http://api.k780.com:88/?app=weather.future&weaid=";
String city = search_city.getText().toString().trim();
String last="&&appkey=18276&sign=3f3716462bb4ed68bd082761c3602509&format=json";
String Weather=getURLConnection(first+city+last);
Message msg1 = new Message();
Bundle bundle1 = new Bundle();
bundle1.putString("Weather", Weather);
msg1.setData(bundle1);
handler.sendMessage(msg1);
}
}
public class Myhandler_now extends Handler{
public void handleMessage(Message msg){
String Weather_now = msg.getData().getString("Weather_now");
Log.e("se",Weather_now);
if(!Weather_now.equals("")){
Log.e("see",Weather_now);
try {
JSONObject json = new JSONObject(Weather_now).getJSONObject("result");
Log.e("see",json.toString());
int jsonq = new JSONObject(Weather_now).getInt("success");
System.out.println("septqqq" + jsonq);
city.setText(json.getString("citynm"));
Init(json.getString("week"));
weather_day.setText(json.getString("weather"));
wendu.setText(json.getString("temperature_curr"));
Init_Image(json.getString("weather"));
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
public class Myhandler extends Handler{
public void handleMessage(Message msg){
String Weather = msg.getData().getString("Weather");
System.out.println("septa"+Weather);
if(!Weather.equals("")){
try {
JSONArray jsonObjs = new JSONObject(Weather).getJSONArray("result");
// int jsona = new JSONObject(Weather).getInt ("success");
// System.out.println("septqqq"+jsona);
for (int i = 0; i < jsonObjs.length(); i++) {
JSONObject jsonOb0 = (JSONObject) jsonObjs.get(i);
if (i == 0) {
day_one.setText(jsonOb0.getString("week"));
image_one.setText(jsonOb0.getString("weather"));
high_one.setText(jsonOb0.getString("temp_high") + "°");
low_one.setText(jsonOb0.getString("temp_low") + "°");
}
if (i == 1) {
day_two.setText(jsonOb0.getString("week"));
image_two.setText(jsonOb0.getString("weather"));
high_two.setText(jsonOb0.getString("temp_high") + "°");
low_two.setText(jsonOb0.getString("temp_low") + "°");
}
if (i == 2) {
day_three.setText(jsonOb0.getString("week"));
image_three.setText(jsonOb0.getString("weather"));
high_three.setText(jsonOb0.getString("temp_high") + "°");
low_three.setText(jsonOb0.getString("temp_low") + "°");
}
if (i == 3) {
day_four.setText(jsonOb0.getString("week"));
image_four.setText(jsonOb0.getString("weather"));
high_four.setText(jsonOb0.getString("temp_high") + "°");
low_four.setText(jsonOb0.getString("temp_low") + "°");
}
if (i == 4) {
day_five.setText(jsonOb0.getString("week"));
image_five.setText(jsonOb0.getString("weather"));
high_five.setText(jsonOb0.getString("temp_high") + "°");
low_five.setText(jsonOb0.getString("temp_low") + "°");
}
if (i == 5) {
day_six.setText(jsonOb0.getString("week"));
image_six.setText(jsonOb0.getString("weather"));
high_six.setText(jsonOb0.getString("temp_high") + "°");
low_six.setText(jsonOb0.getString("temp_low") + "°");
}
if (i == 6) {
day_seven.setText(jsonOb0.getString("week"));
image_seven.setText(jsonOb0.getString("weather"));
high_seven.setText(jsonOb0.getString("temp_high") + "°");
low_seven.setText(jsonOb0.getString("temp_low") + "°");
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void Init_Image(String str){
if (str.equals("晴")){
bei.setBackgroundDrawable(getResources().getDrawable(R.mipmap.sunny));
Init_T();
}
else if(str.equals("多云转小雨")||str.equals("小雨转多云")||str.equals("小雨")){
bei.setBackgroundDrawable(getResources().getDrawable(R.mipmap.light_rain));
Init_T();
}
else if(str.equals("多云转晴")||str.equals("晴转多云")||str.equals("多云转阴")||str.equals("阴转多云")||str.equals("多云")){
bei.setBackgroundDrawable(getResources().getDrawable(R.mipmap.cloudy));
Init_T();
}else{
bei.setBackgroundDrawable(getResources().getDrawable(R.mipmap.frost));
Init_T();
}
}
public void Init_T(){
la.getBackground().setAlpha(100);
lb.getBackground().setAlpha(100);
lc.getBackground().setAlpha(100);
ld.getBackground().setAlpha(100);
le.getBackground().setAlpha(100);
lf.getBackground().setAlpha(100);
lg.getBackground().setAlpha(100);
}
public void Init(String str){
if(str.equals("星期一")){
remind.setText("水力学,水文地质学,工程结构,马克思主义基本原理");
}
else if(str.equals("星期二")){
remind.setText("地理信息系统,工程测量,工程结构");
}
else if(str.equals("星期三")){
remind.setText("工程结构,地理信息系统,水文地质学,水力学");
}
else if(str.equals("星期四")){
remind.setText("工程结构,工程测量,水力学,马克思主义基本原理");
}
else if(str.equals("星期五")){
remind.setText("体育,水力学");
}
else if(str.equals("星期六")){
remind.setText("宝宝今天是周六 好好休息 看看电视想想我");
}
else if(str.equals("星期日")){
remind.setText("宝宝今天是周末,明天就是星期一了 今天你该去运动的!爱你爱你");
}
else{
remind.setText("宝宝你要是看到这句话就证明世界末日了!!!!!!");
}
}
public void Init(){
city=(TextView)findViewById(R.id.city);
weather_day = (TextView)findViewById(R.id.weather_day);
wendu = (TextView)findViewById(R.id.wendu);
remind = (TextView)findViewById(R.id.remind);
change = (Button)findViewById(R.id.change);
search_city =(EditText)findViewById(R.id.serach_city);
day_one = (TextView)findViewById(R.id.day_one);
image_one = (TextView)findViewById(R.id.image_one);
high_one = (TextView)findViewById(R.id.high_one);
low_one = (TextView)findViewById(R.id.low_one);
day_two = (TextView)findViewById(R.id.day_two);
image_two = (TextView)findViewById(R.id.image_two);
high_two = (TextView)findViewById(R.id.high_two);
low_two = (TextView)findViewById(R.id.low_two);
day_three = (TextView)findViewById(R.id.day_three);
image_three = (TextView)findViewById(R.id.image_three);
high_three = (TextView)findViewById(R.id.high_three);
low_three = (TextView)findViewById(R.id.low_three);
day_four = (TextView)findViewById(R.id.day_four);
image_four = (TextView)findViewById(R.id.image_four);
high_four = (TextView)findViewById(R.id.high_four);
low_four = (TextView)findViewById(R.id.low_four);
day_five = (TextView)findViewById(R.id.day_five);
image_five = (TextView)findViewById(R.id.image_five);
high_five = (TextView)findViewById(R.id.high_five);
low_five = (TextView)findViewById(R.id.low_five);
day_six = (TextView)findViewById(R.id.day_six);
image_six = (TextView)findViewById(R.id.image_six);
high_six = (TextView)findViewById(R.id.high_six);
low_six = (TextView)findViewById(R.id.low_six);
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_container);
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
day_seven = (TextView)findViewById(R.id.day_seven);
image_seven = (TextView)findViewById(R.id.image_seven);
high_seven = (TextView)findViewById(R.id.high_seven);
low_seven = (TextView)findViewById(R.id.low_seven);
bei= (LinearLayout)findViewById(R.id.back);
la = (LinearLayout)findViewById(R.id.la);
lb = (LinearLayout)findViewById(R.id.lb);
lc = (LinearLayout)findViewById(R.id.lc);
ld = (LinearLayout)findViewById(R.id.ld);
le = (LinearLayout)findViewById(R.id.le);
lf = (LinearLayout)findViewById(R.id.lf);
lg = (LinearLayout)findViewById(R.id.lg);
}
public boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
return ni != null && ni.isConnectedOrConnecting();
}
}
*如果想直接用源码 以上 over ,如果想知道怎么实现的
以下*
代码不是很长 其中还有一些log 和system 都是我在调试过程中留下的 太懒不想删掉了就留下了
来 慢慢分解一下
我看程序喜欢从oncreat()开始
所以我们来一起看一下oncreat里面 怎么走的 然后再看用到了什么方法是什么功能
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();//初始化控件
if(!isNetworkConnected())//检查手机是否有网络
{
Toast toast = Toast.makeText(getApplicationContext(), "宝宝你手机没有网", Toast.LENGTH_LONG);//显示时间较长
toast.setGravity(Gravity.CENTER, 0, 0);// 居中显示
toast.show();
}
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Weather_Thread weather_thread =new Weather_Thread();
Weather_now_Thread weather_now_thread = new Weather_now_Thread();
new Thread(weather_thread).start();
new Thread(weather_now_thread).start();
handler = new Myhandler();
handler_now = new Myhandler_now();
swipeRefreshLayout.setRefreshing(false);
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
swipeRefreshLayout.setRefreshing(false);
}
}, 10000);
}
});
//以上都是下拉刷新的不需要可以不要这段
Weather_Thread weather_thread =new Weather_Thread();
Weather_now_Thread weather_now_thread = new Weather_now_Thread();
new Thread(weather_thread).start();
new Thread(weather_now_thread).start();
handler = new Myhandler();
handler_now = new Myhandler_now();
//以上是两个线程的开启 一个是实时天气一个是未来天气
// 一开始就可以查询数据显示
//下面这个是点击切换城市然后的点击事件 线程的那些和一开始一样只不过又更新了一次
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Weather_Thread weather_thread =new Weather_Thread();
Weather_now_Thread weather_now_thread = new Weather_now_Thread();
new Thread(weather_thread).start();
new Thread(weather_now_thread).start();
handler = new Myhandler();
handler_now = new Myhandler_now();
}
});
//完啦是不是很简单 其实主要的结构就是这样
初始化 线程 绑定 over!!
可是线程里面的操作是怎么样的呢
下来看一下线程里面的 我们直接看未来天气 因为里面解析了json数组所以来看一下代码
public class Weather_Thread implements Runnable{
public void run(){
String first = "http://api.k780.com:88/?app=weather.future&weaid=";
String city = search_city.getText().toString().trim();
String last="&&appkey=18276&sign=3f3716462bb4ed68bd082761c3602509&format=json";
String Weather=getURLConnection(first+city+last);
//注意上面这个是调用了一个网络访问返回一个String的方法 这个方法也是自己写的 作用就是 把接口传进去 返回这个接口得到的字符串
Message msg1 = new Message();
Bundle bundle1 = new Bundle();
bundle1.putString("Weather", Weather);
msg1.setData(bundle1);
handler.sendMessage(msg1);
//放在异步处理机制的handle里面可以把线程的消息信息什么的传递到主线程 因为我们得到消息之后是要改变UI的所以不能再在子线程中改变必须传递到主线程中这个传递过去的就是那个字符串 因为网络请求是一个耗时的操作 要放在线程中
}
}
传递到主线程里面之后剩下的就是json解析的部分了重点
public class Myhandler extends Handler{
public void handleMessage(Message msg){
String Weather = msg.getData().getString("Weather");
System.out.println("septa"+Weather);
if(!Weather.equals("")){
try {
JSONArray jsonObjs = new JSONObject(Weather).getJSONArray("result");
// int jsona = new JSONObject(Weather).getInt ("success");
// System.out.println("septqqq"+jsona);
for (int i = 0; i < jsonObjs.length(); i++) {
JSONObject jsonOb0 = (JSONObject) jsonObjs.get(i);
if (i == 0) {
day_one.setText(jsonOb0.getString("week"));
image_one.setText(jsonOb0.getString("weather"));
high_one.setText(jsonOb0.getString("temp_high") + "°");
low_one.setText(jsonOb0.getString("temp_low") + "°");
}
if (i == 1) {
day_two.setText(jsonOb0.getString("week"));
image_two.setText(jsonOb0.getString("weather"));
high_two.setText(jsonOb0.getString("temp_high") + "°");
low_two.setText(jsonOb0.getString("temp_low") + "°");
}
if (i == 2) {
day_three.setText(jsonOb0.getString("week"));
image_three.setText(jsonOb0.getString("weather"));
high_three.setText(jsonOb0.getString("temp_high") + "°");
low_three.setText(jsonOb0.getString("temp_low") + "°");
}
if (i == 3) {
day_four.setText(jsonOb0.getString("week"));
image_four.setText(jsonOb0.getString("weather"));
high_four.setText(jsonOb0.getString("temp_high") + "°");
low_four.setText(jsonOb0.getString("temp_low") + "°");
}
if (i == 4) {
day_five.setText(jsonOb0.getString("week"));
image_five.setText(jsonOb0.getString("weather"));
high_five.setText(jsonOb0.getString("temp_high") + "°");
low_five.setText(jsonOb0.getString("temp_low") + "°");
}
if (i == 5) {
day_six.setText(jsonOb0.getString("week"));
image_six.setText(jsonOb0.getString("weather"));
high_six.setText(jsonOb0.getString("temp_high") + "°");
low_six.setText(jsonOb0.getString("temp_low") + "°");
}
if (i == 6) {
day_seven.setText(jsonOb0.getString("week"));
image_seven.setText(jsonOb0.getString("weather"));
high_seven.setText(jsonOb0.getString("temp_high") + "°");
low_seven.setText(jsonOb0.getString("temp_low") + "°");
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
json解析这部分我刚开始学 所以估计说的也不很精准
就是通过他的键得到他的值 这样的一个过程
然后根据json数组的长度 用循环把数组的内容一个个取出来显示
就得到了未来天气
实时天气那部分和未来天气在线程这里的操作是一样的 只是再解析的时候比解析数组要简单一些 代码都在上面可以仔细看看
最后给出我用的接口
未来天气:
http://api.k780.com:88/?app=weather.future&weaid=1&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json
实时天气
http://api.k780.com:88/?app=weather.today&weaid=1&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json
差不多就这样了 现在这个天气预报就完成了
下来准备把四大组件好好看一下基础
再重复一下四大组件
一、Activity
二、Service
三、Broadcast Receiver
四、Content Provider
学了肯定再来贴 啊啊啊啊啊啊啊啊啊啊啊学习好难
可是难了才有成就感 加油啊么么哒
肯定会越来越好的
标签:
原文地址:http://blog.csdn.net/katherine_qj/article/details/51191927