compile 'com.squareup.okhttp3:okhttp:3.5.0'
OkHttpClient mOkHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(uri).build();
Response response = mOkHttpClient.newCall(request).execute();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("lgq","error:"+e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("lgq","okhttp response:"+response);
//封装了观察者对象代码
RequestObserable.responNetRequset(mRecyclerview,response.body().string(),getApplicationContext());
}
});
public static void responNetRequset(final RecyclerView mRecyclerview, String result, final Context context) {
Gson gson = new Gson();
final NewsInfo newsInfo = gson.fromJson(result, NewsInfo.class);
Observable.create(new ObservableOnSubscribe>() {
@Override
public void subscribe(ObservableEmitter> e) throws Exception {
e.onNext(newsInfo.getResult().getList());
e.onComplete();
}
}).observeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer>() {
@Override
public void onSubscribe(Disposable d) {
Log.i("lgq", "onSubscribe");
}
@Override
public void onNext(List value) {
System.out.println("onNext()----Thread:" + Thread.currentThread().getName());
RecycleAdapter mAdapter = new RecycleAdapter(context, value);
//适配器实现监听事件
itemOnClick(mAdapter);
mRecyclerview.setLayoutManager(new LinearLayoutManager(context));
mRecyclerview.setAdapter(mAdapter);
}
@Override
public void onError(Throwable e) {
Log.i("lgq", "onError:" + e);
}
@Override
public void onComplete() {
Log.i("lgq", "onComplete()");
}
});
}
1.okhttp-3.5.0.jar包大小342K。
compile 'com.mcxiaoke.volley:library:1.0.19'
//volley请求队列
public static RequestQueue requestQueue;
public static RequestQueue getRequestQueue(){
return requestQueue;
}
@Override
public void onCreate() {
super.onCreate();
//初始化请求队列
requestQueue = Volley.newRequestQueue(getApplicationContext());
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, uri, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
Log.i("lgq","volley response:"+response);
RequestObserable.responNetRequset(mRecyclerview,response.toString(),getApplicationContext());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("lgq","volley error:"+error);
}
});
//setting tag for cancel
jsonObjectRequest.setTag(VO_GET_TAG);
App.getRequestQueue().add(jsonObjectRequest);
@Override
protected void onStop() {
super.onStop();
//activity关闭的的时候取消请求队列
App.getRequestQueue().cancelAll(VO_GET_TAG);
}
1.library-1.0.19.jar大小为91.6K。
//retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
public interface RetrofitService {
@GET("get")
Call getListDatas(
@Query("channel")String str,
@Query("start")String start,
@Query("num")int num,
@Query("appkey")String appkey
);
}
Retrofit retrofit = new Retrofit.Builder()
//设置baseUrl,注意baseUrl 应该以/ 结尾。
.baseUrl("http://api.jisuapi.com/news/")
//使用Gson解析器,可以替换其他的解析器
.addConverterFactory(GsonConverterFactory.create())
.client(new OkHttpClient())
.build();
//利用Retrofit 创建服务接口
RetrofitService retrofitService = retrofit.create(RetrofitService.class);
//实现接口参数
Call call = retrofitService.getListDatas("头条","0",20,BaseActivity.getAPPKEY());
//执行异步请求,这里的onResponse(),onFailure()都是在主线程中运行的
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()){
RecycleAdapter mAdapter = new RecycleAdapter(getApplicationContext(),response.body().getResult().getList()); //初始化适配器
RequestObserable.itemOnClick(mAdapter); //item监听时间
mRecyclerview.setLayoutManager(linearLayoutManager);
mRecyclerview.setAdapter(mAdapter);
}
}
@Override
public void onFailure(Call call, Throwable t) {
Log.i("lgq","onFailure:"+t);
}
});
1.retrofit-2.1.0.jar大小为86.2K。