Utils类
Okhttp
package com.example.lxs.Utils;
import android.os.Handler;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* author:Created by WangZhiQiang on 2017/12/11.
*/
public class Okhttp {
private Handler handler =new Handler();
private String str;
private static Okhttp okhttp=null;
public Okhttp(){
}
public static Okhttp getInster(){
if (okhttp==null){
synchronized (Okhttp.class){
okhttp=new Okhttp();
}
}
return okhttp;
}
public void doGet(String path, Map<String ,String>map, final CallBack callBack){
StringBuffer sb=null;
for (String key:map.keySet()){
if (sb==null){
sb= new StringBuffer();
sb.append("?");
}else {
sb.append("&");
}
sb.append(key).append("=").append(map.get(key));
}
OkHttpClient okHttpClient=new OkHttpClient();
Request request=new Request
.Builder()
.url(path+sb.toString())
.get()
.build();
Call call=okHttpClient.newCall(request);
call.enqueue(new okhttp3.Callback(){
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
callBack.onFailure(e.getMessage());
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
str=response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
callBack.onSuccess(str);
}
});
}
});
}
public static void doPost(String path,Map<String,String>map ,Callback Call){
//构建参数
FormBody.Builder builder=new FormBody.Builder();
for (String key:map.keySet()){
builder.add(key,map.get(key));
}
Request request =new Request.Builder()
.post(builder.build())
.url(path)
.build();
OkHttpClient okHttpClient=new OkHttpClient();
}
}
Contanst
package com.example.lxs.Utils;
/**
* author:Created by WangZhiQiang on 2017/12/11.
*/
public class Contanst {
public static final String LOGIN="http://120.27.23.105/user/login";
public static final String REG="http://120.27.23.105/user/reg";
public static final String SHOP="http://120.27.23.105/product/getProducts";
}
CallBack
package com.example.lxs.Utils;
/**
* author:Created by WangZhiQiang on 2017/12/11.
*/
public interface CallBack {
void onFailure(String message);
void onSuccess(String str);
}
Model层
LoginModel
package com.example.lxs.m;
import android.text.TextUtils;
import android.util.Log;
import com.example.lxs.Utils.CallBack;
import com.example.lxs.Utils.Contanst;
import com.example.lxs.Utils.Okhttp;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* author:Created by WangZhiQiang on 2017/12/11.
*/
public class LoginModel {
public void vertily(String phone,String pass, int i, final Presenterinterface presenterinterface){
if (TextUtils.isEmpty(phone)){
presenterinterface.onFailuer("手机及号码不为空");
return;
}
boolean mobile=isMobile(phone);
if (!mobile){
presenterinterface.onFailuer("手机号格式不对");
return;
}
if (TextUtils.isEmpty(pass)){
presenterinterface.onFailuer("密码不能为空");
return;
}
if (pass.length()<6){
presenterinterface.onFailuer("密码不能小于六位数");
return;
}
Map<String,String> map=new HashMap<>();
map.put("mobile",phone);
map.put("password",pass);
if (i==1){
Okhttp.getInster().doGet(Contanst.LOGIN, map, new CallBack() {
@Override
public void onFailure(String message) {
presenterinterface.onFailuer(message);
}
@Override
public void onSuccess(String str) {
try {
JSONObject jsonObject=new JSONObject(str);
String code = jsonObject.optString("code");
if (code.equals("0")){
Log.e("Tag","5555");
presenterinterface.onSuccess("成功");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
else {
Okhttp.getInster().doGet(Contanst.REG, map, new CallBack() {
@Override
public void onFailure(String message) {
presenterinterface.onFailuer(message);
}
@Override
public void onSuccess(String str) {
try {
JSONObject object= new JSONObject(str);
String code = object.optString("code");
if (code.equals("0")){
presenterinterface.onSuccess("成功");
}
else {
presenterinterface.onSuccess("已经注册");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
public static boolean isMobile(String number) {
/*
移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
联通:130、131、132、152、155、156、185、186
电信:133、153、180、189、(1349卫通)
总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9*/
String num="[1][358]\\d{9}";
if (TextUtils.isEmpty(number)){
return false;
}else {
return number.matches(num);
}
}
}
Logview
package com.example.lxs.m;
/**
* author:Created by WangZhiQiang on 2017/12/11.
*/
public interface Logview {
void onFailuer(String s);
void onSuccess(String str);
}
Precenter
package com.example.lxs.m;
/**
* author:Created by WangZhiQiang on 2017/12/11.
*/
public class Precenter implements Presenterinterface {
private Logview logview;
private LoginModel loginModel;
public Precenter(Logview logview) {
this.logview = logview;
loginModel=new LoginModel();
}
public void vertify(String phone,String pass,int i){
loginModel.vertily(phone,pass,i,this);
}
@Override
public void onFailuer(String s) {
logview.onFailuer(s);
}
@Override
public void onSuccess(String str) {
logview.onSuccess(str);
}
}
Presenterinterface
package com.example.lxs.m;
/**
* author:Created by WangZhiQiang on 2017/12/11.
*/
public interface Presenterinterface {
void onFailuer(String s);
void onSuccess(String str);
}
P层
ShopModl
package com.example.lxs.p;
import com.example.lxs.Utils.CallBack;
import com.example.lxs.Utils.Contanst;
import com.example.lxs.Utils.Okhttp;
import com.example.lxs.shopmodel.Bean.ShopBean;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
/**
* author:Created by WangZhiQiang on 2017/12/11.
*/
public class ShopModl {
public void vertify(int page, final Shoppreinter shoppreinter){
Map<String ,String> map=new HashMap<>();
map.put("pscid",39+"");
map.put("page",page+"");
Okhttp.getInster().doGet(Contanst.SHOP, map, new CallBack() {
@Override
public void onFailure(String message) {
shoppreinter.onFailure(message);
}
@Override
public void onSuccess(String str) {
ShopBean shopBean=new Gson().fromJson(str,ShopBean.class);
shoppreinter.onSuccess(shopBean);
}
});
}
}
ShopPre
package com.example.lxs.p;
import com.example.lxs.shopmodel.Bean.ShopBean;
/**
* author:Created by WangZhiQiang on 2017/12/12.
*/
public class ShopPre implements Shoppreinter {
private ShopView shopView;
private ShopModl shopModl;
public ShopPre(ShopView shopView) {
this.shopView = shopView;
shopModl=new ShopModl();
}
public void vertify(int i){
shopModl.vertify(i,this);
}
@Override
public void onFailure(String message) {
shopView.onFailuer(message);
}
@Override
public void onSuccess(ShopBean str) {
shopView.onSuccess(str);
}
}
Shoppreinter
package com.example.lxs.p;
import com.example.lxs.shopmodel.Bean.ShopBean;
/**
* author:Created by WangZhiQiang on 2017/12/11.
*/
public interface Shoppreinter {
void onFailure(String message);
void onSuccess(ShopBean str);
}
ShopView
package com.example.lxs.p;
import com.example.lxs.shopmodel.Bean.ShopBean;
/**
* author:Created by WangZhiQiang on 2017/12/12.
*/
public interface ShopView {
void onFailuer(String message);
void onSuccess(ShopBean str);
}
View层
Main2Activity
package com.example.lxs;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.lxs.m.Logview;
import com.example.lxs.m.Precenter;
public class Main2Activity extends AppCompatActivity implements Logview {
private EditText phone;
private EditText pass;
private Precenter precenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
phone = (EditText) findViewById(R.id.edphonetwo);
pass = (EditText) findViewById(R.id.edpasstwo);
precenter=new Precenter(this);
}
public void Zctwo(View view){
precenter.vertify(phone.getText().toString(),pass.getText().toString(),2);
}
@Override
public void onFailuer(String s) {
Toast.makeText(Main2Activity.this,s,Toast.LENGTH_LONG).show();
}
@Override
public void onSuccess(String s) {
Toast.makeText(Main2Activity.this,s,Toast.LENGTH_LONG).show();
if (s.equals("成功")){
finish();
}
}
}
MainActivity
package com.example.lxs;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.lxs.m.Logview;
import com.example.lxs.m.Precenter;
public class MainActivity extends AppCompatActivity implements Logview {
private EditText phone;
private EditText pass;
private Precenter precenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phone = (EditText) findViewById(R.id.edphone);
pass = (EditText) findViewById(R.id.edpass);
precenter=new Precenter(this);
}
public void DI(View view){
precenter.vertify(phone.getText().toString(),pass.getText().toString(),1);
}
public void Zc(View view){
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
@Override
public void onFailuer(String s) {
Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show();
}
@Override
public void onSuccess(String str) {
Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,MainshopActivity.class);
startActivity(intent);
}
}
MainshopActivity
package com.example.lxs;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.widget.Toast;
import com.example.lxs.shopmodel.Bean.ShopBean;
import com.example.lxs.p.ShopPre;
import com.example.lxs.p.ShopView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainshopActivity extends AppCompatActivity implements ShopView{
List<ShopBean.DataBean> list=new ArrayList<>();
int lastVisibleItem;
private SwipeRefreshLayout swipe;
private int i = 0;
private MyAdapter adapter;
private XRecyclerView recycler;
private ShopPre shoppre;
private LinearLayoutManager linearLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainshop);
recycler=(XRecyclerView) findViewById(R.id.recycler);
recycler.setPullRefreshEnabled(true);
recycler.setLoadingMoreEnabled(true);
recycler.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
shoppre= new ShopPre(this);
shoppre.vertify(0);
recycler.setLoadingListener(new XRecyclerView.LoadingListener(){
@Override
public void onRefresh() {
list.clear();
i=0;
shoppre.vertify(i);
recycler.refreshComplete();
adapter.notifyDataSetChanged();
}
@Override
public void onLoadMore() {
i++;
shoppre.vertify(i);
recycler.loadMoreComplete();
adapter.notifyDataSetChanged();
}
});
}
@Override
public void onFailuer(String message) {
Toast.makeText(MainshopActivity.this,message,Toast.LENGTH_LONG).show();
}
public void onSuccess(ShopBean str) {
List<ShopBean.DataBean> data=str.getData();
list.addAll(data);
Log.e("aaa","aaa"+list.size());
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recycler.setLayoutManager(linearLayoutManager);
adapter = new MyAdapter(MainshopActivity.this, list);
recycler.setAdapter(adapter);
}
}
MyAdapter
package com.example.lxs;
import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.lxs.shopmodel.Bean.ShopBean;
import java.util.List;
/**
* author:Created by WangZhiQiang on 2017/12/12.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
private Context context;
private List<ShopBean.DataBean> list;
public MyAdapter(Context context, List<ShopBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = View.inflate(context, R.layout.adapter, null);
MyViewHolder myAdapter = new MyViewHolder(inflate);
return myAdapter;
}
@Override
public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
String images = list.get(position).getImages();
String[] split = images.split("\\|");
Glide.with( context ).load(split[0]).into(holder.iv );
holder.t1.setText(list.get(position).getTitle());
holder.t2.setText("原价:¥"+list.get(position).getPrice()+"");
holder.t2.setPaintFlags(holder.t2.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
holder.t3.setText("优惠价:¥"+list.get(position).getBargainPrice()+"");
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView t1;
TextView t2;
TextView t3;
ImageView iv;
public MyViewHolder(View itemView) {
super(itemView);
iv = itemView.findViewById(R.id.iv);
t1 = itemView.findViewById(R.id.t1);
t2 = itemView.findViewById(R.id.t2);
t3 = itemView.findViewById(R.id.t3);
}
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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.example.lxs.MainActivity">
<TextView
android:layout_gravity="center"
android:text="登录"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />
<EditText
android:id="@+id/edphone"
android:hint="请输入手机号"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/edpass"
android:layout_alignLeft="@+id/edpass"
android:layout_alignStart="@+id/edpass"
android:layout_marginBottom="43dp" />
<EditText
android:id="@+id/edpass"
android:hint="请输入密码"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout"
android:layout_alignLeft="@+id/linearLayout"
android:layout_alignStart="@+id/linearLayout"
android:layout_marginBottom="86dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginBottom="114dp"
android:id="@+id/linearLayout">
<Button
android:text="登录"
android:onClick="DI"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:onClick="Zc"
android:text="注册"
android:layout_weight="1"
android:id="@+id/Zc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
<?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="com.example.lxs.Main2Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/icon_back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="150dp"
android:text="注册" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号"
android:id="@+id/edphonetwo"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:id="@+id/edpasstwo"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Zctwo"
android:text="立即注册"
/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.example.lxs.MainshopActivity">
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView>
</RelativeLayout>
<?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"
>
<ImageView
android:id="@+id/iv"
android:layout_width="150dp"
android:layout_height="120dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/t1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/t2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/t3"
android:textColor="#ff0000"
/>
</LinearLayout>
</LinearLayout>
还有Bean类
compile files('libs/gson-2.2.4.jar')
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
testCompile 'junit:junit:4.12'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'