Android网络请求实战:从HttpURLConnection到Retrofit 📅 2026/7/19 3:25:42 1. Android网络请求基础概念在Android开发中网络请求是与服务器交互的核心方式。GET请求作为HTTP协议中最基础的请求方法主要用于从服务器获取数据。与POST请求不同GET请求的参数会直接附加在URL后面这使得它更适合用于查询操作而非数据提交。Android平台提供了多种实现GET请求的方式从最基础的HttpURLConnection到流行的第三方库如Retrofit。选择哪种方式取决于项目复杂度、团队技术栈和维护需求等因素。注意从Android 9.0API级别28开始默认情况下禁止明文HTTP流量。如果必须使用HTTP而非HTTPS需要在AndroidManifest.xml中配置网络安全策略。2. 使用HttpURLConnection实现GET请求2.1 基本实现步骤HttpURLConnection是Java标准库提供的HTTP客户端也是Android中最基础的网络请求实现方式。以下是完整实现代码public class HttpUtils { public static String get(String urlString) throws IOException { URL url new URL(urlString); HttpURLConnection connection (HttpURLConnection) url.openConnection(); try { connection.setRequestMethod(GET); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); int responseCode connection.getResponseCode(); if (responseCode HttpURLConnection.HTTP_OK) { 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(); } else { throw new IOException(HTTP error code: responseCode); } } finally { connection.disconnect(); } } }2.2 关键参数解析setConnectTimeout(5000)设置连接超时为5秒防止网络状况不佳时长时间等待setReadTimeout(5000)设置读取超时为5秒确保服务器响应不及时时能够及时中断HTTP_OK状态码200表示请求成功finally块中的disconnect()确保无论请求成功与否都会释放连接资源2.3 异步处理方案由于Android禁止在主线程执行网络操作必须使用异步任务new AsyncTaskString, Void, String() { Override protected String doInBackground(String... urls) { try { return HttpUtils.get(urls[0]); } catch (IOException e) { return Error: e.getMessage(); } } Override protected void onPostExecute(String result) { // 更新UI } }.execute(https://api.example.com/data);3. 使用OkHttp实现GET请求3.1 OkHttp基础配置OkHttp是Square公司开发的HTTP客户端相比HttpURLConnection提供了更简洁的API和更强大的功能。首先添加依赖implementation com.squareup.okhttp3:okhttp:4.9.3基础GET请求实现OkHttpClient client new OkHttpClient(); Request request new Request.Builder() .url(https://api.example.com/data?id123) .build(); try (Response response client.newCall(request).execute()) { if (response.isSuccessful()) { String responseData response.body().string(); // 处理响应数据 } }3.2 高级特性OkHttp支持许多实用功能拦截器可以添加日志拦截器监控请求/响应client new OkHttpClient.Builder() .addInterceptor(new HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.BODY)) .build();缓存控制配置缓存策略减少网络请求int cacheSize 10 * 1024 * 1024; // 10MB Cache cache new Cache(context.getCacheDir(), cacheSize); client new OkHttpClient.Builder() .cache(cache) .build();超时设置针对不同环节设置超时client new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .build();4. 使用Retrofit实现GET请求4.1 Retrofit基础配置Retrofit是Square开发的类型安全的HTTP客户端基于OkHttp。添加依赖implementation com.squareup.retrofit2:retrofit:2.9.0 implementation com.squareup.retrofit2:converter-gson:2.9.0定义API接口public interface ApiService { GET(data) CallResponseData getData(Query(id) String id); }创建Retrofit实例Retrofit retrofit new Retrofit.Builder() .baseUrl(https://api.example.com/) .addConverterFactory(GsonConverterFactory.create()) .build(); ApiService service retrofit.create(ApiService.class);4.2 请求执行与响应处理同步请求ResponseResponseData response service.getData(123).execute(); if (response.isSuccessful()) { ResponseData data response.body(); // 处理数据 }异步请求service.getData(123).enqueue(new CallbackResponseData() { Override public void onResponse(CallResponseData call, ResponseResponseData response) { if (response.isSuccessful()) { // 更新UI } } Override public void onFailure(CallResponseData call, Throwable t) { // 处理错误 } });4.3 高级URL配置Retrofit支持多种URL配置方式路径参数GET(user/{id}) CallUser getUser(Path(id) String userId);查询参数GET(search) CallSearchResult search(Query(q) String query);多个查询参数GET(search) CallSearchResult search(QueryMap MapString, String filters);5. 网络请求常见问题与优化5.1 性能优化技巧连接复用OkHttp默认启用连接池保持连接活跃状态数据压缩添加Accept-Encoding: gzip头减少传输数据量缓存策略合理配置缓存减少重复请求批量请求合并多个小请求为一个批量请求5.2 常见错误处理SSL证书问题OkHttpClient client new OkHttpClient.Builder() .hostnameVerifier((hostname, session) - true) .sslSocketFactory(createSSLSocketFactory(), createTrustManager()) .build();JSON解析异常使用Gson的TypeAdapter处理特殊数据类型空响应体检查response.body()是否为null线程切换使用Handler或LiveData将结果传回主线程5.3 调试技巧使用StethoFacebook开发的调试工具可以查看网络请求implementation com.facebook.stetho:stetho:1.6.0 implementation com.facebook.stetho:stetho-okhttp3:1.6.0日志拦截器详细记录请求和响应数据模拟API响应使用MockWebServer进行单元测试6. 现代Android网络请求最佳实践6.1 结合协程使用Retrofit支持协程方式调用GET(data) suspend fun getData(Query(id) id: String): ResponseResponseData // 调用 viewModelScope.launch { try { val response apiService.getData(123) if (response.isSuccessful) { // 更新UI } } catch (e: Exception) { // 处理错误 } }6.2 结合Jetpack组件Repository模式public class DataRepository { private ApiService apiService; public LiveDataResultResponseData getData(String id) { MutableLiveDataResultResponseData result new MutableLiveData(); apiService.getData(id).enqueue(new CallbackResponseData() { // 处理响应 }); return result; } }使用WorkManager处理后台请求Constraints constraints new Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED) .build(); OneTimeWorkRequest request new OneTimeWorkRequest.Builder( NetworkWorker.class) .setConstraints(constraints) .build(); WorkManager.getInstance(context).enqueue(request);6.3 安全建议使用HTTPS始终优先使用HTTPS而非HTTP证书锁定配置Certificate Pinning防止中间人攻击敏感数据不要在URL中传递敏感信息权限控制检查android.permission.INTERNET权限在实际项目中我通常会根据项目规模选择网络库。对于小型项目OkHttp已经足够中型项目使用Retrofit可以提高开发效率大型项目则需要结合协程和架构组件构建完整的网络层。无论选择哪种方式良好的错误处理和日志记录都是必不可少的。