要求
-
天气查询。
-
实现spinner下拉框选择城市,进行查询。
-
显示今天气温,明天气温等多个结果。
-
根据天气显示动态图片(参考:https://urlify.cn/aqmIRj)
准备
在Gradle Scripts下的build.gradle中添加:
dependencies {
implementation 'com.android.volley:volley:1.2.0'
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.15'
}
布局
-
spinner:下拉列表显示
在string.xml中添加:
<resources> <string name="app_name">hbzHttpJsonTest</string> <string name="Spinner_title">请选择城市:</string> <string-array name="city"> <item>北京</item> <item>大连</item> <item>贵州</item> </string-array> </resources>
对spinner的entries进行设置:如这里设置为city。
-
textView:文字显示
-
imageView:图片显示
-
LinearLayout、View:进行动态图片显示
在activity_main.xml中添加:
<LinearLayout android:id="@+id/ll_group" android:layout_width="274dp" android:layout_height="196dp" android:orientation="horizontal" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@+id/guideline"> <pl.droidsonroids.gif.GifImageView android:id="@+id/gv_error" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/gif"/> </LinearLayout>
代码
新建xml文件夹,在该文件夹里再新建一个network_security_config.xml文件,network_security_config.xml中添加代码:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView textView;
RequestQueue requestQueue;//请求队列
String url="http://wthrcdn.etouch.cn/weather_mini?city=";
private String TAG="MainActivity";
int size;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定代码和控制spinner点击事件
ImageView iv=findViewById(R.id.imageView);
Spinner s=findViewById(R.id.spinner);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//当选中某个条目的时候,会执行这段代码
switch (position)
{
case 0:
iv.setImageResource(R.drawable.qing);
// onButtonClick(view);
TQ2(view);
// addGroupImage(size);
break;
case 1:
iv.setImageResource(R.drawable.duoyun);
TQ(view);
break;
case 2:
iv.setImageResource(R.drawable.qing);
TQ3(view);
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
textView =findViewById(R.id.textView);
requestQueue= Volley.newRequestQueue(this);
}
public void onButtonClick(View view){
//控件单击事件
switch (view.getId()){
case R.id.button:
TQ(view);
break;
default:
break;
}
}
//天气代码
public void TQ(View view){
JsonObjectRequest request=new JsonObjectRequest(
url + "大连",
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//网络请求返回数据,执行这里
textView.setText(response.toString());
Log.d(TAG,"onResponse: "+response.toString());
try{
JSONObject o=response.getJSONObject("data");
String city=o.getString("city");
int wendu=o.getInt("wendu");
textView.setText("今天的气温是:"+wendu);
String ganmao=o.getString("ganmao");
JSONArray arr=o.getJSONArray("forecast");
o=arr.getJSONObject(0);
String wendu_h1=o.getString("high");
String wendu_l1=o.getString("low");
String type1=o.getString("type");
o=arr.getJSONObject(1);
String wendu_h2=o.getString("high");
String wendu_l2=o.getString("low");
String type2=o.getString("type");
textView.setText(city+"\n当前温度:"+wendu
+"\n今日:"+type1+" "+wendu_h1+" "+wendu_l1
+"\n明日:"+type2+" "+wendu_h2+" "+wendu_l2
+"\n"+ganmao
);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//如果网络访问错误,执行这里
textView.setText("网络访问错误");
}
}
);
requestQueue.add(request);//添加到请求队列,进行网络访问
}
public void TQ2(View view){
JsonObjectRequest request=new JsonObjectRequest(
url + "北京",
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//网络请求返回数据,执行这里
textView.setText(response.toString());
Log.d(TAG,"onResponse: "+response.toString());
try{
JSONObject o=response.getJSONObject("data");
String city=o.getString("city");
int wendu=o.getInt("wendu");
textView.setText("今天的气温是:"+wendu);
String ganmao=o.getString("ganmao");
JSONArray arr=o.getJSONArray("forecast");
o=arr.getJSONObject(0);
String wendu_h1=o.getString("high");
String wendu_l1=o.getString("low");
String type1=o.getString("type");
o=arr.getJSONObject(1);
String wendu_h2=o.getString("high");
String wendu_l2=o.getString("low");
String type2=o.getString("type");
textView.setText(city+"\n当前温度:"+wendu
+"\n今日:"+type1+" "+wendu_h1+" "+wendu_l1
+"\n明日:"+type2+" "+wendu_h2+" "+wendu_l2
+"\n"+ganmao
);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//如果网络访问错误,执行这里
textView.setText("网络访问错误");
}
}
);
requestQueue.add(request);//添加到请求队列,进行网络访问
}
public void TQ3(View view){
JsonObjectRequest request=new JsonObjectRequest(
url + "贵州",
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//网络请求返回数据,执行这里
textView.setText(response.toString());
Log.d(TAG,"onResponse: "+response.toString());
try{
JSONObject o=response.getJSONObject("data");
String city=o.getString("city");
int wendu=o.getInt("wendu");
textView.setText("今天的气温是:"+wendu);
String ganmao=o.getString("ganmao");
JSONArray arr=o.getJSONArray("forecast");
o=arr.getJSONObject(0);
String wendu_h1=o.getString("high");
String wendu_l1=o.getString("low");
String type1=o.getString("type");
o=arr.getJSONObject(1);
String wendu_h2=o.getString("high");
String wendu_l2=o.getString("low");
String type2=o.getString("type");
textView.setText(city+"\n当前温度:"+wendu
+"\n今日:"+type1+" "+wendu_h1+" "+wendu_l1
+"\n明日:"+type2+" "+wendu_h2+" "+wendu_l2
+"\n"+ganmao
);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//如果网络访问错误,执行这里
textView.setText("网络访问错误");
}
}
);
requestQueue.add(request);//添加到请求队列,进行网络访问
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/city"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClick"
android:text="获取天气"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spinner" />
<TextView
android:id="@+id/textView"
android:layout_width="387dp"
android:layout_height="336dp"
android:text="天气查询结果"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2"
app:layout_constraintVertical_bias="0.025" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="94dp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="135dp"
android:layout_height="95dp"
android:layout_marginTop="4dp"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.992"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline"
app:srcCompat="@drawable/qing" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="316dp" />
<LinearLayout
android:id="@+id/ll_group"
android:layout_width="274dp"
android:layout_height="196dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline">
<pl.droidsonroids.gif.GifImageView
android:id="@+id/gv_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/gif"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.neusoft.hbz.hbzhttpjsontest">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.HbzHttpJsonTest">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
结果显示: