登录注册

来源:互联网 发布:淘宝买家怎么删除评论 编辑:程序博客网 时间:2024/04/29 13:36




Okhttp


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


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


public interface CallBack {    void onFailure(String message);    void onSuccess(String str);}



ShopModl


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



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



public interface Shoppreinter {    void onFailure(String message);    void onSuccess(ShopBean str);}




ShopView


public interface ShopView {    void onFailuer(String message);    void onSuccess(ShopBean str);}



LoginModel


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



public interface Logview {    void onFailuer(String s);    void onSuccess(String str);}


public interface Logview {    void onFailuer(String s);    void onSuccess(String str);}

Precenter

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);    }}


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();         }    }}



Presenterinterface


public interface Presenterinterface {    void onFailuer(String s);    void onSuccess(String str);}

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


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
<?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>
2
<?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><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入手机号"android:id="@+id/edphonetwo"    /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入密码"android:id="@+id/edpasstwo"    /><LinearLayoutandroid: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>
3
<?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>
4


<?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>


原创粉丝点击