导依赖
compile 'org.xutils:xutils:3.5.0 compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.bigkoo:convenientbanner:2.0.5'
加权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Main布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:id="@+id/textView2" /> <EditText android:id="@+id/editText" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="请输入手机号" android:layout_marginTop="81dp" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" /> <EditText android:id="@+id/editText1" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="请输入密码" android:layout_marginTop="20dp" android:layout_below="@+id/editText" android:layout_alignLeft="@+id/editText" android:layout_alignStart="@+id/editText" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="18dp" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true"> <Button android:id="@+id/dl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" /> <Button android:id="@+id/zc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" /> </LinearLayout> </RelativeLayout>
Mainactivity
package com.example.denlulication; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.google.gson.Gson; import org.xutils.common.Callback; import org.xutils.http.RequestParams; import org.xutils.x; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText ed1; private EditText ed2; private Button dl; private Button zc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setOnClick(); } private void setOnClick() { dl.setOnClickListener(this); zc.setOnClickListener(this); } private void initView() { ed1= (EditText) findViewById(R.id.editText); ed2 = (EditText) findViewById(R.id.editText1); dl= (Button) findViewById(R.id.dl); zc = (Button) findViewById(R.id.zc); } @Override public void onClick(View view) { String mobile = ed1.getText().toString().trim(); String password = ed2.getText().toString().trim(); switch (view.getId()){ case R.id.dl: boolean flag=checkData(mobile,password); if (flag){ register(mobile,password); } break; case R.id.zc: Intent intent = new Intent(this, Main2Activity.class); startActivity(intent); break; } } private void register(String mobile, String password) { RequestParams params = new RequestParams("http://120.27.23.105/user/login"); params.addQueryStringParameter("mobile",mobile); params.addQueryStringParameter("password",password); x.http().get(params,new Callback.CommonCallback<String>(){ @Override public void onSuccess(String result) { Gson gson = new Gson(); LoginBean loginBean = gson.fromJson(result, LoginBean.class); if (loginBean.getCode().equals("0")){ Intent intent = new Intent(MainActivity.this, GoodsActivity.class); startActivity(intent); } } @Override public void onError(Throwable ex, boolean isOnCallback) { Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_SHORT).show(); } @Override public void onCancelled(CancelledException cex) { Toast.makeText(x.app(), "cancelled", Toast.LENGTH_SHORT).show(); } @Override public void onFinished() { } }); } private boolean checkData(String mobile, String password) { if(TextUtils.isEmpty(mobile)||TextUtils.isEmpty(password)){ Toast.makeText(this,"不能为空",Toast.LENGTH_SHORT).show(); return false; } if (password.length()<6){ Toast.makeText(this,"密码长度不能小于6",Toast.LENGTH_SHORT).show(); return false; } return true; } }注册页面布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:id="@+id/textView" /> <EditText android:id="@+id/editText" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="请输入手机号" android:layout_marginTop="107dp" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> <EditText android:id="@+id/editText1" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="请输入密码" android:layout_marginTop="17dp" android:layout_below="@+id/editText" android:layout_alignLeft="@+id/editText" android:layout_alignStart="@+id/editText" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="109dp" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:id="@+id/linearLayout"> </LinearLayout> <Button android:id="@+id/zhu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="29dp" android:text="立即注册" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" /> </RelativeLayout>
注册mainactivity
package com.example.denlulication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.google.gson.Gson; import org.xutils.common.Callback; import org.xutils.http.RequestParams; import org.xutils.x; public class Main2Activity extends AppCompatActivity { private Button zhu; private EditText ed1; private EditText ed2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); ed1 = (EditText) findViewById(R.id.editText); ed2 = (EditText) findViewById(R.id.editText1); zhu = (Button) findViewById(R.id.zhu); zhu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String mobile= ed1.getText().toString().trim(); String password= ed2.getText().toString().trim(); switch (view.getId()){ case R.id.zhu: boolean flag=checkData(mobile,password); if(flag){ register(mobile,password); } break; } } }); } private boolean checkData(String mobile, String password) { if (TextUtils.isEmpty(mobile)||TextUtils.isEmpty(password)){ Toast.makeText(this,"不能为空",Toast.LENGTH_SHORT).show(); return false; } if (password.length()<6){ Toast.makeText(this,"密码不能小于6位",Toast.LENGTH_SHORT).show(); return false; } return true; }; private void register(String mobile, String password) { RequestParams params = new RequestParams("http://120.27.23.105/user/reg"); params.addQueryStringParameter("mobile",mobile); params.addQueryStringParameter("password",password); x.http().get(params, new Callback.CommonCallback<String>( ) { @Override public void onSuccess(String result) { Gson gson = new Gson(); RegistBean registBean = gson.fromJson(result, RegistBean.class); Toast.makeText(Main2Activity.this,registBean.getMsg(),Toast.LENGTH_SHORT).show(); if(registBean.getCode().equals("0")){ finish(); } } @Override public void onError(Throwable ex, boolean isOnCallback) { Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_SHORT).show(); } @Override public void onCancelled(CancelledException cex) { Toast.makeText(x.app(), "cancelled", Toast.LENGTH_SHORT).show(); } @Override public void onFinished() { } }); } }
LoginBean
package com.example.denlulication; /** * Created by lenovo on 2017/11/1. */ public class LoginBean { private String msg; private String code; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
RegistBean
package com.example.denlulication; /** * Created by lenovo on 2017/11/1. */ public class RegistBean { private String msg; private String code; private String data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getData() { return data; } public void setData(String data) { this.data = data; } }
MyApplication
package com.example.denlulication; import android.app.Application; import org.xutils.x; /** * Created by SDC on 2017/10/31. */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能. } }
GoodsActivity
package com.example.denlulication; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.ImageView; import android.widget.Toast; import com.bigkoo.convenientbanner.ConvenientBanner; import com.bigkoo.convenientbanner.holder.CBViewHolderCreator; import com.bigkoo.convenientbanner.holder.Holder; import com.bigkoo.convenientbanner.listener.OnItemClickListener; import com.bumptech.glide.Glide; import com.google.gson.Gson; import org.xutils.common.Callback; import org.xutils.http.RequestParams; import org.xutils.x; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Created by lenovo on 2017/11/1. */ public class GoodsActivity extends AppCompatActivity{ private String[] images = {"http://120.27.23.105/images/ad/0.jpg", "http://120.27.23.105/images/ad/1.jpg", "http://120.27.23.105/images/ad/2.jpg", "http://120.27.23.105/images/ad/3.jpg", }; private int[] indicator = {R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused}; private ConvenientBanner convenientBanner; private List<String> bean = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_goods); register(); convenientBanner = (ConvenientBanner) findViewById(R.id.convenientBanner); bean= Arrays.asList(images); convenientBanner.setPointViewVisible(true) //设置小点 .setPageIndicator(indicator); //允许手动轮播 convenientBanner.setManualPageable(true); //设置自动轮播的时间 convenientBanner.startTurning(3000); //设置点击事件 //泛型为具体实现类ImageLoaderHolder convenientBanner.setPages(new CBViewHolderCreator<NetImageLoadHolder>() { @Override public NetImageLoadHolder createHolder() { return new NetImageLoadHolder(); } }, bean); //设置每个pager的点击事件 convenientBanner.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(int position) { Toast.makeText(GoodsActivity.this, "点击了" + convenientBanner.getCurrentItem(), Toast.LENGTH_SHORT).show(); } }); } public class NetImageLoadHolder implements Holder<String> { private ImageView image_lv; //可以是一个布局也可以是一个Imageview @Override public ImageView createView(Context context) { image_lv = new ImageView(context); image_lv.setScaleType(ImageView.ScaleType.FIT_XY); return image_lv; } @Override public void UpdateUI(Context context, int position, String data) { Glide.with(context).load(data).placeholder(R.mipmap.ic_launcher).into(image_lv); } } private void register() { RequestParams params = new RequestParams("http://120.27.23.105/ad/getAd"); // params.setSslSocketFactory(...); // 设置ssl // params.addQueryStringParameter("mobile", mobile); // params.addQueryStringParameter("password", password); x.http().get(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { Gson gson = new Gson(); GoodsBean goodsBean = gson.fromJson(result, GoodsBean.class); List<GoodsBean.DataBean> data = goodsBean.getData(); // Log.i("zxz","123"+data.toString()); for (int i=0 ;i<=20;i++){ String icon = data.get(i).getIcon().toString(); // bean.add(icon); // images[0] = "http://120.27.23.105/images/ad/0.jpg"; // images[1] = "http://120.27.23.105/images/ad/1.jpg"; // images[2] = "http://120.27.23.105/images/ad/2.jpg"; // images[3] = "http://120.27.23.105/images/ad/3.jpg"; Log.i("zxz",icon); // bean.add(icon); // Log.i("zxz",bean.toString()); } } @Override public void onError(Throwable ex, boolean isOnCallback) { Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_SHORT).show(); Log.i("zxz","onsuccess"+ex.getMessage()); } @Override public void onCancelled(CancelledException cex) { Toast.makeText(x.app(), "cancelled", Toast.LENGTH_SHORT).show(); } @Override public void onFinished() { } }); } }
GoodsBean
package com.example.denlulication; import java.util.List; /** * Created by lenovo on 2017/11/1. */ public class GoodsBean { private String msg; private String code; private TuijianBean tuijian; private MiaoshaBean miaosha; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public TuijianBean getTuijian() { return tuijian; } public void setTuijian(TuijianBean tuijian) { this.tuijian = tuijian; } public MiaoshaBean getMiaosha() { return miaosha; } public void setMiaosha(MiaoshaBean miaosha) { this.miaosha = miaosha; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class TuijianBean { private String name; private List<ListBean> list; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> list) { this.list = list; } public static class ListBean { private double bargainPrice; private String createtime; private String detailUrl; private String images; private int itemtype; private int pid; private double price; private int pscid; private int salenum; private int sellerid; private String subhead; private String title; public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getItemtype() { return itemtype; } public void setItemtype(int itemtype) { this.itemtype = itemtype; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSalenum() { return salenum; } public void setSalenum(int salenum) { this.salenum = salenum; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } } public static class MiaoshaBean { private String name; private int time; private List<ListBeanX> list; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } public List<ListBeanX> getList() { return list; } public void setList(List<ListBeanX> list) { this.list = list; } public static class ListBeanX { private double bargainPrice; private String createtime; private String detailUrl; private String images; private int itemtype; private int pid; private double price; private int pscid; private int salenum; private int sellerid; private String subhead; private String title; public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getItemtype() { return itemtype; } public void setItemtype(int itemtype) { this.itemtype = itemtype; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSalenum() { return salenum; } public void setSalenum(int salenum) { this.salenum = salenum; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } } public static class DataBean { private int aid; private String createtime; private String icon; private Object productId; private String title; private int type; private String url; public int getAid() { return aid; } public void setAid(int aid) { this.aid = aid; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public Object getProductId() { return productId; } public void setProductId(Object productId) { this.productId = productId; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } } }