Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added SVID_20180321_170127_20180321170943.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SVID_20180323_225239_20180323230757.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mg.studio.weatherappdesign">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
Expand All @@ -21,4 +21,4 @@
</activity>
</application>

</manifest>
</manifest>
181 changes: 177 additions & 4 deletions app/src/main/java/mg/studio/weatherappdesign/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package mg.studio.weatherappdesign;


import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -14,18 +19,92 @@
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MainActivity extends AppCompatActivity {

String[] ADate = new String[]{"MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY","SUNDAY"};
Pattern p = Pattern.compile("location.{3}([\u4E00-\u9FA5]+).*?cond_txt_d.{3}([\u4E00-\u9FA5]+).*?date.{3}(.{10}).*?tmp_max.{3}(\\d+).*" +
"?cond_txt_d.{3}([\u4E00-\u9FA5]+).*?date.{3}(.{10}).*?tmp_max.{3}(\\d+).*" +
"?cond_txt_d.{3}([\u4E00-\u9FA5]+).*?date.{3}(.{10}).*?tmp_max.{3}(\\d+).*");
Calendar c = Calendar.getInstance();
String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
Integer mwee = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void btnClick(View view) {
new DownloadUpdate().execute();
String str = "the function is not designed";
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
}

public void btnRefresh(View view) {
boolean success = false;
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo.State state = connManager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态
if (NetworkInfo.State.CONNECTED == state) { // whether using the WIFI or not
success = true;
}

state = connManager.getNetworkInfo(
ConnectivityManager.TYPE_MOBILE).getState(); // get the MOBILE net-connection state
if (NetworkInfo.State.CONNECTED == state) { // whether using the GPRS or not
success = true;
}
if (success) {
myUploadData();
new DownloadUpdate().execute();
} else {
Toast.makeText(MainActivity.this, "Please check your net-connection", Toast.LENGTH_LONG).show();
}
}

private void myUploadData() {

int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
if ("1".equals(mWay)) {
mWay = "SUNDAY";
mwee = 6;
} else if ("2".equals(mWay)) {
mWay = "MONDAY";
mwee = 0;
} else if ("3".equals(mWay)) {
mWay = "TUESDAY";
mwee = 1;
} else if ("4".equals(mWay)) {
mWay = "WEDNESDAY";
mwee = 2;
} else if ("5".equals(mWay)) {
mWay = "THURSDAY";
mwee = 3;
} else if ("6".equals(mWay)) {
mWay = "FRIDAY";
mwee = 4;
} else if ("7".equals(mWay)) {
mWay = "SATURDAY";
mwee = 5;
}
// String date = null;
// if (day > 9 && month > 9) {
// date = Integer.toString(month) + '/' + Integer.toString(day) + '/' + Integer.toString(year);
// } else if (day < 10 && month > 9) {
// date = Integer.toString(month) + '/' + '0' + Integer.toString(day) + '/' + Integer.toString(year);
// } else if (month < 10 && day > 9) {
// date = '0' + Integer.toString(month) + '/' + Integer.toString(day) + '/' + Integer.toString(year);
// } else {
// date = '0' + Integer.toString(month) + '/' + '0' + Integer.toString(day) + '/' + Integer.toString(year);
// }
((TextView) findViewById(R.id.head_date)).setText(mWay);
// ((TextView) findViewById(R.id.tv_date)).setText(date);

}


Expand All @@ -34,9 +113,16 @@ private class DownloadUpdate extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... strings) {
String stringUrl = "http://mpianatra.com/Courses/info.txt";
String stringUrl = "https://free-api.heweather.com/s6/weather/forecast?";
HttpURLConnection urlConnection = null;
BufferedReader reader;
String location = "location=chongqing";


String key = "key=411e1bd9c04e4d6c80369ab50bcb63ac";

stringUrl = stringUrl
+ location + "&" + key;

try {
URL url = new URL(stringUrl);
Expand Down Expand Up @@ -81,9 +167,96 @@ protected String doInBackground(String... strings) {
}

@Override
/*
get the data from the json file which I download from hefeng weather
*/
protected void onPostExecute(String temperature) {
//Update the temperature displayed
((TextView) findViewById(R.id.temperature_of_the_day)).setText(temperature);
Matcher m = p.matcher(temperature);
m = p.matcher(temperature);
String location = null;
String temprature = null;
String weather = null;
String date = null;
if (m.find()) {
location = m.group(1);
temprature = m.group(4);
weather = m.group(2);
date = m.group(3);

}
ImageView wea = findViewById(R.id.img_weather_condition);
TextView text = findViewById(R.id.temperature_of_the_day);//
myChangeWeather(wea,weather);//change the main image of weather
myChangeText(text,temprature);//change the temperature of today
text = findViewById(R.id.tv_location);
myChangeText(text,location);
text = findViewById(R.id.tv_date);
myChangeText(text,date);

//Change the list below
myChangeList(m);
}
}

private void myChangeList(Matcher m) {
String temp = m.group(2);
ImageView wea = findViewById(R.id.day_one);
myChangeWeather(wea,temp);

temp = ADate[mwee].substring(0,3);
TextView text = findViewById(R.id.text_day_one);
myChangeText(text,temp);

wea = findViewById(R.id.day_two);
temp = m.group(5);
myChangeWeather(wea,temp);

temp = ADate[(mwee+1)%7].substring(0,3);
text = findViewById(R.id.text_day_tow);
myChangeText(text,temp);

temp = m.group(8);
wea = findViewById(R.id.day_thr);
myChangeWeather(wea,temp);

temp = ADate[(mwee+2)%7].substring(0,3);
text = findViewById(R.id.text_day_thr);
myChangeText(text,temp);

temp = ADate[(mwee+3)%7].substring(0,3);
text = findViewById(R.id.text_day_fou);
myChangeText(text,temp);
}

private void myChangeText(TextView viewById, String location) {
viewById.setText(location);
}

private void myChangeWeather(ImageView wea, String weather) {
switch (weather){
case "阴":
wea.setImageResource(R.drawable.windy_small);
break;
case"小雨":
wea.setImageResource(R.drawable.rainy_small);
break;
case "晴":
wea.setImageResource(R.drawable.sunny_small);
break;
case"阵雨":
wea.setImageResource(R.drawable.rainy_small);
break;
case "多云":
wea.setImageResource(R.drawable.partly_sunny_small);
break;
case "晴间多云":
wea.setImageResource(R.drawable.partly_sunny_small);
break;
default:

break;
}
}

}
Binary file added app/src/main/res/drawable-v24/refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/blue_bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 41 additions & 19 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
tools:context="mg.studio.weatherappdesign.MainActivity">

<TextView
android:id="@+id/head_date"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
Expand Down Expand Up @@ -81,10 +82,10 @@

<ImageView
android:id="@+id/img_weather_condition"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_width="43dp"
android:layout_height="52dp"
android:layout_gravity="center"
app:srcCompat="@drawable/rainy_up" />
android:src="@drawable/windy_small" />

<TextView
android:id="@+id/tv_location"
Expand All @@ -99,11 +100,20 @@
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="03/03/2018"
android:text="2018-03-03"
android:textColor="@android:color/white" />

</LinearLayout>

<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorBlue"
android:layout_toRightOf="@id/linearLayout"
android:layout_marginTop="70dp"
android:onClick="btnRefresh"
app:srcCompat="@android:drawable/ic_popup_sync" />
<RelativeLayout
android:id="@+id/tv_temperature"
android:layout_width="match_parent"
Expand Down Expand Up @@ -131,7 +141,10 @@
android:gravity="right"
android:text="°C"
android:textColor="@android:color/white"
android:textSize="22sp" />
android:textSize="22sp"
android:id="@+id/textView" />




</RelativeLayout>
Expand Down Expand Up @@ -171,15 +184,18 @@
android:orientation="vertical">

<ImageView
android:id="@+id/day_one"
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@drawable/rainy_small" />
android:src="@drawable/rainy_small" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_day_one"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center"
android:text="mon"
android:background="@drawable/blue_bg"
android:textAllCaps="true"
android:textColor="#909090" />
</LinearLayout>
Expand All @@ -192,13 +208,15 @@
android:orientation="vertical">

<ImageView
android:id="@+id/day_two"
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@drawable/partly_sunny_small" />
android:src="@drawable/partly_sunny_small" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_day_tow"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center"
android:text="tue"
android:textAllCaps="true"
Expand All @@ -213,15 +231,17 @@
android:orientation="vertical">

<ImageView
android:id="@+id/day_thr"
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@drawable/windy_small" />
android:src="@drawable/windy_small" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_day_thr"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center"
android:text="thu"
android:text="wed"
android:textAllCaps="true"
android:textColor="#909090" />
</LinearLayout>
Expand All @@ -234,15 +254,17 @@
android:orientation="vertical">

<ImageView
android:id="@+id/day_fou"
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@drawable/sunny_small" />
android:src="@drawable/sunny_small" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_day_fou"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center"
android:text="fri"
android:text="thu"
android:textAllCaps="true"
android:textColor="#909090" />
</LinearLayout>
Expand Down
Loading