网络库添加
1. 使用 OkHttp 库
dependencies { implementation 'com.squareup.okhttp3:okhttp:4.10.0' // 版本可以根据需要调整 }import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MyNetworkClient { public static void fetchData() { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://jsonplaceholder.typicode.com/posts") .build(); client.newCall(request).enqueue(new okhttp3.Callback() { @Override public void onFailure(okhttp3.Call call, IOException e) { // 处理失败的情况 e.printStackTrace(); } @Override public void onResponse(okhttp3.Call call, Response response) throws IOException { if (response.isSuccessful()) { String responseData = response.body().string(); // 处理成功的响应 Log.d("NetworkResponse", responseData); } } }); } }
2. 使用 Retrofit 库
3. 使用 Volley 库
总结:
Last updated