1. 程式人生 > >Fragment標籤頁+OKHttp網路請求資料+MVP模式

Fragment標籤頁+OKHttp網路請求資料+MVP模式

分包方式

在這裡插入圖片描述
需要的第三方依賴
在這裡插入圖片描述

Fragment

新建兩個fragment

在這裡插入圖片描述

在這裡插入圖片描述

MainActivity
在這裡插入圖片描述
主頁面佈局
在這裡插入圖片描述

設定Fragment+tablayout的介面卡
在這裡插入圖片描述

Fragment標籤頁結束

OKHttp網路請求

okhttputils工具類

package com.example.moni3.utils;

import com.google.gson.Gson;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public  class OkHttpUtils {
private static Gson gson = new Gson();
private static final String MEDIA_TYPE = "application/json; charset=utf-8";
private static final String METHOD_GET = "GET";
private static OkHttpClient okHttpClient;

public static OkHttpUtils okHttpUtils=new OkHttpUtils();
private OkHttpUtils(){}

public static OkHttpUtils init() {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.readTimeout(3000, TimeUnit.MILLISECONDS);
    builder.writeTimeout(3000, TimeUnit.MILLISECONDS);
    builder.connectTimeout(3000, TimeUnit.MILLISECONDS);
    okHttpClient = builder.build();
    return okHttpUtils;
}
private static Request createRequest(String url, String method, BaseRequest baseRequest) {

    Request.Builder builder = new Request.Builder().url(url);
    Request request = builder.build();
    return request;
}
public static  void enqueueGet(String url, Callback callback) {
    Request request = createRequest(url, METHOD_GET, null);
    Call call = okHttpClient.newCall(request);
    call.enqueue(callback);

}

}
注意 需要建立一個BaseRequest實現Serializable在這裡插入圖片描述

Application裡面呼叫工具類裡的初始化的方法
在這裡插入圖片描述

清單檔案要寫name
在這裡插入圖片描述

MVP

Model層呼叫網路請求
package com.example.moni3.mvp;

import com.example.moni3.bean.ShowBean;
import com.example.moni3.utils.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class ShowModel {
    String url = "http://www.xieast.com/api/news/news.php";
    public void show(final ShowCallBack showCallBack){
        OkHttpUtils.enqueueGet(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            //獲取資料
            String s = response.body().string();
            Gson gson = new Gson();
            ShowBean showBean = gson.fromJson(s,ShowBean.class);
            List<ShowBean.DataBean> data = showBean.getData();
            if(showBean.getCode() == 1){
                showCallBack.onShowSuccess(data);
            }else{
                showCallBack.onShowFailure("展示失敗");
            }
        }

    });
}

}
View層
在這裡插入圖片描述

Presenter連線Model和View
package com.example.moni3.mvp;

import java.util.List;

public class ShowPresenter {
    private ShowView showView;
    private final ShowModel showModel;

public ShowPresenter(ShowView showView) {
    this.showView = showView;
    showModel = new ShowModel();
}
public void show(){
    showModel.show(new ShowCallBack() {
        @Override
        public void onShowSuccess(List list) {
            showView.showSuccess(list);
        }

        @Override
        public void onShowFailure(String msg) {
            showView.failSuccess(msg);
        }
    });
}

}
CallBack介面
在這裡插入圖片描述

在fragment中展示資料
呼叫presenter層
package com.example.moni3.frag;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.moni3.R;
import com.example.moni3.bean.ShowBean;
import com.example.moni3.mvp.ShowPresenter;
import com.example.moni3.mvp.ShowView;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements ShowView {

    
   private ShowPresenter showPresenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        showPresenter = new ShowPresenter(this);
        showPresenter.show();
        return view;
    }

    @Override
    public void showSuccess(final List<ShowBean.DataBean> list) {
        Log.e("222222222222",""+list);
        //檢視當前執行緒
        Log.e("222222222222",""+Thread.currentThread().getName());
        //更換執行緒
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
            //吐司顯示
                Toast.makeText(getActivity(),list.toString(),Toast.LENGTH_SHORT).show();
                //下面展示資料(沒寫完)

            }
        });
    }

    @Override
    public void failSuccess(String msg) {

    }
}