1、效果展现
2、首先创建一个activity,修改里面的布局文件,添加控件,设置id
<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>
3、在activity中进行绑定控件,设置监听,调用自定义类,启动线程
public class WeatherTestActivity extends AppCompatActivity {
private Button searchBtn;
private EditText editText;
private TextView weatherTv;
private TextView tempTv;
private TextView windTv;
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) {
String api_str = weatherAPI+editText.getText().toString();
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);
}
}
4、在自定义类中完成Http请求和Json数据的解析(获得的json数据放到www.json.cn中,这样结构就清晰明了了),这样项目就完成了,运行一下吧。
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 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 = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("HeWeather6");
JSONObject jsonObjectindex = jsonArray.getJSONObject(0);
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();
}
}
}