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);
}