編輯:關於Android編程
之前實現過了天氣預報的功能 但是真的好丑 真的只是實現功能 所以上一篇博客也沒有貼出圖片 這次 相對於第一個 首先是界面做了調整 其次就是 之前那個只能查看實時天氣 這個天氣預報我還加入了未來天氣
先 看一下對比圖
因為這是給我楊凌一個閨蜜寫的逗她開心也練練技術 所以中間那部分是自己定義的一些話 當然可以替換成穿衣建議什麼的
不管之前那個天氣預報這個重新來一遍
在你剛剛創建好這個項目的時候 因為需要聯網操作 所以我們需要先導入網絡包和在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之外加上
基本准備工作就完成了這樣在編碼的工程中會減少很多錯誤
現在就開始要入手這個項目的第一個關鍵的部分就是layout
我的布局寫的很爛基本都是最簡單的控件組成的顯示大部分用的TextView
先把代碼貼上來 因為id這個在後面有些不知道的還要查這個代碼
哦 那個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數組的長度 用循環把數組的內容一個個取出來顯示
就得到了未來天氣
實時天氣那部分和未來天氣在線程這裡的操作是一樣的 只是再解析的時候比解析數組要簡單一些 代碼都在上面可以仔細看看
本文實例講述了Android編程滑動效果之Gallery+GridView實現圖片預覽功能。分享給大家供大家參考,具體如下:Android系統自帶一個GridView和G
在前面一篇文章中,我們分析了ART運行時加載類以及查找其方法的過程。一旦找到了目標類方法,我們就可以獲得它的DEX字節碼或者本地機器指令,這樣就可以對它進行執行了。在AR
明天還要加班,苦逼的程序猿,簡單說說最近遇到的一個問題吧。我在viewpager+fragment學習筆記中簡單介紹過ViewPager+Fragment的用法,其實並不
1、View是什麼View是Android所有控件的基類,簡單到TextView、Button,復雜到RelativeLayout,LinearLayout,其共同基類都