Created
July 31, 2025 01:44
-
-
Save shuhrat/95ef74d957c1b717742baa44598bb8e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.example; | |
| import okhttp3.OkHttpClient; | |
| import okhttp3.ResponseBody; | |
| import retrofit2.Call; | |
| import retrofit2.converter.gson.GsonConverterFactory; | |
| import retrofit2.Retrofit; | |
| import retrofit2.http.*; | |
| import java.io.IOException; | |
| import java.util.Map; | |
| import java.util.concurrent.TimeUnit; | |
| public class Main { | |
| public static void main(String[] args) throws IOException { | |
| var baseUrl = "https://teeeee.free.beeceptor.com"; | |
| var okHttpClient = new OkHttpClient.Builder() | |
| .connectTimeout(30, TimeUnit.SECONDS) | |
| .readTimeout(30, TimeUnit.SECONDS) | |
| .writeTimeout(30, TimeUnit.SECONDS) | |
| .build(); | |
| // Создание Retrofit | |
| Retrofit retrofit = new Retrofit.Builder() | |
| .baseUrl(baseUrl) | |
| .client(okHttpClient) | |
| .addConverterFactory(GsonConverterFactory.create()) | |
| .build(); | |
| var client = retrofit.create(HttpMethod.class); | |
| var call = client.get( | |
| "astros.json", | |
| Map.of( | |
| "header", "value" | |
| ), | |
| Map.of( | |
| "query", "value" | |
| )//, | |
| // "some body" | |
| ); | |
| // Execute the request (in real code you'd do this asynchronously) | |
| try { | |
| var apiResponse = call.execute(); | |
| if (apiResponse.isSuccessful()) { | |
| System.out.println("Success: " + apiResponse.body().string()); | |
| } else { | |
| System.out.println("Error: " + apiResponse.code() + " - " + apiResponse.errorBody().string()); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } finally { | |
| okHttpClient.dispatcher().executorService().shutdown(); | |
| okHttpClient.connectionPool().evictAll(); | |
| } | |
| } | |
| public interface HttpMethod { | |
| @GET | |
| Call<ResponseBody> get(@Url String url, | |
| @HeaderMap Map<String, String> headers, | |
| @QueryMap Map<String, String> queries); | |
| @POST | |
| Call<ResponseBody> post(@Url String url, | |
| @HeaderMap Map<String, String> headers, | |
| @QueryMap Map<String, String> queries, | |
| @Body String body); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment