Retrofit框架越來(lái)越流行了俭尖,Retrofit是基于OKHTTP的大家都知道,在之前的話,在Retrofit1.x的時(shí)候稽犁,是必須要自己手動(dòng)導(dǎo)入OKHTTP 和 OKio的包的焰望,因?yàn)镽etrofit依賴于這兩個(gè)庫(kù)的。但是自從升級(jí)了Retrofit2之后已亥,就可以不用手動(dòng)導(dǎo)入了熊赖,因?yàn)橐呀?jīng)自己引入了。
Retrofit有一個(gè)優(yōu)點(diǎn)虑椎,就是可以自動(dòng)根據(jù)獲取到的數(shù)據(jù)轉(zhuǎn)換成相對(duì)應(yīng)的Bean震鹉,它內(nèi)部提供了一個(gè)轉(zhuǎn)換機(jī)制,只需要你重寫捆姜,就能寫出自己的轉(zhuǎn)換規(guī)則传趾。
dependencies {
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-scalars:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
}
可以看到,我上面引入了Retrofit2的庫(kù)
compile 'com.squareup.retrofit2:retrofit:2.2.0'
但是除了這個(gè)份之外泥技,我還引入了其他的浆兰。
這兩個(gè),是在從請(qǐng)求Json數(shù)據(jù)到Bean需要使用到的珊豹。依賴了谷歌的Gson庫(kù)
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.google.code.gson:gson:2.7'
直接獲取字符串手動(dòng)解析
除了這種情況簸呈,我們經(jīng)常會(huì)因?yàn)楹笈_(tái)傳來(lái)的數(shù)據(jù)的不穩(wěn)定性,我們需要自己手動(dòng)去解析字符串平夜,那么就引入了這個(gè)
compile 'com.squareup.retrofit2:converter-scalars:2.0.0'
使用方式好簡(jiǎn)單:
new Retrofit.Builder() //01:獲取Retrofit對(duì)象
.baseUrl(Globals.SERVER_ADDRESS) //02采用鏈?zhǔn)浇Y(jié)構(gòu)綁定Base url
.addConverterFactory(ScalarsConverterFactory.create())//首先判斷是否需要轉(zhuǎn)換成字符串蝶棋,簡(jiǎn)單類型
.addConverterFactory(GsonConverterFactory.create())//再將轉(zhuǎn)換成bean
.client(okHttpClient)
.build();//03執(zhí)行操作
public interface StartPageNet
{
@GET("hao123-soft-online-bcs/soft/2017_02_22_W.P.S.5041.19.552.exe")
public Call<String> getIndex();
}
首先在這里定義卸亮,
Call<String>
里的泛型可以寫這幾種類型:
if (type == String.class
|| type == boolean.class
|| type == Boolean.class
|| type == byte.class
|| type == Byte.class
|| type == char.class
|| type == Character.class
|| type == double.class
|| type == Double.class
|| type == float.class
|| type == Float.class
|| type == int.class
|| type == Integer.class
|| type == long.class
|| type == Long.class
|| type == short.class
|| type == Short.class) {
return ScalarRequestBodyConverter.INSTANCE;
}
這里會(huì)自動(dòng)根據(jù)返回?cái)?shù)據(jù)轉(zhuǎn)換成你泛型里寫的類型的數(shù)據(jù)忽妒。
StartPageNet startPageNet = streamRetrofit.create(StartPageNet.class);
Call<String> download = startPageNet.getIndex();
download.enqueue(new Callback<String>()
{
@Override
public void onResponse(Call<String> call, Response<String> response)
{
String result = response.body();
}
@Override
public void onFailure(Call<String> call, Throwable t)
{
}
});
查看Retrofit請(qǐng)求網(wǎng)絡(luò)日志
有時(shí)候需要隨時(shí)查看網(wǎng)絡(luò)請(qǐng)求日志,我們這里可以利用
OKHttp
的Interceptor
機(jī)制
上面我們引入了這個(gè)庫(kù):
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
使用代碼如下:
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger()
{
@Override
public void log(String message)
{
if (BuildConfig.DEBUG) Log.d("Http", message+"");
}
});
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//設(shè)置日志打印等級(jí)
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)//設(shè)置日志打印
.retryOnConnectionFailure(true)//失敗重連
.connectTimeout(30, TimeUnit.SECONDS)//網(wǎng)絡(luò)請(qǐng)求超時(shí)時(shí)間單位為秒
.build();
.addInterceptor()
可以調(diào)用多次
自定義Interceptor實(shí)現(xiàn)過(guò)濾改變請(qǐng)求返回的數(shù)據(jù)(可使用與保證APP的穩(wěn)定性)
import com.alibaba.fastjson.JSON;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.BufferedSource;
import okio.Okio;
/**
* 網(wǎng)絡(luò)請(qǐng)求的攔截器
* Created by xiaolei on 2017/3/2.
*/
public class SessionInterceptor implements Interceptor
{
@Override
public Response intercept(Chain chain) throws IOException
{
Request request = chain.request();
Response response = chain.proceed(request);
return new Response.Builder()
.body(newResponseBody(response))
.headers(response.headers())
.message(response.message())
.code(response.code())
.request(response.request())
.protocol(response.protocol())
.build();
}
private ResponseBody newResponseBody(final Response response)
{
return new ResponseBody()
{
@Override
public MediaType contentType()
{
return response.body().contentType();
}
@Override
public long contentLength()
{
return response.body().contentLength();
}
@Override
public BufferedSource source()
{
try
{
String result = response.body().string();
if(JSON.parseObject(result).getInteger("code") == 500)
{
/**
*這里改變返回的數(shù)據(jù)兼贸,如果服務(wù)器返回的是一個(gè)HTML網(wǎng)頁(yè)段直,
*那么移動(dòng)端也能拿到一個(gè)Json數(shù)據(jù),用于保證數(shù)據(jù)可解析不至于崩潰
*/
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream("{code:500,success:false}".getBytes());
BufferedSource source = Okio.buffer(Okio.source(tInputStringStream));
return source;
}else
{
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(result.getBytes());
BufferedSource source = Okio.buffer(Okio.source(tInputStringStream));
return source;
}
} catch (IOException e)
{
e.printStackTrace();
return null;
}
}
};
}
}