目录
第九章:网络通信基础(HttpURLConnection 与 OkHttp 实现数据请求)
🔸 9.2 使用 HttpURLConnection(原生方式)
第九章:网络通信基础(HttpURLConnection 与 OkHttp 实现数据请求)
在现代 App 中,网络通信是不可或缺的功能,比如请求天气数据、获取用户信息、与服务器交互等。
本章主要介绍 Android 中最常见的两种网络请求方式:
-
原生
HttpURLConnection
-
第三方库
OkHttp
🔹 9.1 网络权限配置
在进行任何网络操作前,必须在清单文件中申请权限:
<uses-permission android:name="android.permission.INTERNET" />
🔸 9.2 使用 HttpURLConnection(原生方式)
✅ 步骤说明:
-
创建 URL 对象
-
打开连接,设置请求方式(GET/POST)
-
获取响应流并读取数据
-
关闭连接
✦ 示例:GET 请求(Java)
new Thread(() -> {
try {
URL url = new URL("https://jsonplaceholder.typicode.com/posts/1");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
Log.d("HTTP", "响应结果:" + result.toString());
reader.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}).start(); // 网络操作要在子线程进行
✦ 示例:POST 请求(带参数)
URL url = new URL("https://example.com/login");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream os = conn.getOutputStream();
os.write("username=alice&password=123456".getBytes());
os.flush();
os.close();
🔸 9.3 使用 OkHttp(推荐方式)
OkHttp 官网
优点:
API 简洁易用
自动线程管理
支持异步请求、缓存、拦截器
✅ 添加依赖
在 build.gradle
中添加:
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
✦ 示例:GET 请求
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts/1")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String result = response.body().string();
Log.d("OKHTTP", "响应结果:" + result);
}
}
});
✦ 示例:POST 请求(携带表单参数)
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("username", "alice")
.add("password", "123456")
.build();
Request request = new Request.Builder()
.url("https://example.com/login")
.post(formBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String responseText = response.body().string();
Log.d("OKHTTP", responseText);
}
}
});
📌 网络通信小贴士
注意事项 | 建议 |
---|---|
主线程网络请求 | Android 9+ 已禁止,必须放在子线程 |
解析 JSON 数据 | 推荐使用 JSONObject 或 Gson |
数据加密 | 使用 HTTPS,敏感信息再加密 |
防止泄露 | 不要在日志中打印 token、密码 |
🧪 实战练习建议
-
用 OkHttp 实现一个登录表单功能
-
调用免费 API(如天气、新闻)并展示在页面上
-
练习使用
Handler
或runOnUiThread
更新界面
📢 下一章预告:
第十章:使用 Gson 实现网络 JSON 数据解析与对象映射
习题答案
项目结构
MainActivity.java
LoginActivity.java
WeatherFragment.java
NewsFragment.java
activity_login.xml
activity_main.xml
fragment_weather.xml
fragment_news.xml
item_news.xml
1. LoginActivity.java
package com.example.demo;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class LoginActivity extends AppCompatActivity {
private EditText editTextUsername, editTextPassword;
private Button buttonLogin;
private OkHttpClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);
client = new OkHttpClient();
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
return;
}
// 模拟登录请求
login(username, password);
}
});
}
private void login(String username, String password) {
String url = "https://reqres.in/api/login"; // 免费的模拟登录 API
okhttp3.RequestBody body = new okhttp3.FormBody.Builder()
.add("email", username)
.add("password", password)
.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(() -> Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String responseData = response.body().string();
runOnUiThread(() -> {
Toast.makeText(LoginActivity.this, "登录成功: " + responseData, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
});
} else {
runOnUiThread(() -> Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show());
}
}
});
}
}
2. WeatherFragment.java
package com.example.demo;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONObject;
import java.io.IOException;
public class WeatherFragment extends Fragment {
private TextView textViewWeather;
private OkHttpClient client;
private Handler handler;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_weather, container, false);
textViewWeather = view.findViewById(R.id.textViewWeather);
client = new OkHttpClient();
handler = new Handler();
fetchWeatherData();
return view;
}
private void fetchWeatherData() {
String apiKey = "YOUR_API_KEY"; // 替换为你的 API 密钥
String url = "https://api.openweathermap.org/data/2.5/weather?q=Beijing&appid=" + apiKey;
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
handler.post(() -> textViewWeather.setText("获取天气失败"));
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
try {
String responseData = response.body().string();
JSONObject jsonObject = new JSONObject(responseData);
String weather = jsonObject.getJSONArray("weather").getJSONObject(0).getString("main");
String temperature = jsonObject.getJSONObject("main").getString("temp");
final String result = "天气: " + weather + "\n温度: " + temperature + "K";
handler.post(() -> textViewWeather.setText(result));
} catch (Exception e) {
e.printStackTrace();
handler.post(() -> textViewWeather.setText("解析天气数据失败"));
}
} else {
handler.post(() -> textViewWeather.setText("获取天气失败"));
}
}
});
}
}
3. NewsFragment.java
package com.example.demo;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class NewsFragment extends Fragment {
private RecyclerView recyclerView;
private NewsAdapter adapter;
private List<NewsItem> newsList;
private OkHttpClient client;
private Handler handler;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
newsList = new ArrayList<>();
adapter = new NewsAdapter(newsList);
recyclerView.setAdapter(adapter);
client = new OkHttpClient();
handler = new Handler();
fetchNewsData();
return view;
}
private void fetchNewsData() {
String url = "https://newsapi.org/v2/top-headlines?country=us&apiKey=YOUR_API_KEY"; // 替换为你的 API 密钥
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
handler.post(() -> adapter.updateData(new ArrayList<>()));
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
try {
String responseData = response.body().string();
JSONObject jsonObject = new JSONObject(responseData);
JSONArray articles = jsonObject.getJSONArray("articles");
List<NewsItem> newsItems = new ArrayList<>();
for (int i = 0; i < articles.length(); i++) {
JSONObject article = articles.getJSONObject(i);
String title = article.getString("title");
String description = article.getString("description");
newsItems.add(new NewsItem(title, description));
}
handler.post(() -> adapter.updateData(newsItems));
} catch (Exception e) {
e.printStackTrace();
handler.post(() -> adapter.updateData(new ArrayList<>()));
}
} else {
handler.post(() -> adapter.updateData(new ArrayList<>()));
}
}
});
}
}
4. NewsAdapter.java
package com.example.demo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private List<NewsItem> newsList;
public NewsAdapter(List<NewsItem> newsList) {
this.newsList = newsList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_news, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
NewsItem item = newsList.get(position);
holder.textViewTitle.setText(item.getTitle());
holder.textViewDescription.setText(item.getDescription());
}
@Override
public int getItemCount() {
return newsList.size();
}
public void updateData(List<NewsItem> newData) {
newsList.clear();
newsList.addAll(newData);
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewDescription;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.textViewTitle);
textViewDescription = itemView.findViewById(R.id.textViewDescription);
}
}
}
总结
- 登录表单功能:使用 OkHttp 调用模拟登录 API。
- 天气和新闻 API:分别调用 OpenWeatherMap 和 NewsAPI 获取数据。
- 更新界面:通过
Handler
或runOnUiThread
更新 UI。