您的位置:首页 > 理论基础 > 计算机网络

MVP+okhttp请求网络接口

2017-11-23 14:27 281 查看
                                                            MVP+okhttp请求网络接口

manifest加权限:

<uses-permission android:name="android.permission.INTERNET" />

build.gradle添加依赖:
recyclerview依赖:
compile 'com.android.support:recyclerview-v7:26.0.+'
okhttp依赖:
compile 'com.squareup.okhttp3:okhttp:3.9.0'
gson依赖:
compile 'com.google.code.gson:gson:2.8.1'
glide依赖:
compile 'com.github.bumptech.glide:glide:3.7.0'

main——activity布局:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

item布局:
<ImageView
android:id="@+id/img"
android:layout_margin="10dp"
android:src="@mipmap/ic_launcher"
android:layout_width="80dp"
android:layout_height="80dp" />

<TextView
android:id="@+id/tv"
android:text="您好!我是帅哥刘鑫"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

 工具类OkHttpUtils:
public class OkHttpUtils {

private static OkHttpClient okHttpClient;
private static Handler handler = new Handler();

public static void doGet(String url, final SendData callBack){
okHttpClient = new OkHttpClient.Builder()
//添加拦截器等等
.build();
//请求体
Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
callBack.sendData(string);
}
});
}
});
}

public interface SendDa
4000
ta{
void sendData(String result);
}
}

Modle层:
public class DataModel implements Contract.IDataModel{

private String url = "http://shouyoutoutiao.app.17wanba.com/toutiao/get_list";
@Override
public void getData(final IGetDataFromNet iGetDataFromNet) {
OkHttpUtils.doGet(url, new OkHttpUtils.SendData() {
@Override
public void sendData(String result) {
Bean bean = Bean.objectFromData(result);
List<Bean.DataBean> data = bean.data;
iGetDataFromNet.getData(data);
}
});
}

public interface IGetDataFromNet{
void getData(List<Bean.DataBean> list);
}
}


Contract层:
public class Contract {
//    1、创建View  get/set
public interface IDataView {
void setData(List<Bean.DataBean> list);
//   void setText(String str);
//    String getText();
}

//    2、Model接口
public interface IDataModel {
//获取数据
void getData(DataModel.IGetDataFromNet iGetDataFromNet);//此处需要回调接口
}

//    3、Presenter接口
public interface IDataPresenter {
void showData();
}
}

Presenter层:
public class DataPresenter implements Contract.IDataPresenter{
//需要Model,view
private DataModel model;
private Contract.IDataView view;

public DataPresenter(Contract.IDataView view) {
this.view = view;
model = new DataModel();
}

@Override
public void showData() {
model.getData(new DataModel.IGetDataFromNet() {
@Override
public void getData(List<Bean.DataBean> list) {
view.setData(list);
}
});
}
}

Bean类:
public class Bean {

/**
* ret : 0
* data : [{"id":"23427","type":"1","source_id":"15","template_id":"5","title":"烧菜制药成就《我叫MT3》财富之路","img":[{"url":"http://img.storage.17wanba.org.cn/shouyou/2016/07/05/6bb734aa3f191fc34b3f62e17968f6e4.jpg"}],"url":"/toutiao/detail/23427?create_time=1510170601","status":"1","create_time":"11月08日 19:50","time_length":"0","source_name":"一起玩吧"},{"id":"20386","type":"1","source_id":"19","template_id":"5","title":"《枪战英雄》超燃首测 百万名枪礼品助你超神","img":[{"url":"http://img.storage.17wanba.org.cn/shouyou/2016/06/17/26d484db27eaa4812d153662147f59f7.jpg"}],"url":"/toutiao/detail/20386?create_time=1508936894","status":"1","create_time":"10月25日 13:08","time_length":"0","source_name":"游戏多"},{"id":"736","type":"1","source_id":"15","template_id":"5","title":"《全民奇迹MU》好基友一辈子 坑坑更健康","img":[{"url":"http://img.storage.17wanba.org.cn/shouyou/2016/03/23/e555b5fca3037f3be0dfaf61d19a3977.jpg"}],"url":"/toutiao/detail/736?create_time=1507698316","status":"1","create_time":"10月11日 05:05","time_length":"0","source_name":"一起玩吧"},{"id":"15263","type":"1","source_id":"15","template_id":"5","title":"《山口山战记》站撸小王子会长全面解析","img":[{"url":"http://img.storage.17wanba.org.cn/shouyou/2016/05/26/2acd8269422f84ae2e7dbe79e5b24714.jpg"}],"url":"/toutiao/detail/15263?create_time=1505516991","status":"1","create_time":"09月15日 23:09","time_length":"0","source_name":"一起玩吧"},{"id":"14609","type":"1","source_id":"15","template_id":"5","title":"玩友时代:专注耕耘女性市场","img":[{"url":"http://img.storage.17wanba.org.cn/shouyou/2016/05/25/3eadbc701ab2fb043249ccc81b2dc5d4.jpg"}],"url":"/toutiao/detail/14609?create_time=1503712094","status":"1","create_time":"08月26日 01:48","time_length":"0","source_name":"一起玩吧"},{"id":"9875","type":"1","source_id":"15","template_id":"5","title":"漫威的英雄主义,触手TV的游戏激情","img":[{"url":"http://img.storage.17wanba.org.cn/shouyou/2016/05/11/b2150f6ef0fc1cfeb2df732d5a5ba659.jpg"}],"url":"/toutiao/detail/9875?create_time=1502334268","status":"1","create_time":"08月10日 03:04","time_length":"0","source_name":"一起玩吧"},{"id":"10751","type":"1","source_id":"17","template_id":"5","title":"中国Dota2职业联赛启动 电竞选手成为国家注册运动员","img":[{"url":"http://img.storage.17wanba.org.cn/shouyou/2016/05/13/ac6f94266c1e425e8b545d9dffde8de9.jpg"}],"url":"/toutiao/detail/10751?create_time=1500270343","status":"1","create_time":"07月17日 05:45","time_length":"0","source_name":"触乐"},{"id":"15227","type":"1","source_id":"21","template_id":"5","title":"史上首位机器人律师即将上岗","img":[{"url":"http://img.storage.17wanba.org.cn/shouyou/2016/05/26/bf23b72f63948cf9a2e3f831f86220dc.jpg"}],"url":"/toutiao/detail/15227?create_time=1499444287","status":"1","create_time":"07月07日 16:18","time_length":"0","source_name":"口袋巴士"}]
* flag : 0
*/

public int ret;
public int flag;
public List<DataBean> data;

public static Bean objectFromData(String str) {

return new Gson().fromJson(str, Bean.class);
}

public static class DataBean {
/**
* id : 23427
* type : 1
* source_id : 15
* template_id : 5
* title : 烧菜制药成就《我叫MT3》财富之路
* img : [{"url":"http://img.storage.17wanba.org.cn/shouyou/2016/07/05/6bb734aa3f191fc34b3f62e17968f6e4.jpg"}]
* url : /toutiao/detail/23427?create_time=1510170601
* status : 1
* create_time : 11月08日 19:50
* time_length : 0
* source_name : 一起玩吧
*/

public String id;
public String type;
public String source_id;
public String template_id;
public String title;
public String url;
public String status;
public String create_time;
public String time_length;
public String source_name;
public List<ImgBean> img;

public static DataBean objectFromData(String str) {

return new Gson().fromJson(str, DataBean.class);
}

public static class ImgBean {
/**
* url : http://img.storage.17wanba.org.cn/shouyou/2016/07/05/6bb734aa3f191fc34b3f62e17968f6e4.jpg */

public String url;

public static ImgBean objectFromData(String str) {

return new Gson().fromJson(str, ImgBean.class);
}
}
}
}

适配器:
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
private List<Bean.DataBean> list = new ArrayList<>();
private Context context;
private LayoutInflater inflater;
public Adapter(List<Bean.DataBean> list, Context context) {
this.list = list;
this.context = context;
inflater = LayoutInflater.from(context);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item, parent, false);

return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Bean.DataBean bean = list.get(position);
holder.tv.setText(bean.title);
Glide.with(context).load(bean.img.get(0).url).into(holder.iv);
}

@Override
public int getItemCount() {
return list==null?0:list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView iv;
public TextView tv;
public ViewHolder(View itemView) {
super(itemView);
iv = itemView.findViewById(R.id.img);
tv = itemView.findViewById(R.id.tv);

}
}
}
Main_Activity类:
public class Main_Activity extends AppCompatActivity implements Contract.IDataView{
private RecyclerView my_recycler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

               initView();
}
private void initView() {
my_recycler = (RecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager manager = new LinearLayoutManager(this);
my_recycler.setLayoutManager(manager);

DataPresenter presenter = new DataPresenter(this);
presenter.showData();
}

@Override
public void setData(List<Bean.DataBean> list) {
Adapter adapter = new Adapter(list,this);
my_recycler.setAdapter(adapter);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: