07-02 03:26
Notice
Recent Posts
Recent Comments
07-02 03:26
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

pear

[android] simple GPT retrofit 1 - setting 본문

android

[android] simple GPT retrofit 1 - setting

pearlab 2023. 6. 13. 10:20

OKHttpclient


import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class RetrofitGPT {

    static Retrofit retrofit = null;
    public static OkHttpClient client = null;

    static String MYCODE = "";

    public static Retrofit getClient(String aServer) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        try {

            client = new OkHttpClient.Builder()
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .addInterceptor(interceptor)
                    .addInterceptor(new Interceptor() {

                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            // TODO Auto-generated method stub
                            String token = "Bearer " + MYCODE;
                            Request request = chain.request().newBuilder()
                                    .addHeader("Authorization", token)
                                    .build();
                            return chain.proceed(request);
                        }
                    }).build();

            retrofit = new Retrofit.Builder().baseUrl(aServer)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client).build();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return retrofit;
    }
}

Rest API Interface

import com.google.gson.JsonObject;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public interface GPTRetrofitInterface {
   @Headers("Content-Type: application/json")
   @POST("completions ")
   Call<gptResponse> completions(@Body JsonObject sendJSONObject);
}
 

'android' 카테고리의 다른 글

[android] simple GPT retrofit 3 - call  (0) 2023.06.13
[android] simple GPT retrofit 2 - data  (0) 2023.06.13
[AudioRecord] API  (0) 2022.08.09
[policy] 오디오 입력 공유 (from. android 10)  (0) 2022.07.14
[Util]Logfilter  (0) 2022.07.12