本文整理汇总了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);
}