dependencies { implementation 'com.squareup.okhttp3:okhttp:4.9.1' } OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://api.example.com/data") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String responseData = response.body().string(); } }); dependencies { implementation 'com.android.volley:volley:1.2.0' } RequestQueue queue = Volley.newRequestQueue(context); String url = "https://api.example.com/data"; StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); queue.add(stringRequest); dependencies { implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' } public interface ApiService { @GET("data") Call<DataResponse> getData(); } Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.example.com/") .addConverterFactory(GsonConverterFactory.create()) .build(); ApiService apiService = retrofit.create(ApiService.class); Call<DataResponse> call = apiService.getData(); call.enqueue(new Callback<DataResponse>() { @Override public void onResponse(Call<DataResponse> call, Response<DataResponse> response) { } @Override public void onFailure(Call<DataResponse> call, Throwable t) { } });


上一篇:
下一篇:
切换中文