一、XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
二、Api
public class Api {
/*
* 开发环境
* */
//public static final String BASE_URL="http://172.17.8.100";
/*http://172.17.8.100/small/commodity/v1/commodityList
* 测试环境
* */
public static final String BASE_URL="http://172.17.8.100";
//首页商品信息列表
public static String SHOPLIST=BASE_URL+"/small/commodity/v1/commodityList";
}
三、model
1、接口
public interface IHomeListModeInter {
//获取数据
void getDate(String url);
}
2、class
public class HomeListModeInter implements IHomeListModeInter{
ModuleInterface moduleInterface;
public HomeListModeInter(ModuleInterface moduleInterface){
this.moduleInterface = moduleInterface;
}
@Override
public void getDate(final String url) {
new Runnable(){
@Override
public void run() {
new MyTask<String>(url,"GET").setListebter(new MyTask.TaskListeners() {
@Override
public void result(String t) {
Log.i("","result:=== "+t);
if (t != null){
moduleInterface.LoadSuccess(t);
}else {
moduleInterface.LoadFailed();
}
}
}).execute();
}
}.run();
}
public interface ModuleInterface{
//获取数据状态回调的接口
void LoadSuccess(String data);
void LoadFailed();
}
}
四、presenter
1、接口
public interface IHomeListPresenter {
//获取model
public void getModelDate();
}
2、class
public class HomeListPresenter implements IHomeListPresenter,HomeListModeInter.ModuleInterface{
MainActivity mview;
private final HomeListModeInter homeListModeInter;
public HomeListPresenter(MainActivity mainActivity) {
this.mview = mainActivity;
//初始化Model层
homeListModeInter = new HomeListModeInter(this);
}
@Override
public void getModelDate() {
//回调model
homeListModeInter.getDate(Api.SHOPLIST);
}
@Override
public void LoadSuccess(String data) {
mview.getViewData(data);
}
@Override
public void LoadFailed() {
mview.getViewData("加载失败");
}
}
五、view
1、接口
public interface IHomeListView {
public void getViewData(String mViewData);
}
2、MainActivity
public class MainActivity extends AppCompatActivity implements IHomeListView {
private TextView viewById;
private HomeListPresenter homeListPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewById = findViewById(R.id.tv);
//初始化
homeListPresenter = new HomeListPresenter(this);
homeListPresenter.getModelDate();
}
public void getViewData(String mViewData) {
viewById.setText(mViewData);
}
}
六、封装AsyncTask网络请求
1、HttpUrlUtils
public class HttpUrlUtils {
//请求获取网络请求
public static String getHttpCon(String mPath,String mRam){
String messages = "";
try {
URL murl = new URL(mPath);
HttpURLConnection connection = (HttpURLConnection) murl.openConnection();
//设置请求发送
connection.setRequestMethod(mRam);
//请求超时
connection.setConnectTimeout(5 * 1000);
connection.setReadTimeout(5 * 1000);
//数据流处理
if (connection.getResponseCode() == 200){
//获取输入流
InputStream inputStream = connection.getInputStream();
//读取输入流
byte[] b = new byte[1024 * 1024];//定义一个byte数组读取输入流
ByteArrayOutputStream stream = new ByteArrayOutputStream();//定义缓存流来保存输入流的数据
int len = 0;
while ((len = inputStream.read(b))>-1){
stream.write(b,0,len);//三个参数 输入流byte数组 读取起始位置 读取终止位置
}
messages = stream.toString();
inputStream.close();
connection.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
return messages;
}
}
2、MyTask
public class MyTask<T> extends AsyncTask<T,T,String>{
String mPath;
String mRam;
MainActivity main;
private TaskListeners listeners;
public MyTask setListebter(TaskListeners listeners) {
this.listeners = listeners;
return this;
}
public MyTask(String mPath, String mRam){
this.mPath = mPath;
this.mRam = mRam;
}
@Override
protected String doInBackground(T... ts) {
return HttpUrlUtils.getHttpCon(mPath,mRam);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s != null){
//实现接口内面方法
listeners.result(s);
}
}
//定义接口
public interface TaskListeners{
void result(String t);
}
}
七、判断网络是否可以使用
public class NetWorkUtils {
//获取网络状态
public static boolean getNet(Context context){
//初始化网络装填管理者
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络类型
NetworkInfo info = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return info.isConnected();
}
}