实验二 安卓网络编程
一、目的要求
1、理解安卓应用开发中调用web服务的过程和方法。
2、学习在应用开发中使用第三方开发包的过程和方法。
3、掌握json数据的解析方法。
二、实验环境
1、硬件配置:Intel Core i5及以上CPU、内存8G、固态硬盘容量200G以上。
2、软件环境:windows 7及后续版本;JDK 8;Android Studio 3.x及以上版本。
三、实验内容
1、了解下列天气预报相关web服务的有关知识:
https://wis.qq.com/weather/common?source=pc&weather_type=observe&province=江苏省&city=南京市
2、在安卓应用中,使用此web服务获取天气预报相关信息
3、获取到的天气信息请在界面中显示出来
四、实验步骤
定义一个MainActivity类,继承自AppCompatActivity,实现了一个内部类FetchWeatherTask继承自AsyncTask。
在MainActivity的onCreate方法中,首先设置界面布局为activity_main,然后通过findViewById获取了几个TextView控件的引用,并实例化一个FetchWeatherTask并调用execute方法。
FetchWeatherTask类重写了AsyncTask的doInBackground和onPostExecute方法。在doInBackground方法中,通过URL类获取指定URL的输入流,并使用BufferedReader读取输入流数据保存到StringBuilder中,最终返回字符串形式的响应结果。
在onPostExecute方法中,如果接收到了响应结果,则将JSON格式的数据解析为对应的天气信息,然后将这些信息显示在TextView控件上。如果没有接收到结果,则输出日志“No result received from server”。
五、实验结果与分析
- 实验总结
本次实验让我深刻体会到了网络编程在安卓应用开发中的重要性。通过实践,我不仅掌握了基本的网络编程技能,还学会了如何根据实际需求选择合适的工具和方法。同时,我也意识到了数据交换格式(如JSON和XML)在接口通信中的重要性。
在未来的学习中,我将继续深入研究安卓网络编程的相关技术,探索更高效、更安全的编程方法。同时,我也将关注新技术的发展,不断提升自己的编程能力。
七、思考题
1、应用之间的接口数据除了采用json描述以外,主要还有哪种方式?可以借助什么工具进行解析?
JSON解析:
Java:可以使用内置的org.json库或第三方库如Jackson、Gson等。
Python:可以使用内置的json模块。
JavaScript:可以使用JSON.parse()和JSON.stringify()方法。
其他语言:大多数现代编程语言都有相应的JSON解析库或模块。
XML解析:
Java:可以使用内置的javax.xml.parsers包或第三方库如DOM4J、JDOM等。
Python:可以使用内置的xml.etree.ElementTree模块或第三方库如lxml。
JavaScript:可以使用DOMParser对象或第三方库如xml2js。
2、安卓应用开发中进行HTTP编程时,主要会使用哪两类jar包,使用方式有何区别?
在安卓应用开发中,进行HTTP编程时,主要会使用的两类jar包是HttpURLConnection和Apache HttpClient。这两者的使用方式和区别如下:
HttpURLConnection:
使用方式:HttpURLConnection是Java的标准类,继承自HttpConnection。使用它进行HTTP编程通常涉及以下步骤:
准备一个URL对象。
打开一个连接通道。
设置连接超时时间。
设置请求方式(如GET或POST)。
发送请求并获取响应码。
判断响应码,如果等于200说明请求成功。
获取服务端返回的数据流。
特点:HttpURLConnection是Java的标准类,什么都没封装,使用起来较为原始,对于重访问的自定义以及一些高级功能等,可能需要更多的代码和配置。
Apache HttpClient:
使用方式:Apache HttpClient是一个功能丰富的开源框架,封装了访问HTTP的请求头、参数、内容体、响应等。使用它进行HTTP编程通常更加简洁和高效。
特点:Apache HttpClient提供了更丰富的功能和更灵活的配置选项,支持HTTP协议最新的版本和建议。它简化了HTTP请求和响应的处理,使得开发者能够更快速地开发出功能强大的HTTP程序。
代码如下:
一、java代码
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private TextView tvTemperature;
private TextView tvHumidity;
private TextView tvPrecipitation;
private TextView tvPressure;
private TextView tvWeather;
private TextView tvWind;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTemperature = findViewById(R.id.tv_temperature);
tvHumidity = findViewById(R.id.tv_humidity);
tvPrecipitation = findViewById(R.id.tv_precipitation);
tvPressure = findViewById(R.id.tv_pressure);
tvWeather = findViewById(R.id.tv_weather);
tvWind = findViewById(R.id.tv_wind);
new FetchWeatherTask().execute();
}
private class FetchWeatherTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL("https://wis.qq.com/weather/common?source=pc&weather_type=observe&province=江苏省&city=南京市");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
return response.toString();
} catch (IOException e) {
Log.e(TAG, "Error fetching weather data: " + e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
try {
JSONObject jsonObject = new JSONObject(result);
JSONObject dataObject = jsonObject.getJSONObject("data").getJSONObject("observe");
String temperature = dataObject.getString("degree");
String humidity = dataObject.getString("humidity");
String precipitation = dataObject.getString("precipitation");
String pressure = dataObject.getString("pressure");
String weather = dataObject.getString("weather");
String windDirection = dataObject.getString("wind_direction_name");
String windPower = dataObject.getString("wind_power");
tvTemperature.setText("Temperature: " + temperature + "°C");
tvHumidity.setText("Humidity: " + humidity + "%");
tvPrecipitation.setText("Precipitation: " + precipitation + "mm");
tvPressure.setText("Pressure: " + pressure + "hPa");
tvWeather.setText("Weather: " + weather);
tvWind.setText("Wind: " + windDirection + " " + windPower);
} catch (JSONException e) {
Log.e(TAG, "Error parsing weather data: " + e.getMessage());
}
} else {
Log.e(TAG, "No result received from server");
}
}
}
}
二、布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Temperature:"
android:textSize="16sp"
android:layout_marginBottom="8dp"/>
<TextView
android:id="@+id/tv_humidity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Humidity:"
android:textSize="16sp"
android:layout_below="@id/tv_temperature"
android:layout_marginBottom="8dp"/>
<TextView
android:id="@+id/tv_precipitation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Precipitation:"
android:textSize="16sp"
android:layout_below="@id/tv_humidity"
android:layout_marginBottom="8dp"/>
<TextView
android:id="@+id/tv_pressure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pressure:"
android:textSize="16sp"
android:layout_below="@id/tv_precipitation"
android:layout_marginBottom="8dp"/>
<TextView
android:id="@+id/tv_weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weather:"
android:textSize="16sp"
android:layout_below="@id/tv_pressure"
android:layout_marginBottom="8dp"/>
<TextView
android:id="@+id/tv_wind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wind:"
android:textSize="16sp"
android:layout_below="@id/tv_weather"
android:layout_marginBottom="8dp"/>
</RelativeLayout>
三、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.21250208bwtsy02"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>