首先在build.gradle导入依赖
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okio:okio:1.5.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.google.code.gson:gson:2.2.4'
testCompile 'junit:junit:4.12'
在配置文件中添加权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
接下来是布局,四个布局,登录页面,注册页面,listview数据展示页面,title标题栏页面
activity_main.xml登录页面布局
<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="com.example.dengluadd.IVew.MainActivity">
<TextView
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:textSize="30dp"/>
<View
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="0.75dp"/>
<EditText
android:id="@+id/tell"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"/>
<Button
android:id="@+id/zhuce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"/>
</LinearLayout>
</LinearLayout>
activity_main2.xml注册页面
<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="com.example.dengluadd.IVew.Main2Activity">
<com.example.dengluadd.IVew.Mytitle
android:layout_width="match_parent"
android:layout_height="50dp" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
activity_main3.xml数据展示页面
<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="com.example.dengluadd.IVew.Main3Activity">
<TextView
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="30dp"/>
<View
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="0.75dp"/>
<EditText
android:id="@+id/tell"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/zhuce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"/>
</LinearLayout>
</LinearLayout>
title_aa.xml title标题栏
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#8A9BF3"
android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_alignParentLeft="true"
android:src="@drawable/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="段子"
android:textSize="35dp"
android:textColor="#FFFF"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:layout_alignParentRight="true"
android:src="@drawable/raw_1500034665"
android:layout_width="50dp"
android:layout_height="wrap_content"/>
</RelativeLayout>
OkHttpUlis OkHttp请求+数据获取路径
okHttpjiexi
package com.example.dengluadd.OkHttpUlis;
import android.os.Handler;
import android.os.Message;
import com.example.dengluadd.model.Loadlisterten;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class okHttpjiexi {
private MyHandler my = new MyHandler();
private Loadlisterten loadListener;
public void get(String url) {
OkHttpClient client = new OkHttpClient();
Request builder = new Request.Builder().url(url).build();
Call call = client.newCall(builder);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Message message = my.obtainMessage();
message.what = 0;
message.obj = "失败";
my.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message message = my.obtainMessage();
String json = response.body().string();
message.what = 1;
message.obj = json;
my.sendMessage(message);
}
});
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
int what = msg.what;
if (what == 0) {
String error = (String) msg.obj;
loadListener.LoadrError(error);
}
if (what == 1) {
String json = (String) msg.obj;
loadListener.LoadSuccess(json);
}
}
}
public void setLoginlistener(Loadlisterten loadListener){
this.loadListener=loadListener;
}
}
HttpConig
package com.example.dengluadd.OkHttpUlis;
public class HttpConig {
public static String login_url="https://www.zhaoapi.cn/user/login";
public static String reg_url="https://www.zhaoapi.cn/user/reg ";
public static String getJokes_url="https://www.zhaoapi.cn/quarter/getJokes";
}
presenter P层
Ipresenter
package com.example.dengluadd.presenter;
import android.content.Context; import com.example.dengluadd.IVew.GoodsView; import com.example.dengluadd.IVew.Iview; import com.example.dengluadd.model.GoodModel; import com.example.dengluadd.model.IModel; public interface Ipresenter { void IPresenterLogin(IModel iModel, Iview vew); void IPresenterreg(IModel iModel, Iview vew); void showGood(Context context, GoodModel goodModel, GoodsView goodsView); }
Ipresenterimp
package com.example.dengluadd.presenter;
import android.content.Context;
import android.util.Log;
import com.example.dengluadd.IVew.GoodsView;
import com.example.dengluadd.IVew.Iview;
import com.example.dengluadd.OkHttpUlis.HttpConig;
import com.example.dengluadd.model.GoodModel;
import com.example.dengluadd.model.GoodUser;
import com.example.dengluadd.model.GoodsLoginlisterten;
import com.example.dengluadd.model.IModel;
import com.example.dengluadd.model.LoginListener;
import com.example.dengluadd.model.RegLoginListenter;
import com.example.dengluadd.model.User;
import com.google.gson.Gson;
import java.util.List;
public class Ipresenterimp implements Ipresenter{
@Override
public void IPresenterLogin(IModel iModel, final Iview vew) {
iModel.loginSuccess(new User(vew.getmobile(), vew.getpassword()), new LoginListener() {
@Override
public void getloginSuccess(String json) {
vew.jump();
}
@Override
public void getloginError(String json) {
vew.showError();
}
});
}
@Override
public void IPresenterreg(IModel iModel, final Iview vew) {
iModel.regSuccess(new User(vew.getmobile(), vew.getpassword()), new RegLoginListenter() {
@Override
public void getregSuccess(String json) {
vew.jump();
}
@Override
public void getlregError(String json) {
vew.showError();
}
});
}
@Override
public void showGood(final Context c, GoodModel goodModel, final GoodsView goodsView) {
goodModel.GoodSuccess(HttpConig.getJokes_url + "?source=android&appVersion=100&page=1", new GoodsLoginlisterten() {
@Override
public void GoodsLoginSuccess(String json) {
Log.e("tag","有数据++++++++++++++++++++");
Gson g=new Gson();
GoodUser goodUser = g.fromJson(json, GoodUser.class);
List<GoodUser.DataBean> data = goodUser.getData();
goodsView.showdate(c,data);
}
@Override
public void GoodsLoginError(String jsons) {
}
});
}
}
model M层
LoginListener
package com.example.dengluadd.model;
public interface LoginListener {
//登录的成功和失败方法
void getloginSuccess(String json);
void getloginError(String json);
}
Loadlisterten
package com.example.dengluadd.model;
public interface Loadlisterten {
//登录的成功和失败方法
void LoadSuccess(String json);
void LoadrError(String json);
}
RegLoginListenter
package com.example.dengluadd.model; public interface RegLoginListenter { //注册成功和失败的方法 void getregSuccess(String json); void getlregError(String json); }
IModel
package com.example.dengluadd.model;
public interface IModel {
void loginSuccess(User user, LoginListener loginListener);
void regSuccess(User user, RegLoginListenter reginListener);
}
ImodelImp
package com.example.dengluadd.model;
import android.util.Log;
import com.example.dengluadd.OkHttpUlis.HttpConig;
import com.example.dengluadd.OkHttpUlis.okHttpjiexi;
import org.json.JSONException;
import org.json.JSONObject;
public class ImodelImp implements IModel {
private static final String TAG = "ImodelImp";
@Override
public void loginSuccess(User user, final LoginListener loginListener) {
okHttpjiexi ok = new okHttpjiexi();
ok.get(HttpConig.login_url + "?mobile=" + user.getMobile() + "&password=" + user.getPassword());
ok.setLoginlistener(new Loadlisterten() {
@Override
public void LoadSuccess(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
String code = jsonObject.getString("code");
if (code.equals("0")) {
loginListener.getloginSuccess(json);
} else {
loginListener.getloginError("登陆失败");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void LoadrError(String json) {
loginListener.getloginSuccess(json);
}
});
}
@Override
public void regSuccess(User user, final RegLoginListenter reginListener) {
okHttpjiexi ok = new okHttpjiexi();
ok.get(HttpConig.reg_url + "?mobile=" + user.getMobile() + "&password=" + user.getPassword());
ok.setLoginlistener(new Loadlisterten() {
@Override
public void LoadSuccess(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
String code = jsonObject.getString("code");
if (code.equals("0")) {
Log.d(TAG, "注册成功");
reginListener.getregSuccess(json);
} else {
Log.d(TAG, "注册失败");
reginListener.getlregError("注册失败");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void LoadrError(String json) {
Log.d(TAG, "注册失败");
reginListener.getlregError(json);
}
});
}
}
GoodsLoginlisterten
package com.example.dengluadd.model; public interface GoodsLoginlisterten { //获取数据的成功和失败方法 void GoodsLoginSuccess(String json); void GoodsLoginError(String jsons); }
GoodModel
package com.example.dengluadd.model; public interface GoodModel { void GoodSuccess(String url, GoodsLoginlisterten goodsLoginlisterten); }
GoodModeImp
package com.example.dengluadd.model;
import android.util.Log;
import com.example.dengluadd.OkHttpUlis.okHttpjiexi;
public class GoodModeImp implements GoodModel{
@Override
public void GoodSuccess(String url, final GoodsLoginlisterten goodsLoginlisterten) {
okHttpjiexi ok = new okHttpjiexi();
ok.get(url);
ok.setLoginlistener(new Loadlisterten() {
@Override
public void LoadSuccess(String json) {
Log.e("tag","有数据++++++++++++++++++++++++++++++++++++");
goodsLoginlisterten.GoodsLoginSuccess(json);
}
@Override
public void LoadrError(String json) {
Log.e("tag","没有数据++++++++++++++++++++++++++++++++++++++");
goodsLoginlisterten.GoodsLoginError(json);
}
});
}
}
GoodUser数据解析
User bean类
GoodsView
package com.example.dengluadd.IVew;
import android.content.Context;
import com.example.dengluadd.model.GoodUser;
import java.util.List;
/**
* Created by lenovo on 2018/1/9.
*/
public interface GoodsView {
void showdate(Context context, List<GoodUser.DataBean> data);
}
Iview
package com.example.dengluadd.IVew;
public interface Iview {
String getmobile();
String getpassword();
void jump();
void showError();
}
Mytitle
package com.example.dengluadd.IVew;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import com.example.dengluadd.R;
public class Mytitle extends RelativeLayout {
public Mytitle(Context context) {
this(context,null);
}
public Mytitle(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public Mytitle(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater from = LayoutInflater.from(context);
from.inflate(R.layout.title_aa, this, true);
}
}
MainActivity
package com.example.dengluadd.IVew;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.dengluadd.R;
import com.example.dengluadd.model.ImodelImp;
import com.example.dengluadd.presenter.Ipresenterimp;
public class MainActivity extends AppCompatActivity implements Iview,View.OnClickListener{
private EditText mobile;
private EditText password;
private Button login;
private Button reg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
mobile = (EditText) findViewById(R.id.tell);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
reg = (Button) findViewById(R.id.zhuce);
login.setOnClickListener(this);
reg.setOnClickListener(this);
}
@Override
public String getmobile() {
return mobile.getText().toString();
}
@Override
public String getpassword() {
return password.getText().toString();
}
@Override
public void jump() {
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
@Override
public void showError() {
Toast.makeText(this, "登陆失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.login:
Ipresenterimp ipresenterimp=new Ipresenterimp();
ipresenterimp.IPresenterLogin(new ImodelImp(),this);
//跳转页面时清空原来内容
mobile.setText("");
password.setText("");
break;
case R.id.zhuce:
startActivity(new Intent(MainActivity.this,Main3Activity.class));
finish();
break;
}
}
}
MyAdapter
package com.example.dengluadd.IVew;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.dengluadd.model.GoodUser;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private Context context;
private List<GoodUser.DataBean> data;
public MyAdapter(Context context, List<GoodUser.DataBean> data) {
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(context);
textView.setText(data.get(position).getContent());
return textView;
}
}
Main2Activity
package com.example.dengluadd.IVew;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import com.example.dengluadd.R;
import com.example.dengluadd.model.GoodModeImp;
import com.example.dengluadd.model.GoodUser;
import com.example.dengluadd.presenter.Ipresenterimp;
import java.util.List;
public class Main2Activity extends AppCompatActivity implements GoodsView {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
lv = (ListView) findViewById(R.id.lv);
Ipresenterimp p=new Ipresenterimp();
p.showGood(this,new GoodModeImp(),this);
Log.e("tag","sadasdas++++++++++++++++++++++++++++++++++++");
}
@Override
public void showdate(Context context, List<GoodUser.DataBean> data) {
if (data==null){
return;
}
Log.e("tag",data.toString()+"sadasdas++++++++++++++++++++++++++++++++++++");
MyAdapter myAdapter = new MyAdapter(context, data);
lv.setAdapter(myAdapter);
}
}
Main3Activity
package com.example.dengluadd.IVew;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.dengluadd.R;
import com.example.dengluadd.model.ImodelImp;
import com.example.dengluadd.presenter.Ipresenterimp;
public class Main3Activity extends AppCompatActivity implements Iview,View.OnClickListener{
private EditText mobile;
private EditText password;
private Button reg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
initViews();
}
private void initViews() {
mobile = (EditText) findViewById(R.id.tell);
password = (EditText) findViewById(R.id.password);
reg = (Button) findViewById(R.id.zhuce);
reg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Ipresenterimp i=new Ipresenterimp();
i.IPresenterreg(new ImodelImp(),this);
}
@Override
public String getmobile() {
return mobile.getText().toString();
}
@Override
public String getpassword() {
return password.getText().toString();
}
@Override
public void jump() {
startActivity(new Intent(Main3Activity.this,MainActivity.class));
finish();
}
@Override
public void showError() {
Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
}
}