BaseInterface
package com.bawei.myapplication.application;
public interface BaseInterface {
//P
public interface PresenterInterface {
public void toLogin(String phone, String pwd);
public void toSign(String phone, String pwd);
public void toShowList();
public void onDeistry();
}
//V
public interface ViewInterface {
public void showLogin(String str);
public void showSign(String str);
}
public interface ListViewInterface{
public void showList(Object obj);
}
}
contance
package com.bawei.myapplication.application;
public class Contance {
public static final String SIGN_URL="http://172.17.8.100/small/user/v1/register";
public static final String LOGIN_URL="http://172.17.8.100/small/user/v1/login";
public static final String LIST_URL="http://365jia.cn/news/api3/365jia/news/headline?page=1";
}
httputil
public class HttpUtils {
//單例
Contance contance;
static HttpUtils utils;
public OkHttpClient okHttpClient;
private HttpUtils(){
okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
return response;
}
}).build();
}
public static HttpUtils getInstance(){
if(utils==null){
utils=new HttpUtils();
}
return utils;
}
public void doGet(String url,Callback callback){
Request request = new Request.Builder()
.url(url)
.get()
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
public void dopost(String url,String phone, String pwd, Callback callback){
FormBody.Builder builder = new FormBody.Builder();
builder.add("phone",phone);
builder.add("pwd",pwd);
RequestBody body = builder.build();
Request request=new Request.Builder()
.post(body)
.url(url)
.build();
Call call= okHttpClient.newCall(request);
call.enqueue(callback);
}
}
M
public class MainModel{
MyCallback myCallback;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String josn = (String) msg.obj;
int type =msg.arg1;
if (type==1){
try {
JSONObject object=new JSONObject(josn);
String str=object.getString("message");
myCallback.success(str);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (type==2){
Gson gson=new Gson();
JsonBean jsonBean=gson.fromJson(josn, JsonBean.class);
myCallback.success(josn);
}
}
};
public void getRequst(){
HttpUtils utile = HttpUtils.getInstance();
utile.doGet(Contance.LIST_URL, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Message message = new Message();
message.obj = json;
message.arg1 = 1;
handler.sendMessage(message);
}
});
}
public void postRequst(String url,String phone,String pwd){
HttpUtils utils=HttpUtils.getInstance();
utils.dopost(url,phone, pwd, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Message message = new Message();
message.obj=json;
message.arg1=1;
handler.sendMessage(message);
}
});
}
public void setMyCallback(MyCallback myCallback){
this.myCallback=myCallback;
}
public interface MyCallback{
public void success(String mes);
}
}
P
public class MainPresenter<T> implements BaseInterface.PresenterInterface {
MainModel myModel;
T tt;
public MainPresenter(T t){
this.tt=t;
myModel = new MainModel();
}
@Override
public void toLogin(String phone, String pwd) {
myModel.setMyCallback(new MainModel.MyCallback() {
@Override
public void success(String mes) {
((BaseInterface.ViewInterface)tt).showLogin(mes);
}
});
myModel.postRequst(Contance.LOGIN_URL,phone, pwd);
}
@Override
public void toSign(String phone, String pwd) {
myModel.setMyCallback(new MainModel.MyCallback() {
@Override
public void success(String mes) {
((BaseInterface.ViewInterface)tt).showSign(mes);
}
});
myModel.postRequst(Contance.SIGN_URL,phone, pwd);
}
@Override
public void toShowList() {
myModel.setMyCallback(new MainModel.MyCallback() {
@Override
public void success(String mes) {
BaseInterface.ListViewInterface listViewInterface= ((BaseInterface.ListViewInterface)tt);
listViewInterface.showList(mes);
}
});
myModel.getRequst();
}
@Override
public void onDeistry() {
if (tt!=null){
tt=null;
}
}
}
V
main2
public class Main2Activity extends AppCompatActivity implements BaseInterface.ListViewInterface{
BaseInterface.PresenterInterface pInterface;
RecyclerView list;
List<JsonBean.DataBeanX.DataBean> mlist=new ArrayList<>();
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
list = findViewById(R.id.RecyclerView_id);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
list.setLayoutManager(layoutManager);
myAdapter = new MyAdapter(mlist,this);
list.setAdapter(myAdapter);
pInterface = new MainPresenter(this);
pInterface.toShowList();
}
@Override
public void showList(Object obj) {
JsonBean jsonBean= (JsonBean) obj;
mlist.addAll(jsonBean.getData().getData());
myAdapter.notifyDataSetChanged();
}
@Override
protected void onDestroy() {
super.onDestroy();
pInterface.onDeistry();
pInterface=null;
}
}
main
public class MainActivity extends AppCompatActivity implements BaseInterface.ViewInterface{
BaseInterface.PresenterInterface pInterface;
public EditText phone;
public EditText pwd;
public Button login;
public Button sign;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pInterface = new MainPresenter(this);
initView();
}
public void initView() {
phone = findViewById(R.id.phone_id);
pwd = findViewById(R.id.pwd_id);
login = findViewById(R.id.login_id);
sign = findViewById(R.id.sign_id);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String lphone = phone.getText().toString();
String lpwd = pwd.getText().toString();
pInterface.toLogin(lphone, lpwd);
}
});
sign.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String lphone = phone.getText().toString();
String lpwd = pwd.getText().toString();
pInterface.toSign(lphone, lpwd);
}
});
}
@Override
public void showLogin(String str) {
if (str.equals("登錄成功")){
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}else {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
@Override
public void showSign(String str) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {
List<JsonBean.DataBeanX.DataBean> list;
Context context;
int type=2;
View view=null;
public MyAdapter(List<JsonBean.DataBeanX.DataBean> list, Context context) {
this.list=list;
this.context=context;
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (type==2){
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, null);
}
return new Holder(view);
}
@Override
public void onBindViewHolder(@NonNull Holder holder, int i) {
if (type==2){
String title=list.get(i).getTitle();
String image="http://365jia.cn/uploads/"+list.get(i).getPics().get(0);
if(holder !=null&&holder.title!=null){
holder.title.setText(title);
Glide.with(context).load(image).into(holder.image);
}
}
}
@Override
public int getItemCount() {
if (list!=null){
return list.size();
}
return 0;
}
public class Holder extends RecyclerView.ViewHolder{
public TextView title;
public ImageView image;
public Holder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tet);
image = itemView.findViewById(R.id.image);
}
}
}