<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.me.WeatherFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/appintro_bar_color"
android:gravity="center"
android:text="天气"
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
<LinearLayout
android:layout_margin="20dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_margin="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/province_tv"
android:text="省份:"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<EditText
android:id="@+id/province_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/province_tv"
android:hint="请输入要查询的天气的省份" />
</RelativeLayout>
<RelativeLayout
android:layout_margin="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/city_tv"
android:text="城市:"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<EditText
android:id="@+id/city_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/city_tv"
android:hint="请输入该省份的城市" />
</RelativeLayout>
<Button
android:id="@+id/search"
android:text="查询"
android:layout_alignParentRight="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="@+id/city_weather"
android:text="城市的天气"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/weather_info"
android:layout_margin="10dp"
android:orientation="vertical"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="天气:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="湿度:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/shidu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="生活建议:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/suggestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
public class WeatherFragment extends Fragment implements View.OnClickListener {
private String weatherCountry = "http://guolin.tech/api/china/";
private String weatherProvince, weatherCity;
private ArrayList<Province> provinceList;
private ArrayList<City> cityList;
private String weatherUrl = "https://free-api.heweather.net/s6/weather/now?location=%s&key=%s";
private String cityId;
private String userName = "HE2205202149311119";
private String key = "4c0f7dccc80748e49e20d0b99f27c43c";
private EditText mCityEdit;
private Button mSearch;
private TextView mCityWeather;
private TextView mWeather;
private TextView mShidu;
private TextView mProvinceTv;
private EditText mProvinceEdit;
String province, city;
private LinearLayout mWeatherInfo;
private TextView mSuggestion;
private String TAG = "WeatherFragment";
private LocationClient mLocationClient = null;
@Override
public void onDestroy() {
mLocationClient.stop();
super.onDestroy();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyLocationListenner myListener = new MyLocationListenner();
mLocationClient = new LocationClient(getContext());
LocationClientOption option = new LocationClientOption();
option.setIsNeedAddress(true);
option.setAddrType("all");
mLocationClient.setLocOption(option);
mLocationClient.registerLocationListener(myListener);
mLocationClient.start();
}
private class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
try {
String province = location.getProvince();
String city = location.getCity();
mCityEdit.setText(city.substring(0,city.length()-1));
mProvinceEdit.setText(province.substring(0,province.length()-1
));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
HeConfig.init(userName, key);
HeConfig.switchToFreeServerNode();
View root=inflater.inflate(R.layout.fragment_weather, container, false);
initView(
root
);
return root;
}
private void initView(View root) {
mCityEdit = (EditText) root.findViewById(R.id.city_edit);
mSearch = (Button) root.findViewById(R.id.search);
mSearch.setOnClickListener(this);
mCityWeather = (TextView) root.findViewById(R.id.city_weather);
mWeather = (TextView) root.findViewById(R.id.weather);
mShidu = (TextView) root.findViewById(R.id.shidu);
mProvinceTv = (TextView) root.findViewById(R.id.province_tv);
mProvinceEdit = (EditText) root.findViewById(R.id.province_edit);
mWeatherInfo = (LinearLayout) root.findViewById(R.id.weather_info);
mSuggestion = (TextView) root.findViewById(R.id.suggestion);
}
public void onClick(View v) {
System.out.println("111");
switch (v.getId()) {
default:
break;
case R.id.search:
province = mProvinceEdit.getText().toString().trim();
city = mCityEdit.getText().toString().trim();
queryWeather();
break;
}
}
private void queryWeather() {
provinceList = new ArrayList<Province>();
cityList = new ArrayList<City>();
new Thread() {
@Override
public void run() {
try {
URL url = new URL(weatherCountry);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(3000);
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuffer sb = new StringBuffer();
String string;
while ((string = br.readLine()) != null) {
sb.append(string);
}
String result = sb.toString();
Log.d("WeatherFragment", "" + result);
JSONArray provinceArray = new JSONArray(result);
for (int i = 0; i < provinceArray.length(); i++) {
JSONObject provinceInfo = provinceArray.getJSONObject(i);
Province provinceBean = new Province();
Gson gson = new Gson();
provinceBean = gson.fromJson(provinceInfo.toString(), Province.class);
provinceList.add(provinceBean);
}
for (Province pro : provinceList) {
if (pro.getName().equals(province)) {
weatherProvince = weatherCountry + pro.getId() + "/";
}
}
Log.d("WeatherProvince", "" + weatherProvince);
url = new URL(weatherProvince);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(3000);
is = connection.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
StringBuffer sb2 = new StringBuffer();
while ((string = br.readLine()) != null) {
sb2.append(string);
}
String result2 = sb2.toString();
Log.d("MainActivity2", "" + result2);
JSONArray cityArray = new JSONArray(result2);
for (int i = 0; i < cityArray.length(); i++) {
JSONObject cityInfo = cityArray.getJSONObject(i);
City cityBean = new City();
Gson gson = new Gson();
cityBean = gson.fromJson(cityInfo.toString(), City.class);
cityList.add(cityBean);
}
for (City c : cityList) {
if (c.getName().equals(city)) {
weatherCity = weatherProvince + c.getId() + "/";
}
}
Log.d("WeatherCity", ""+weatherCity);
url = new URL(weatherCity);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(3000);
is = connection.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
StringBuffer sb3 = new StringBuffer();
while ((string = br.readLine()) != null) {
sb3.append(string);
}
String result3 = sb3.toString();
Log.d("MainActivity3", "" + result3);
JSONArray jsonArray = new JSONArray(result3);
JSONObject cityIdInfo = jsonArray.getJSONObject(0);
cityId=cityIdInfo.getString("weather_id");
String weatherApi = String.format(weatherUrl, cityId, key);
Log.d("WeatherApi", "" + weatherApi);
queryWeather2();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
public void queryWeather2(){
HeWeather.getWeatherNow(getContext(), cityId, Lang.CHINESE_SIMPLIFIED , Unit.METRIC , new HeWeather.OnResultWeatherNowBeanListener() {
@Override
public void onError(Throwable e) {
Log.i(TAG, "Weather Now onError: ", e);
}
@Override
public void onSuccess(Now dataObject) {
Log.i(TAG, " Weather Now onSuccess: " + new Gson().toJson(dataObject));
if ( Code.OK.getCode().equalsIgnoreCase(dataObject.getStatus()) ){
Basic basic=dataObject.getBasic();
String location=basic.getLocation();
mCityWeather.setText(location+"的天气");
NowBase now = dataObject.getNow();
String tmp=now.getTmp();
String cond_txt=now.getCond_txt();
String wind_dir=now.getWind_dir();
mWeather.setText("当前温度:"+tmp+"℃,"+cond_txt+","+wind_dir);
String hum=now.getHum();
mShidu.setText(hum+"%");
} else {
String status = dataObject.getStatus();
Code code = Code.toEnum(status);
Log.i(TAG, "failed code: " + code);
}
}
});
HeWeather.getWeatherLifeStyle(getContext(),cityId, new HeWeather.OnResultWeatherLifeStyleBeanListener() {
@Override
public void onError(Throwable throwable) {
}
@Override
public void onSuccess(Lifestyle lifestyle) {
List<LifestyleBase> lifestyleBases=lifestyle.getLifestyle();
String shushidu=lifestyleBases.get(0).getBrf();
String shushidu2=lifestyleBases.get(0).getTxt();
String sport=lifestyleBases.get(3).getBrf();
String sport2=lifestyleBases.get(3).getTxt();
String cw=lifestyleBases.get(6).getBrf();
String cw2=lifestyleBases.get(6).getTxt();
System.out.println("sdfsdf");
mSuggestion.setText("舒适度指数:"+shushidu+"\n" +
"舒适度建议:"+shushidu2+"\n" +
"运动指数:"+sport+"\n" +
"运动建议:"+sport2+"\n" +
"洗车指数:"+cw+"\n" +
"洗车建议:"+cw2+"\n");
mCityWeather.setVisibility(View.VISIBLE);
mWeatherInfo.setVisibility(View.VISIBLE);
}
});
}
}
public class Province {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class City {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.video.VideoFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center"
android:orientation="vertical"
android:background="@color/purple_500">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/circleImageView"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/default_head"
app:civ_border_color="#fff"
app:civ_border_width="2dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击登录"
android:textColor="#fff"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"
app:cardElevation="10dp"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/linearLayout_run"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="@drawable/calendar_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="日历" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#ebebeb" />
<LinearLayout
android:id="@+id/linearLayout_star"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="@drawable/constellation_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="星座" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#ebebeb" />
<LinearLayout
android:id="@+id/linearLayout_ty"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="@drawable/scraw_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="涂鸦" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#ebebeb" />
<LinearLayout
android:id="@+id/linearLayout_map"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="@drawable/map_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="地图" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"
app:cardElevation="10dp"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout_weather"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="@drawable/collection_icon" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="天气"
android:layout_marginStart="10dp"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
app:srcCompat="@drawable/iv_right_arrow" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ebebeb"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="@drawable/setting_icon" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="设置"
android:layout_marginStart="10dp"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
app:srcCompat="@drawable/iv_right_arrow" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>