实例天气预报
步骤:
添加网络权限
<uses-permission android:name="android.permission.INTERNET" />
设置布局视图,activity文件中绑定ID,设置按钮的点击事件,将API网址传值过来,设置输入框输入城市
- 创建MyTask类继承AsyncTask,设置三个参数
- 在doInbackground方法中,(找水源)创建URL,请求返回,(开水闸)设置openConnection,(建管道)InputStream,
- 判断网页返回码是否为200正确,如果网络正常且返回数据正常,我们才能创建,否则返回“network_failed”
- (建蓄水池蓄水)InputStreamReader,(水桶盛水)BufferReader
- 设置temp,判断当读取temp是否能读取,当他不为空的时候,提取值,否则直接退出
- 返回stringBuffer.toString(),返回到JSON数据
- 在onPostExcute方法中,判断如果网络返回码错误,提示网络连接失败,否则就正常返回,做JSON解析判断,区分是{}还是【】来定义是object还是array,获得各种风力温度
赋值
视图代码展示:
<EditText
android:id="@+id/city_et"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/search_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天气:"/>
<TextView
android:id="@+id/wind_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="温度:"/>
<TextView
android:id="@+id/temp_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="风力:"/>
<TextView
android:id="@+id/weather_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
activity代码演示:
public class Weather2Activity extends AppCompatActivity {
private EditText cityET;
private TextView weatherTV;
private TextView windTV;
private TextView tempTV;
private Button searchBtn;
private String API="https://free-api.heweather.com/s6/weather/now?key=73faf20c8cdc4935943a9703a9068b4f&location=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wea);
bindID();
searchBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyTask().execute(API+cityET.getText().toString());
}
});
}
class MyTask extends AsyncTask<String,String,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 "network_failed";
}
InputStreamReader reader=new InputStreamReader(inputStream);
BufferedReader bufferedReader=new BufferedReader(reader);
String temp=null;
while ((temp=bufferedReader.readLine())!=null){
stringBuffer.append(temp);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s.equals("network_failed")){
Toast.makeText(Weather2Activity.this,"连接失败",Toast.LENGTH_SHORT).show();
}else {
//JSON解析
try {
JSONObject object=new JSONObject(s);
JSONArray array=object.getJSONArray("HeWeather6");
JSONObject object1=array.getJSONObject(0);
JSONObject newobject=object1.getJSONObject("now");
String wind=newobject.getString("cond_txt");
String weather=newobject.getString("wind_dir")+newobject.getString("wind_sc")+"级";
String tmp=newobject.getString("tmp");
//赋值
weatherTV.setText(weather);
windTV.setText( wind);
tempTV.setText(tmp);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
private void bindID() {
cityET=findViewById(R.id.city_et);
windTV=findViewById(R.id.wind_tv);
tempTV=findViewById(R.id.temp_tv);
weatherTV=findViewById(R.id.weather_tv);
searchBtn=findViewById(R.id.search_btn);
}
}