嗨,我正在尝试使用 retrofit 和 okhttp3 将我的android客户端连接到服务器,但是我在改造时遇到问题,应该添加基本URL,但是当网址具有 SSL(https://),它可以正常工作,但是当我想在没有SSL(http://)的情况下完成确切的工作时,有时会出现超时错误(5 10次),我真的不明白我的后端是一个 Symfony 框架的问题,它在我的手机浏览器和计算机浏览器或邮递员应用程序,这是我的改造客户端:
public class apiClient {
private static Retrofit retrofit = null;
private static final String CACHE_CONTROL = "Cache-Control";
public static Retrofit initClient(){
String text = "";
byte[] data = android.util.Base64.decode(apiClient.retrofit_id, android.util.Base64.DEFAULT);
try {
text = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(text)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
public static void setClient(retrofit2.Response<ApiResponse> response, Activity activity, PrefManager prf){
if (response.isSuccessful()) {
if (response.body().getCode().equals(202)) {
Toasty.error(activity, response.body().getMessage(), Toast.LENGTH_SHORT).show();
SplashActivity.adapteActivity(activity);
} else {
prf.setString("formatted","true");
}
}
}
public static String LoadClientData(Activity activity){
return activity.getApplicationContext().getPackageName();
}
public static void FormatData(final Activity activity,Object o){
try {
final PrefManager prf = new PrefManager(activity.getApplication());
if (!prf.getString("formatted").equals("true")) {
if (apiClient.check(activity)) {
Retrofit retrofit=apiClient.initClient();
apiRest service = retrofit.create(apiRest.class);
Call<ApiResponse> callback = service.setWallpaper(apiClient.LoadClientData(activity));
callback.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(Call<ApiResponse> call, retrofit2.Response<ApiResponse> response) {
apiClient.setClient(response,activity,prf);
}
@Override
public void onFailure(Call<ApiResponse> call, Throwable t) {
}
});
}
}
}catch (Exception e){
if (o!=null){
return;
}else{
}
}
}
public static boolean check(Activity activity){
final PrefManager prf = new PrefManager(activity.getApplication());
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = sdf.format(c.getTime());
if (prf.getString("LAST_DATA_LOAD").equals("")) {
prf.setString("LAST_DATA_LOAD", strDate);
} else {
String toyBornTime = prf.getString("LAST_DATA_LOAD");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date oldDate = dateFormat.parse(toyBornTime);
System.out.println(oldDate);
Date currentDate = new Date();
long diff = currentDate.getTime() - oldDate.getTime();
long seconds = diff / 1000;
if (seconds >15) {
prf.setString("LAST_DATA_LOAD", strDate);
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
}
return false;
}
public static Retrofit getClient() {
if (retrofit==null) {
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.addInterceptor( provideHttpLoggingInterceptor() )
.addInterceptor( provideOfflineCacheInterceptor() )
.addNetworkInterceptor( provideCacheInterceptor() )
.cache( provideCache() )
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
OkHttp3Downloader okHttp3Downloader = new OkHttp3Downloader(okHttpClient);
Picasso picasso = new Picasso.Builder(App.getInstance())
.downloader(okHttp3Downloader)
.build();
Picasso.setSingletonInstance(picasso);
retrofit = new Retrofit.Builder()
.baseUrl(Config.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
private static Cache provideCache ()
{
Cache cache = null;
try
{
cache = new Cache( new File( App.getInstance().getCacheDir(), "wallpaper-cache" ),
10 * 1024 * 1024 ); // 10 MB
}
catch (Exception e)
{
Timber.e( e, "Could not create Cache!" );
}
return cache;
}
private static HttpLoggingInterceptor provideHttpLoggingInterceptor ()
{
HttpLoggingInterceptor httpLoggingInterceptor =
new HttpLoggingInterceptor( new HttpLoggingInterceptor.Logger()
{
@Override
public void log (String message)
{
Timber.d( message );
}
} );
httpLoggingInterceptor.setLevel( BuildConfig.DEBUG ? HEADERS : NONE );
return httpLoggingInterceptor;
}
public static Interceptor provideCacheInterceptor ()
{
return new Interceptor()
{
@Override
public Response intercept (Chain chain) throws IOException
{
Response response = chain.proceed( chain.request() );
// re-write response header to force use of cache
CacheControl cacheControl = new CacheControl.Builder()
.maxAge( 2, TimeUnit.SECONDS )
.build();
return response.newBuilder()
.header( CACHE_CONTROL, cacheControl.toString() )
.build();
}
};
}
public static String retrofit_id = "aHR0cDovL2xpY2Vuc2UucmlzdGFuYS5jb20vYXBpLw==";
public static Interceptor provideOfflineCacheInterceptor ()
{
return new Interceptor()
{
@Override
public Response intercept (Chain chain) throws IOException
{
Request request = chain.request();
if ( !App.hasNetwork() )
{
CacheControl cacheControl = new CacheControl.Builder()
.maxStale( 30, TimeUnit.DAYS )
.build();
request = request.newBuilder()
.cacheControl( cacheControl )
.build();
}
return chain.proceed( request );
}
};
}
}
答案 0 :(得分:0)
您应该添加Network Security Config以允许非SSL连接。 https://developer.android.com/training/articles/security-config