1.可查询城市天气
1.xml布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/et"
android:hint="输入查询天气"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
2.在https://www.juhe.cn/申请数据和key值
JSON返回示例:
{
"resultcode": "200",
"reason": "查询成功!",
"result": {
"sk": { /*当前实况天气*/
"temp": "21", /*当前温度*/
"wind_direction": "西风", /*当前风向*/
"wind_strength": "2级", /*当前风力*/
"humidity": "4%", /*当前湿度*/
"time": "14:25" /*更新时间*/
},
"today": {
"city": "天津",
"date_y": "2014年03月21日",
"week": "星期五",
"temperature": "8℃~20℃", /*今日温度*/
"weather": "晴转霾", /*今日天气*/
"weather_id": { /*天气唯一标识*/
"fa": "00", /*天气标识00:晴*/
"fb": "53" /*天气标识53:霾 如果fa不等于fb,说明是组合天气*/
},
"wind": "西南风微风",
"dressing_index": "较冷", /*穿衣指数*/
"dressing_advice": "建议着大衣、呢外套加毛衣、卫衣等服装。", /*穿衣建议*/
"uv_index": "中等", /*紫外线强度*/
"comfort_index": "",/*舒适度指数*/
"wash_index": "较适宜", /*洗车指数*/
"travel_index": "适宜", /*旅游指数*/
"exercise_index": "较适宜", /*晨练指数*/
"drying_index": ""/*干燥指数*/
},
"future": [ /*未来几天天气*/
{
"temperature": "28℃~36℃",
"weather": "晴转多云",
"weather_id": {
"fa": "00",
"fb": "01"
},
"wind": "南风3-4级",
"week": "星期一",
"date": "20140804"
},
{
"temperature": "28℃~36℃",
"weather": "晴转多云",
"weather_id": {
"fa": "00",
"fb": "01"
},
"wind": "东南风3-4级",
"week": "星期二",
"date": "20140805"
},
{
"temperature": "27℃~35℃",
"weather": "晴转多云",
"weather_id": {
"fa": "00",
"fb": "01"
},
"wind": "东南风3-4级",
"week": "星期三",
"date": "20140806"
},
{
"temperature": "27℃~34℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "东南风3-4级",
"week": "星期四",
"date": "20140807"
},
{
"temperature": "27℃~33℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "东北风4-5级",
"week": "星期五",
"date": "20140808"
},
{
"temperature": "26℃~33℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "北风4-5级",
"week": "星期六",
"date": "20140809"
},
{
"temperature": "26℃~33℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "北风4-5级",
"week": "星期日",
"date": "20140810"
}
]
},
"error_code": 0
}
3.java代码
1.实体类
public class WeatherBean {
/**
* resultcode : 200
* reason : successed!
* result : {"sk":{"temp":"31","wind_direction":"西南风","wind_strength":"3级","humidity":"59%","time":"11:54"},"today":{"temperature":"25℃~32℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"持续无风向微风","week":"星期二","city":"中山","date_y":"2018年05月15日","dressing_index":"炎热","dressing_advice":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。","uv_index":"中等","comfort_index":"","wash_index":"较适宜","travel_index":"适宜","exercise_index":"适宜","drying_index":""},"future":[{"temperature":"25℃~32℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"持续无风向微风","week":"星期二","date":"20180515"},{"temperature":"25℃~31℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"持续无风向微风","week":"星期三","date":"20180516"},{"temperature":"25℃~31℃","weather":"雷阵雨转多云","weather_id":{"fa":"04","fb":"01"},"wind":"持续无风向微风","week":"星期四","date":"20180517"},{"temperature":"25℃~32℃","weather":"阵雨","weather_id":{"fa":"03","fb":"03"},"wind":"持续无风向微风","week":"星期五","date":"20180518"},{"temperature":"25℃~31℃","weather":"阵雨","weather_id":{"fa":"03","fb":"03"},"wind":"持续无风向微风","week":"星期六","date":"20180519"},{"temperature":"25℃~31℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"持续无风向微风","week":"星期日","date":"20180520"},{"temperature":"25℃~31℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"持续无风向微风","week":"星期一","date":"20180521"}]}
* error_code : 0
*/
private String resultcode;
private String reason;
private ResultBean result;
private int error_code;
public String getResultcode() {
return resultcode;
}
public void setResultcode(String resultcode) {
this.resultcode = resultcode;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public int getError_code() {
return error_code;
}
public void setError_code(int error_code) {
this.error_code = error_code;
}
public static class ResultBean {
/**
* sk : {"temp":"31","wind_direction":"西南风","wind_strength":"3级","humidity":"59%","time":"11:54"}
* today : {"temperature":"25℃~32℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"持续无风向微风","week":"星期二","city":"中山","date_y":"2018年05月15日","dressing_index":"炎热","dressing_advice":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。","uv_index":"中等","comfort_index":"","wash_index":"较适宜","travel_index":"适宜","exercise_index":"适宜","drying_index":""}
* future : [{"temperature":"25℃~32℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"持续无风向微风","week":"星期二","date":"20180515"},{"temperature":"25℃~31℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"持续无风向微风","week":"星期三","date":"20180516"},{"temperature":"25℃~31℃","weather":"雷阵雨转多云","weather_id":{"fa":"04","fb":"01"},"wind":"持续无风向微风","week":"星期四","date":"20180517"},{"temperature":"25℃~32℃","weather":"阵雨","weather_id":{"fa":"03","fb":"03"},"wind":"持续无风向微风","week":"星期五","date":"20180518"},{"temperature":"25℃~31℃","weather":"阵雨","weather_id":{"fa":"03","fb":"03"},"wind":"持续无风向微风","week":"星期六","date":"20180519"},{"temperature":"25℃~31℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"持续无风向微风","week":"星期日","date":"20180520"},{"temperature":"25℃~31℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"持续无风向微风","week":"星期一","date":"20180521"}]
*/
private SkBean sk;
private TodayBean today;
private List<FutureBean> future;
public SkBean getSk() {
return sk;
}
public void setSk(SkBean sk) {
this.sk = sk;
}
public TodayBean getToday() {
return today;
}
public void setToday(TodayBean today) {
this.today = today;
}
public List<FutureBean> getFuture() {
return future;
}
public void setFuture(List<FutureBean> future) {
this.future = future;
}
public static class SkBean {
/**
* temp : 31
* wind_direction : 西南风
* wind_strength : 3级
* humidity : 59%
* time : 11:54
*/
private String temp;
private String wind_direction;
private String wind_strength;
private String humidity;
private String time;
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getWind_direction() {
return wind_direction;
}
public void setWind_direction(String wind_direction) {
this.wind_direction = wind_direction;
}
public String getWind_strength() {
return wind_strength;
}
public void setWind_strength(String wind_strength) {
this.wind_strength = wind_strength;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
public static class TodayBean {
/**
* temperature : 25℃~32℃
* weather : 多云
* weather_id : {"fa":"01","fb":"01"}
* wind : 持续无风向微风
* week : 星期二
* city : 中山
* date_y : 2018年05月15日
* dressing_index : 炎热
* dressing_advice : 天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。
* uv_index : 中等
* comfort_index :
* wash_index : 较适宜
* travel_index : 适宜
* exercise_index : 适宜
* drying_index :
*/
private String temperature;
private String weather;
private WeatherIdBean weather_id;
private String wind;
private String week;
private String city;
private String date_y;
private String dressing_index;
private String dressing_advice;
private String uv_index;
private String comfort_index;
private String wash_index;
private String travel_index;
private String exercise_index;
private String drying_index;
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public WeatherIdBean getWeather_id() {
return weather_id;
}
public void setWeather_id(WeatherIdBean weather_id) {
this.weather_id = weather_id;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDate_y() {
return date_y;
}
public void setDate_y(String date_y) {
this.date_y = date_y;
}
public String getDressing_index() {
return dressing_index;
}
public void setDressing_index(String dressing_index) {
this.dressing_index = dressing_index;
}
public String getDressing_advice() {
return dressing_advice;
}
public void setDressing_advice(String dressing_advice) {
this.dressing_advice = dressing_advice;
}
public String getUv_index() {
return uv_index;
}
public void setUv_index(String uv_index) {
this.uv_index = uv_index;
}
public String getComfort_index() {
return comfort_index;
}
public void setComfort_index(String comfort_index) {
this.comfort_index = comfort_index;
}
public String getWash_index() {
return wash_index;
}
public void setWash_index(String wash_index) {
this.wash_index = wash_index;
}
public String getTravel_index() {
return travel_index;
}
public void setTravel_index(String travel_index) {
this.travel_index = travel_index;
}
public String getExercise_index() {
return exercise_index;
}
public void setExercise_index(String exercise_index) {
this.exercise_index = exercise_index;
}
public String getDrying_index() {
return drying_index;
}
public void setDrying_index(String drying_index) {
this.drying_index = drying_index;
}
public static class WeatherIdBean {
/**
* fa : 01
* fb : 01
*/
private String fa;
private String fb;
public String getFa() {
return fa;
}
public void setFa(String fa) {
this.fa = fa;
}
public String getFb() {
return fb;
}
public void setFb(String fb) {
this.fb = fb;
}
}
}
public static class FutureBean {
/**
* temperature : 25℃~32℃
* weather : 多云
* weather_id : {"fa":"01","fb":"01"}
* wind : 持续无风向微风
* week : 星期二
* date : 20180515
*/
private String temperature;
private String weather;
private WeatherIdBeanX weather_id;
private String wind;
private String week;
private String date;
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public WeatherIdBeanX getWeather_id() {
return weather_id;
}
public void setWeather_id(WeatherIdBeanX weather_id) {
this.weather_id = weather_id;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public static class WeatherIdBeanX {
/**
* fa : 01
* fb : 01
*/
private String fa;
private String fb;
public String getFa() {
return fa;
}
public void setFa(String fa) {
this.fa = fa;
}
public String getFb() {
return fb;
}
public void setFb(String fb) {
this.fb = fb;
}
}
}
}
}
2.Gsonuti工具
package com.weatherforecast.util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.List;
import java.util.Map;
/**
* Created by yinwei on 2015-09-24.
*/
public class GsonUtil {
private static Gson gson = null;
static {
if (gson == null) {
gson = new Gson();
}
}
private GsonUtil() {
}
/**
* 转成json
*
* @param object
* @return
*/
public static String GsonString(Object object) {
String gsonString = null;
if (gson != null) {
gsonString = gson.toJson(object);
}
return gsonString;
}
/**
* 转成bean
*
* @param gsonString
* @param cls
* @return
*/
public static <T> T GsonToBean(String gsonString, Class<T> cls) {
T t = null;
if (gson != null) {
t = gson.fromJson(gsonString, cls);
}
return t;
}
/**
* 转成list
*
* @param gsonString
* @param cls
* @return
*/
public static <T> List<T> GsonToList(String gsonString, Class<T> cls) {
List<T> list = null;
if (gson != null) {
list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
}.getType());
}
return list;
}
/**
* 转成list中有map的
*
* @param gsonString
* @return
*/
public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
List<Map<String, T>> list = null;
if (gson != null) {
list = gson.fromJson(gsonString,
new TypeToken<List<Map<String, T>>>() {
}.getType());
}
return list;
}
/**
* 转成map的
*
* @param gsonString
* @return
*/
public static <T> Map<String, T> GsonToMaps(String gsonString) {
Map<String, T> map = null;
if (gson != null) {
map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
}.getType());
}
return map;
}
}
3.application代码 一定要在mainfest中注册!!!
package com.weatherforecast.application;
import android.app.Application;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
/**
* Created by Administrator on 2018/5/15.
*/
public class MyApplication extends Application{
private static MyApplication myApplication;
private static RequestQueue queues;
@Override
public void onCreate() {
super.onCreate();
myApplication=this;
queues= Volley.newRequestQueue(getApplicationContext());
}
public static synchronized MyApplication getInstance(){
return myApplication;
}
public static RequestQueue getHttpQueues(){
return queues;
}
}
4.main代码
package com.weatherforecast.main;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.weatherforecast.R;
import com.weatherforecast.application.MyApplication;
import com.weatherforecast.bean.WeatherBean;
import com.weatherforecast.util.GsonUtil;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private EditText edittext;
private Button button;
private TextView textView;
private WeatherBean weatherBean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
edittext = (EditText) findViewById(R.id.et);
button = (Button) findViewById(R.id.btn);
textView = (TextView) findViewById(R.id.tv);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String et=edittext.getText().toString().trim();
// textView.setText(et);
getWeather(et);
}
});
}
private void getWeather(String et) {
String url="http://v.juhe.cn/weather/index?format=2&cityname="+et+"&key您申请的KEY";
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
//请求成功
weatherBean= GsonUtil.GsonToBean(jsonObject.toString(), WeatherBean.class);
String city=weatherBean.getResult().getToday().getCity(); //城市
String wind=weatherBean.getResult().getToday().getWind(); //风向
String tem=weatherBean.getResult().getToday().getTemperature(); //温度
String dressing_advice=weatherBean.getResult().getToday().getDressing_advice();
String dressing_index=weatherBean.getResult().getToday().getDressing_index();
textView.setText("城市:"+city+"\n温度:"+tem+"\n风向:"+wind+"\n穿衣:"+dressing_advice+dressing_index);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//请求失败
textView.setText(volleyError.toString());
}
});
MyApplication.getHttpQueues().add(request);
}
}