學習的第二個星期,已經看不懂自己打的代碼了,真是一個苦逼的職業。
然後,上課無限摸魚。
先舉一個天氣預報的實例
<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:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.example.myapplication2.WeatherTestActivity">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="50dp">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="請輸入城市名:"/>
<EditText
android:id="@+id/weather_test_et"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<Button
android:id="@+id/weather_test_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="查詢"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
android:text="天氣:"/>
<TextView
android:layout_marginLeft="10dp"
android:id="@+id/weather_test_weather"
android:layout_width="wrap_content"
android:gravity="center"
android:textSize="30sp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
android:text="溫度:"/>
<TextView
android:layout_marginLeft="10dp"
android:id="@+id/weather_test_temp"
android:layout_width="wrap_content"
android:gravity="center"
android:textSize="30sp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
android:text="風力:"/>
<TextView
android:layout_marginLeft="10dp"
android:id="@+id/weather_test_windpower"
android:layout_width="wrap_content"
android:gravity="center"
android:textSize="30sp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
佈局文件就這麼多了。接來下是Activity
public class WeatherTestActivity extends AppCompatActivity {
private Button searchBtn;
private EditText editText;
private TextView weatherTv;
private TextView tempTv;
private TextView windTv;
//在這裏使用的是和風天氣的api,登錄和風天氣的官網,獲得普通用戶挨批,添加key,地址則由輸入的地址決定
private String weatherAPI = "https://free-api.heweather.com/s6/weather/now?key=14134781d3024bf3945b32caeebf29ae&location=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather_test);
bangID();
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//讀取輸入的地址,獲得完整的天氣預報api,傳給子線程讀取輸入流
String api_str = weatherAPI+editText.getText().toString();
//在自定義類中添加構造方法,傳值,啓動線程時傳入api
new WeatherTest(WeatherTestActivity.this,editText,weatherTv,tempTv,windTv).execute(api_str);
}
});
}
private void bangID() {
searchBtn = findViewById(R.id.weather_test_btn);
editText = findViewById(R.id.weather_test_et);
weatherTv = findViewById(R.id.weather_test_weather);
tempTv = findViewById(R.id.weather_test_temp);
windTv = findViewById(R.id.weather_test_windpower);
}
}
//在自定義類中完成Http請求和Json數據的解析
public class WeatherTest extends AsyncTask<String,Integer,String> {
private EditText editText;
private TextView weatherTv;
private TextView tempTv;
private TextView windTv;
private Context context;
//通過構造方法傳值
public WeatherTest (Context context,EditText editText,TextView weatherTv,TextView tempTv,TextView windTv){
this.context = context;
this.editText = editText;
this.weatherTv = weatherTv;
this.tempTv = tempTv;
this.windTv = windTv;
}
@Override
protected String doInBackground(String... strings) {
InputStream inputStream = null;
StringBuffer stringBuffer = null;
try {
//創建URL對象,接收api
URL url = new URL(strings[0]);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
//判斷是否聯網
if(httpURLConnection.getResponseCode()==200){
inputStream = httpURLConnection.getInputStream();
}
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
stringBuffer = new StringBuffer();
String temp = null;
while((temp=bufferedReader.readLine())!=null){
stringBuffer.append(temp);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.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對象,接收字符串
JSONObject jsonObject = new JSONObject(s);
//創建jsonarray對象,接收數組
JSONArray jsonArray = jsonObject.getJSONArray("HeWeather6");
//創建jsonobject對象,接收數組的第一個對象
JSONObject jsonObjectindex = jsonArray.getJSONObject(0);
//創建jsonobject對象,接收對象
JSONObject jsonObjectnow = jsonObjectindex.getJSONObject("now");
//讀取數據
String cond_txt = jsonObjectnow.getString("cond_txt");
String tmp = jsonObjectnow.getString("tmp");
String wind_dir = jsonObjectnow.getString("wind_dir");
String wind_sc = jsonObjectnow.getString("wind_sc");
//設置值
weatherTv.setText(cond_txt);
tempTv.setText(tmp+"℃");
windTv.setText(wind_dir+wind_sc+"級");
} catch (JSONException e) {
e.printStackTrace();
}
}
}