分包
1创建ibase接口然后用imainView继承ibaseview
package com.example.lian.view.interfaces;
import com.example.lian.model.bean.NewsBean;
lic interface IMainView extends IBaseView {
void callBackSuccess(NewsBean data);
void cakkBackFail(int code,String err);
}
2创建bean类然后httputils
package com.example.lian.model.utils;
import com.google.gson.Gson;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
pulic class HttpUtils<T> {
private static HttpUtils httpUtils;
public static HttpUtils getInstance(){
if (httpUtils==null){
httpUtils = new HttpUtils();
return httpUtils;
}else{
return httpUtils;
}
}
public T getData(String path,Class<T> tClass){
try {
URL url = new URL(path);
HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
if (urlConnection.getResponseCode()==200){
InputStream inputStream = urlConnection.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int length=0;
while ((length=inputStream.read(bytes))!=-1){
byteArrayOutputStream.write(bytes,0,length);
}
String value=byteArrayOutputStream.toString();
Gson gson = new Gson();
T t = gson.fromJson(value, tClass);
return t;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String postData(String path,String post){
URL url=null;
try {
url = new URL(path);
HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
printWriter.write(post);
printWriter.flush();
BufferedInputStream stream = new BufferedInputStream(httpURLConnection.getInputStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
byte[] arr = new byte[1024];
while ((len=stream.read(arr))!=-1){
bos.write(arr,0,len);
bos.flush();
}
bos.close();
return bos.toString("utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
3创建basepresenter然后mainpresenter层继承basepresenter
package com.example.lian.presenter;
import com.example.lian.model.bean.NewsBean;
import com.example.lian.model.utils.HttpUtils;
import com.example.lian.view.interfaces.IMainView;
pulic class MainPresenter extends BasePresenter {
private IMainView view;
private HttpUtils httpUtils;
public MainPresenter(){
httpUtils=HttpUtils.getInstance();
}
public void getHomeData(final String url){
new Thread(new Runnable() {
@Override
public void run() {
NewsBean data =(NewsBean) httpUtils.getData(url, NewsBean.class);
view.callBackSuccess(data);
}
}).start();
}
public void setView(IMainView view){
this.view=view;
}
}
4mainactivity
package com.example.lian.view.activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import com.example.lian.R;
import com.example.lian.model.bean.NewsBean;
import com.example.lian.presenter.MainPresenter;
import com.example.lian.view.adapter.MyAdapter;
import com.example.lian.view.interfaces.IBaseView;
import com.example.lian.view.interfaces.IMainView;
import java.util.List;
public class MainActivity extends AppCompatActivity implements IMainView {
private String path = "http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=one";
private MainPresenter mainPresenter;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
NewsBean newsBean =(NewsBean) msg.obj;
List<NewsBean.DataBean> data = newsBean.getData();
MyAdapter myAdapter=new MyAdapter(data,MainActivity.this);
listView.setAdapter(myAdapter);
}
};
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
//Log.e("myMessage","main"+Thread.currentThread().toString());
}
@Override
protected void onResume() {
super.onResume();
loadDataFromNet();
}
private void initView() {
listView = findViewById(R.id.ListView);
}
private void initData() {
mainPresenter = new MainPresenter();
mainPresenter.setView(this);
}
private void loadDataFromNet() {
mainPresenter.getHomeData(path);
}
@Override
public void callBackSuccess(NewsBean data) {
Message message = handler.obtainMessage();
message.obj=data;
message.what=1;
handler.sendMessage(message);
}
@Override
public void cakkBackFail(int code, String err) {
}
}