JSON
JSON是一种轻量级的数据交换格式,有两种数据结构:单条JSON数据和多条JSON数组,其中JSON数组是以一对中括号【】表示,JSON对象是一对花括号{}表示,键-值相对,键必须包裹一对双引号,多个键值对中间用逗号分割。
天气预报实例
思路:运用HttpUrlConnection发送请求,从网络上获取数据(JSON),解析json数据更新UI界面。
效果图展示


代码展示
一、在布局文件中创建出一个EditText,用来输入城市,一个按钮用来查询,三个TextView来显示天气状况。
<EditText
android:id="@+id/weather_edit"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="请输入城市名"/>
<Button
android:id="@+id/weather_btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="查询"/>
<TextView
android:id="@+id/weather_text1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="风向-风力:"
android:textSize="20sp"/>
<TextView
android:id="@+id/weather_text2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="温度:"
android:textSize="20sp"/>
<TextView
android:id="@+id/weather_text3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="天气:"
android:textSize="20sp"/>
二、1、在Activity中绑定ID和设置监听
2、创建内部类继承AsyncTack,定义三种参数,重写dolnBackground方法和onPostExeccute方法。dolnBackground方法中写关于Http请求的,onPostExeccute方法来解析Json数据。
01、在dolnBackground方法中创建URL对象
02、通过URL对象调用openConnection()方法获得HttpURLConnection对象
03、HttpURLConnection对象设置其他连接属性
05、读取输入流,转换成String字符串
public class WeatherActivity extends AppCompatActivity {
private EditText cityET;
private Button button;
private TextView windTV;
private TextView tempTV;
private TextView weatherTV;
private String API="https://free-api.heweather.com/s6/weather-now?key=f4df0dc926f54557b9c94b6c09451b51&location=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
bindID();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name =cityET.getText().toString();
new MeTask().execute(API+name);
}
});
}
private void bindID() {
cityET=findViewById(R.id.weather_edit);
button=findViewById(R.id.weather_btn);
windTV=findViewById(R.id.weather_text1);
tempTV=findViewById(R.id.weather_text2);
weatherTV=findViewById(R.id.weather_text3);
}
class MeTask extends AsyncTask<String,Integer,String>{
@Override
protected String doInBackground(String... strings) {
StringBuffer stringBuffer=new StringBuffer();
try {
URL url=new URL(strings[0]);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
InputStream inputStream=null;
if (connection.getResponseCode()==200){
inputStream=connection.getInputStream();
}else {
return "fail";
}
InputStreamReader reader=new InputStreamReader(inputStream);
BufferedReader bufferedReader=new BufferedReader(reader);
String temp="";
while ((temp=bufferedReader.readLine())!=null){
stringBuffer.append(temp);
}
inputStream.close();
reader.close();
bufferedReader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject object=new JSONObject(s);
JSONArray array=object.getJSONArray("HeWeather6");
JSONObject object1=array.getJSONObject(0);
JSONObject object2=object1.getJSONObject("now");
String temp=object2.getString("tmp");
Stringwind=object2.getString("wind_dir")+object2.getString("wind_sc");
String weather=object2.getString("cond_txt");
weatherTV.setText("天气:"+weather);
windTV.setText("风向-风力:"+wind);
tempTV.setText("温度:"+temp);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}