本文整理匯總了Java中retrofit2.converter.gson.GsonConverterFactory類的典型用法代碼示例。如果您正苦於以下問題:Java GsonConverterFactory類的具體用法?Java GsonConverterFactory怎麽用?Java GsonConverterFactory使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
GsonConverterFactory類屬於retrofit2.converter.gson包,在下文中一共展示了GsonConverterFactory類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: retrofit
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
public static Retrofit retrofit() {
if (mRetrofit == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
// Log信息攔截器
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//設置 Debug Log 模式
builder.addInterceptor(loggingInterceptor);
}
OkHttpClient okHttpClient = builder.build();
mRetrofit = new Retrofit.Builder()
.baseUrl(ApiStores2.URL_BASE)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(okHttpClient)
.build();
}
return mRetrofit;
}
示例2: NetWorkManager
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
private NetWorkManager() {
this.mOkHttpClient = RetrofitUrlManager.getInstance().with(new OkHttpClient.Builder()) //RetrofitUrlManager 初始化
.readTimeout(5, TimeUnit.SECONDS)
.connectTimeout(5, TimeUnit.SECONDS)
.build();
this.mRetrofit = new Retrofit.Builder()
.baseUrl(APP_DEFAULT_DOMAIN)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//使用rxjava
.addConverterFactory(GsonConverterFactory.create())//使用Gson
.client(mOkHttpClient)
.build();
this.mOneApiService = mRetrofit.create(OneApiService.class);
this.mTwoApiService = mRetrofit.create(TwoApiService.class);
this.mThreeApiService = mRetrofit.create(ThreeApiService.class);
}
示例3: apiManager
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
public static ChatDemoApi apiManager(){
if(chatDemoApi == null){
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(logging)
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.build();
Retrofit client = new Retrofit.Builder()
.baseUrl(EndPointConstant.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
chatDemoApi = client.create(ChatDemoApi.class);
}
return chatDemoApi;
}
示例4: getRetrofit
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
public static RetrofitInterface getRetrofit(String token) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder builder = original.newBuilder()
.addHeader("x-access-token", token)
.method(original.method(),original.body());
return chain.proceed(builder.build());
});
RxJavaCallAdapterFactory rxAdapter = RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io());
return new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.client(httpClient.build())
.addCallAdapterFactory(rxAdapter)
.addConverterFactory(GsonConverterFactory.create())
.build().create(RetrofitInterface.class);
}
示例5: Api
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
private Api() {
HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
File cacheFile = new File(App.getAppContext().getCacheDir(), "cache");
Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); //100Mb
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(7676, TimeUnit.MILLISECONDS)
.connectTimeout(7676, TimeUnit.MILLISECONDS)
.addInterceptor(headInterceptor)
.addInterceptor(logInterceptor)
.addNetworkInterceptor(new HttpCacheInterceptor())
.cache(cache)
.build();
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").serializeNulls().create();
retrofit = new Retrofit.Builder()
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(C.BASE_URL)
.build();
service = retrofit.create(ApiService.class);
}
示例6: creatService
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
/**
* 創建ApiService實例
*
* @param service Service類型
* @param <T>
* @return
*/
private static <T> T creatService(final Class<T> service) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(AppData.LOG_DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(CONNECT_TIME_OUT_SECONDS, TimeUnit.SECONDS)
.readTimeout(READ_TIME_OUT_SECONDS, TimeUnit.SECONDS)
.addInterceptor(new LoggingInterceptor())
.addInterceptor(new ParamsInterceptord())
.build();
return new Retrofit.Builder()
.baseUrl(AppData.BaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build()
.create(service);
}
示例7: RemoteHelper
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
private RemoteHelper(){
mOkHttpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(
new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
//在這裏獲取到request後就可以做任何事情了
Response response = chain.proceed(request);
Log.d(TAG, "intercept: "+request.url().toString());
return response;
}
}
).build();
mRetrofit = new Retrofit.Builder()
.client(mOkHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(Constant.API_BASE_URL)
.build();
}
示例8: onCustomApiCall
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
public void onCustomApiCall(View view) {
final String name = "18688994275";
final String pass = "123456";
final CustomRequest request = EasyHttp.custom()
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.sign(true)
.timeStamp(true)
.params(ComParamContact.Login.ACCOUNT, name)
.params(ComParamContact.Login.PASSWORD, MD5.encrypt4login(pass, AppConstant.APP_SECRET))
.build();
LoginService mLoginService = request.create(LoginService.class);
Observable<AuthModel> observable = request.apiCall(mLoginService.login("v1/account/login", request.getParams().urlParamsMap));
Disposable disposable = observable.subscribe(new Consumer<AuthModel>() {
@Override
public void accept(@NonNull AuthModel authModel) throws Exception {
showToast(authModel.toString());
}
}, new Consumer<Throwable>() {
@Override
public void accept(@NonNull Throwable throwable) throws Exception {
showToast(throwable.getMessage());
}
});
//EasyHttp.cancelSubscription(disposable);//取消訂閱
}
示例9: getProtocore
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
default ProtoCore getProtocore() {
Gson gson = NetworkUtilities.getGson();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(getSatispayContext().getBaseUrl())
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(
getProtocoreHttpClientProvider().getProtocoreClient(
getSatispayContext(),
getSecurePersistenceManager(),
getSessionManager(),
getSdkDeviceInfo()
)
)
.build();
return retrofit.create(ProtoCore.class);
}
示例10: create
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
private static Retrofit create() {
OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
builder.readTimeout(10, TimeUnit.SECONDS);
builder.connectTimeout(9, TimeUnit.SECONDS);
builder.addNetworkInterceptor(new StethoInterceptor());
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
}
return new Retrofit.Builder().baseUrl(ENDPOINT)
.client(builder.build())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
示例11: retrofit
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
@Provides Retrofit retrofit() {
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
return chain.proceed(
chain.request()
.newBuilder()
.addHeader("Authorization", "Client-ID " + ImgurService.CLIENT_ID)
.build());
}
})
.build();
return new Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl("https://api.imgur.com/3/")
.build();
}
示例12: main
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
public static void main(String... args) throws IOException, InterruptedException {
MockWebServer server = new MockWebServer();
server.start();
server.enqueue(new MockResponse());
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(new JsonStringConverterFactory(GsonConverterFactory.create()))
.build();
Service service = retrofit.create(Service.class);
Call<ResponseBody> call = service.example(new Filter("123"));
Response<ResponseBody> response = call.execute();
// TODO handle user response...
// Print the request path that the server saw to show the JSON query param:
RecordedRequest recordedRequest = server.takeRequest();
System.out.println(recordedRequest.getPath());
server.shutdown();
}
示例13: ApiClient
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
private ApiClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.addInterceptor(new SessionInterceptor())
//.addInterceptor(new GzipRequsetInterceptor())
.build();
OKHttpUtils.cancelCallWithTag(client);
this.retrofit = new Retrofit.Builder()
.baseUrl(API_HOST)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
示例14: beforeEachTest
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
@Before public void beforeEachTest() {
server = new MockWebServer();
NumbersWebService numberAPI =
new Retrofit.Builder()
.baseUrl(server.url("/").toString())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(NumbersWebService.class);
infrastructure = new TriviaInfrastructure(
numberAPI,
new TriviaGenerator(),
new PayloadMapper(),
new PayloadValidator(),
Schedulers.trampoline() // non-concurrent integration on tests
);
}
開發者ID:ubiratansoares,項目名稱:reactive-architectures-playground,代碼行數:21,代碼來源:TriviaInfrastructureTests.java
示例15: setUp
import retrofit2.converter.gson.GsonConverterFactory; //導入依賴的package包/類
@Before
public void setUp() throws IOException {
mMockServer = new MockWebServer();
mMockServer.start();
Retrofit retrofit = (new Retrofit.Builder().baseUrl(mMockServer.url("").toString())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())).build();
CivifyLoginService civifyLoginService = retrofit.create(CivifyLoginService.class);
CivifyMeService civifyMeService = retrofit.create(CivifyMeService.class);
SharedPreferences.Editor editor = mock(SharedPreferences.Editor.class);
mSharedPreferences = mock(SharedPreferences.class);
mLoginFinishedCallbackMock = mock(LoginFinishedCallback.class);
when(mSharedPreferences.edit()).thenReturn(editor);
mUser = new User("username", "name", "surname",
"email", "password", "password");
mLoginAdapter =
new LoginAdapterImpl(civifyLoginService, civifyMeService, mSharedPreferences);
}