RetrofitOkHttp网络请求
public static final String HOST_URL="";
public interface HttpUtils { @GET(API.URL) Call<Bean<NewslistBean>> NeeslistBean(); }
public interface NetWorkListener<T> { public void onSuccess(T t); public void onFailure(Exception e); }
public class RetrofitHelper { private static OkHttpClient okHttpClient; private static HttpUtils httpUtils; /** * 静态代码块 */ static { initOkHttpClient(); } /** * 初始化OkHttpClient */ private static void initOkHttpClient(){ if(okHttpClient==null){ synchronized (RetrofitHelper.class){ if (okHttpClient==null){ //创建拦截器 HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); //创建OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(loggingInterceptor) .build(); } } } } /** * 初始化一个单例 * */ public static HttpUtils getHttpUtils(){ if(httpUtils==null){ synchronized (HttpUtils.class){ if(httpUtils==null){ httpUtils=RetrofitHelper.createAPI(HttpUtils.class,API.HOST_URL); } } } return httpUtils; } public static <T> T createAPI(Class<T> tClass,String url){ Retrofit retrofit = new Retrofit.Builder().baseUrl(url).client(okHttpClient) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(tClass); } }
public class RetrofitHelper { private static OkHttpClient client; private static ServiceApi serviceApi; static { initOkHttp(); } //初始化OkHttpClient private static void initOkHttp() { if (client == null) { synchronized (OkHttpClient.class) { if (client == null) { client = new OkHttpClient.Builder().build(); } } } } /** * 初始化一个单例 * @return */ public static ServiceApi getServiceApi() { if (serviceApi == null) { synchronized (ServiceApi.class) { if (serviceApi == null) { serviceApi = RetrofitHelper.create(ServiceApi.class, UrlUtils.BASE_URL); } } } return serviceApi; } /** * 初始化Retrofit ,生成API方法 * * @param tClass * @param url * @param <T> * @return */ private static ServiceApi create(Class<ServiceApi> serviceApiClass, String baseUrl) { Retrofit re = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); return re.create(serviceApiClass); } }
public class DataPresenter { private IMainActivity iMainActivity; private IGetModel iGetModel; public DataPresenter(IMainActivity iMainActivity){ this.iMainActivity = iMainActivity; iGetModel = new GetModel(); } public void getDatas(){ /*iGetModel.getData(new OnListiner<Bean>() { @Override public void onSuccess(Bean bean) { List<Bean.NewslistBean> newslist = bean.getNewslist(); iMainActivity.onShow(newslist); } @Override public void onFailure(Throwable throwable) { } });*/ iGetModel.getData(new OnListiner<Bean>() { @Override public void onSuccess(Bean bean) { List<Bean.NewslistBean> newslist = bean.getNewslist(); iMainActivity.onShow(newslist); } @Override public void onFailure(Throwable throwable) { } }); } }
public class GetModel implements IGetModel { @Override public void getData(final OnListiner<Bean> onListiner) { ServiceApi serviceApi = RetrofitHelper.getServiceApi(); Call<Bean> data = serviceApi.getData(); data.enqueue(new Callback<Bean>() { @Override public void onResponse(Call<Bean> call, Response<Bean> response) { Bean body = response.body(); onListiner.onSuccess(body); } @Override public void onFailure(Call<Bean> call, Throwable t) { onListiner.onFailure(t); } }); } }
public class MainActivity extends AppCompatActivity implements IMainActivity{ private XRecyclerView mRc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* DataPresenter presenter = new DataPresenter(this); presenter.getDatas();*/ DataPresenter presenter = new DataPresenter(this); presenter.getDatas(); initView(); } private void initView() { mRc = (XRecyclerView) findViewById(R.id.rc); } @Override public void onShow(List<Bean.NewslistBean> list) { MyAdapter adapter = new MyAdapter(this, list); mRc.setLayoutManager(new LinearLayoutManager(this)); mRc.setAdapter(adapter); mRc.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { mRc.refreshComplete(); } @Override public void onLoadMore() { } }); } }
item <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ti" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/des" /> </LinearLayout> <com.facebook.drawee.view.SimpleDraweeView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/img" fresco:roundAsCircle="true" android:layout_alignParentRight="true"></com.facebook.drawee.view.SimpleDraweeView> </RelativeLayout>