MVP
来源:互联网 发布:java date 时区转换 编辑:程序博客网 时间:2024/06/05 03:35
//mainactivity
<?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" tools:context="com.bawei.zy.mvpstudy.MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
//item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
//M
public interface IModel {
void data(IP ip);
}
//另一个
public class Model implements IModel{
Handler handler = new Handler();
@Override
public void data(final IP ip) {
OkHttp3Utils.doGet("http://m.yunifang.com/yunifang/mobile/home", new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
handler.post(new Runnable() {
@Override
public void run() {
try {
String string = response.body().string();
Guanggao netData = Guanggao.objectFromData(string);
List<Guanggao.DataBean.Ad1Bean> ad1 = netData.getData().getAd1();
ip.getdata(ad1);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
}
}
//P
public interface IP {
void ptada();
void getdata(List<Guanggao.DataBean.Ad1Bean> ad1 );
}
//另一个
public class P implements IP{
private IModel iModel;
private IV iv;
public P(IV iv) {
this.iv = iv;
iModel = new Model();
}
@Override
public void ptada() {
iModel.data(this);
}
@Override
public void getdata(List<Guanggao.DataBean.Ad1Bean> ad1 ) {
iv.vdata(ad1);
}
}
//V
public interface IV {
void vdata(List<Guanggao.DataBean.Ad1Bean> ad1 );
}
//工具类
public class OkHttp3Utils {
private static OkHttpClient okHttpClient = null;
public OkHttp3Utils() {
}
//通过单例模式获取实例
public static OkHttpClient getInstance() {
if (okHttpClient == null) {
//同步代码
synchronized (OkHttp3Utils.class) {
if (okHttpClient == null) {
//缓存路径
File sdcache = new File(Environment.getExternalStorageDirectory(), "wybcache");
int cacheSize = 10 * 1024 * 1024;
//okhttp拦截器
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("msg", message.toString());
}
});
//拦截器日志分类
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.MINUTES)
.addInterceptor(httpLoggingInterceptor)
//.addNetworkInterceptor(null)
.writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS)
.cache(new Cache(sdcache.getAbsoluteFile(), cacheSize)).build();
//add
}
}
}
return okHttpClient;
}
/**
* get请求方式
*
* @param url 请求地址
* @param callback 回调接口
*/
public static void doGet(String url, Callback callback) {
//获取请求对象的实例
OkHttpClient okHttpClient = OkHttp3Utils.getInstance();
//创建Request
Request request = new Request.Builder().url(url).build();
//得到Call
Call call = okHttpClient.newCall(request);
//执行异步请求
call.enqueue(callback);
}
/**
* post请求方式
*
* @param url 请求地址
* @param params 请求体
* @param callback 接口回调
*/
public static void doPost(String url, Map<String, String> params, Callback callback) {
//获取请求对象的实例
OkHttpClient okHttpClient = OkHttp3Utils.getInstance();
//3.x版本post请求换成FormBody 封装键值对参数
FormBody.Builder builder = new FormBody.Builder();
//遍历map集合给请求体添加值
for (String key : params.keySet()) {
builder.add(key, params.get(key));
}
//创建Request
Request request = new Request.Builder().url(url).post(builder.build()).build();
//获取call
Call call = okHttpClient.newCall(request);
//异步请求
call.enqueue(callback);
}
/**
* 上传文件
*
* @param url 接口地址
* @param file 文件
* @param fileName 文件名
*/
public static void loadFile(String url, File file, String fileName) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//创建RequestBody 封装file参数
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
//创建RequestBody 设置类型等
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", fileName, fileBody).build();
//创建Request
Request request = new Request.Builder().url(url).post(requestBody).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 {
//请求成功
}
});
}
/**
* 上传json字符
*
* @param url 接口地址
* @param jsonParams Json串
* @param callback 接口回调
*/
public static void doPostJson(String url, String jsonParams, Callback callback) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams);
Request request = new Request.Builder().url(url).post(requestBody).build();
Call call = getInstance().newCall(request);
call.enqueue(callback);
}
/**
* 下载
*
* @param context
* @param url 下载地址
* @param saveDir 保存的位置
*/
public static void downFile(final Activity context, final String url, final String saveDir) {
//创建Request
Request request = new Request.Builder().url(url).build();
//创建Call
Call call = getInstance().newCall(request);
//同步
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("xxx", e.toString());
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
try {
is = response.body().byteStream();
//apk保存路径
final String fileDir = isExistDir(saveDir);
//文件
File file = new File(fileDir, getNameFromUrl(url));
context.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "下载成功:" + fileDir + "," + getNameFromUrl(url), Toast.LENGTH_SHORT).show();
}
});
fos = new FileOutputStream(file);
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
//apk下载完成后 调用系统的安装方法
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) is.close();
if (fos != null) fos.close();
}
}
});
}
/**
* @param saveDir
* @return
* @throws IOException 判断下载目录是否存在
*/
public static String isExistDir(String saveDir) throws IOException {
// 下载位置
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir);
if (!downloadFile.mkdirs()) {
downloadFile.createNewFile();
}
String savePath = downloadFile.getAbsolutePath();
Log.e("savePath", savePath);
return savePath;
}
return null;
}
/**
* @param url
* @return 从下载连接中解析出文件名
*/
private static String getNameFromUrl(String url) {
return url.substring(url.lastIndexOf("/") + 1);
}
}
//MainActivity
public class MainActivity extends AppCompatActivity implements IV{
private ListView listView;
private IP ip;
List<Guanggao.DataBean.Ad1Bean> ad1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
ip = new P(this);
ip.ptada();
}
@Override
public void vdata(List<Guanggao.DataBean.Ad1Bean> ad1) {
this.ad1 = ad1;
listView.setAdapter(new MyApater());
}
class MyApater extends BaseAdapter{
@Override
public int getCount() {
return ad1.size();
}
@Override
public Object getItem(int i) {
return ad1.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view=View.inflate(MainActivity.this,R.layout.item,null);
TextView textView = (TextView)view. findViewById(R.id.tv);
textView.setText(ad1.get(i).getTitle());
return view;
}
}
//http://m.yunifang.com/yunifang/mobile/home
}
- MVP
- MVP
- MVP
- MVP
- MVP
- MVP
- MVP
- MVP
- mvp
- MVP
- MVP
- mvp
- MVP
- MVP
- MVP
- MVP
- MVP
- MVP
- 1.mongodb简介
- C++之memset使用详解及This function or variable may be unsafe错误
- Leetcode 22 Generate Parentheses
- SpringBoot集成Swagger
- java08总结
- MVP
- 五线谱上的Linux旋律
- 永安行收购哈罗单车 无人驾驶公交湖南试运营
- AI | 人才短缺!LinkedIn成立人工智能学院 内部孵化AI工程师
- 共享单车并购第一案背后:都快撑不住了!
- 如何查看对方发邮件的ip地址
- 原码, 反码, 补码 详解
- $.ajax使用总结(一):Form提交与Payload提交
- Java并发工具类之CountDownLatch