//登录页面
package com.example.cll.zhoukaomoni2; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; 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.cll.zhoukaomoni2.bean.Denglu; import com.google.gson.Gson; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; public class MainActivity extends AppCompatActivity implements View.OnClickListener { /** * 输入手机号 */ private EditText mDlname; /** * 输入密码 */ private EditText mDlpwd; /** * 登录 */ private Button mLagin; /** * 注册 */ private Button mZhuce; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mDlname = (EditText) findViewById(R.id.dlname); mDlpwd = (EditText) findViewById(R.id.dlpwd); mLagin = (Button) findViewById(R.id.lagin); mLagin.setOnClickListener(this); mZhuce = (Button) findViewById(R.id.zhuce); mZhuce.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.lagin: String namel = mDlname.getText().toString().trim(); String pwdl = mDlpwd.getText().toString().trim(); String url="http://120.27.23.105/user/login?mobile="+namel+"&password="+pwdl; OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(new HttpLoggingInterceptor()).build(); Request request = new Request.Builder().url(url).build(); httpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } String string=null; @Override public void onResponse(Call call, final Response response) throws IOException { runOnUiThread(new Runnable() { @Override public void run() { try { string = response.body().string(); } catch (IOException e) { e.printStackTrace(); } Denglu denglu = new Gson().fromJson(string, Denglu.class); Toast.makeText(MainActivity.this, denglu.getMsg(), Toast.LENGTH_SHORT).show(); if (denglu.getCode().equals("0")){ int uid = denglu.getData().getUid(); Intent intent = new Intent(MainActivity.this,GerenyemianActivity.class); intent.putExtra("name",denglu.getData().getUsername()); intent.putExtra("password",denglu.getData().getPassword()); intent.putExtra("UID",uid); startActivity(intent); MainActivity.this.finish(); } } }); } }); break; case R.id.zhuce: Intent intent = new Intent(MainActivity.this, ZhuceActivity.class); startActivity(intent); finish(); break; } } } //登录布局//个人页面<?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" android:gravity="center" tools:context="com.example.cll.zhoukaomoni2.MainActivity"> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/dlname" android:hint="输入手机号" /> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/dlpwd" android:hint="输入密码"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:id="@+id/lagin"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:id="@+id/zhuce"/> </LinearLayout> </LinearLayout>//注册页面
package com.example.cll.zhoukaomoni2; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; 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.cll.zhoukaomoni2.bean.User; import com.example.cll.zhoukaomoni2.bean.Zhuce; import com.google.gson.Gson; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; public class ZhuceActivity extends AppCompatActivity implements View.OnClickListener { /** * 输入手机号 */ private EditText mZcname; /** * 输入密码 */ private EditText mZcpwd; /** * 注册 */ private Button mZcbut; private User user; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zhuce); initView(); } private void initView() { mZcname = (EditText) findViewById(R.id.zcname); mZcpwd = (EditText) findViewById(R.id.zcpwd); mZcbut = (Button) findViewById(R.id.zcbut); mZcbut.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.zcbut: String name = mZcname.getText().toString(); String pwd = mZcpwd.getText().toString(); String url="http://120.27.23.105/user/reg?mobile="+name+"&password="+pwd; OkHttpClient builder = new OkHttpClient.Builder().addInterceptor(new HttpLoggingInterceptor()).build(); Request request = new Request.Builder().url(url).build(); builder.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, final Response response) throws IOException { runOnUiThread(new Runnable() { private String string; @Override public void run() { try { string = response.body().string(); } catch (IOException e) { e.printStackTrace(); } Zhuce zhuce = new Gson().fromJson(string, Zhuce.class); Toast.makeText(ZhuceActivity.this, zhuce.getMsg(), Toast.LENGTH_SHORT).show(); Intent intent = new Intent(ZhuceActivity.this,MainActivity.class); startActivity(intent); ZhuceActivity.this.finish(); } }); } }); break; } } }
//注册布局<?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" android:gravity="center" tools:context="com.example.cll.zhoukaomoni2.ZhuceActivity"> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/zcname" android:hint="输入手机号" /> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/zcpwd" android:hint="输入密码"/> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:id="@+id/zcbut" android:text="注册"/> </LinearLayout>
package com.example.cll.zhoukaomoni2; 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.TextView; import android.widget.Toast; import com.example.cll.zhoukaomoni2.bean.Geren; import com.google.gson.Gson; import java.io.IOException; import java.util.ArrayList; import java.util.List; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; public class GerenyemianActivity extends AppCompatActivity implements View.OnClickListener { private TextView mUser; /** * kson */ private TextView mName; /** * 退出登录 */ private Button mFinish; /** * 跳转到商品搜索页 */ private Button mShopping; private String string; private Geren geren; private List<Geren> list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gerenyemian); initView(); Intent intent = getIntent(); int uid = intent.getIntExtra("UID",0); String url="http://120.27.23.105/user/getUserInfo?uid="+uid; OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(new HttpLoggingInterceptor()).build(); Request request = new Request.Builder().url(url).build(); httpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, final Response response) throws IOException { runOnUiThread(new Runnable() { @Override public void run() { try { string = response.body().string(); } catch (IOException e) { e.printStackTrace(); } geren = new Gson().fromJson(string, Geren.class); list.add(geren); } }); } }); } private void initView() { mUser = (TextView) findViewById(R.id.user); mName = (TextView) findViewById(R.id.name); mFinish = (Button) findViewById(R.id.finish); mFinish.setOnClickListener(this); mShopping = (Button) findViewById(R.id.shopping); mShopping.setOnClickListener(this); String name = getIntent().getStringExtra("name"); String password = getIntent().getStringExtra("password"); mUser.setText(name); mName.setText(password); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.finish: finish(); break; case R.id.shopping: Intent intent = new Intent(GerenyemianActivity.this,ShoppingActivity.class); startActivity(intent); break; } } }
//个人布局
<?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.cll.zhoukaomoni2.GerenyemianActivity"> <TextView android:gravity="center" android:textSize="20sp" android:text="个人信息" android:layout_width="match_parent" android:layout_height="50dp" /> <View android:background="#33000000" android:layout_width="match_parent" android:layout_height="1dp"></View> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:textSize="16sp" android:paddingLeft="15dp" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="头像"/> <ImageView android:layout_centerVertical="true" android:layout_alignParentRight="true" android:paddingRight="15dp" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> <View android:background="#33000000" android:layout_width="match_parent" android:layout_height="1dp"></View> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:textSize="16sp" android:paddingLeft="15dp" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名"/> <TextView android:textSize="15sp" android:id="@+id/user" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:paddingRight="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> <View android:background="#33000000" android:layout_width="match_parent" android:layout_height="1dp"></View> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:textSize="16sp" android:paddingLeft="15dp" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="昵称"/> <TextView android:textSize="15sp" android:id="@+id/name" android:text="kson" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:paddingRight="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> <View android:background="#33000000" android:layout_width="match_parent" android:layout_height="1dp"></View> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/finish" android:text="退出登录" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/shopping" android:text="跳转到商品搜索页" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> </LinearLayout>
//数据页面
package com.example.cll.zhoukaomoni2; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.example.cll.zhoukaomoni2.bean.ShoppingBean; import com.google.gson.Gson; import java.io.IOException; import java.util.ArrayList; import java.util.List; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; public class ShoppingActivity extends AppCompatActivity implements View.OnClickListener { /** * 输入搜索关键词 */ private EditText mEt; /** * 查找 */ private TextView mFind; /** * 切换 */ private TextView mChange; private RecyclerView mRv; private boolean flag; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 1: String string = (String) msg.obj; Toast.makeText(ShoppingActivity.this,string,Toast.LENGTH_LONG).show(); ShoppingBean shangpinbean = new Gson().fromJson(string, ShoppingBean.class); list= shangpinbean.getData(); show(true); break; } } }; private List<ShoppingBean.DataBean> list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shopping); initView(); } private void initView() { mEt = (EditText) findViewById(R.id.et); mFind = (TextView) findViewById(R.id.find); mChange = (TextView) findViewById(R.id.change); mRv = (RecyclerView) findViewById(R.id.rv); mFind.setOnClickListener(this); mChange.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.find: String s = mEt.getText().toString().trim(); String url="http://120.27.23.105/product/searchProducts?keywords=" + s + "&page=1"; OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(new HttpLoggingInterceptor()).build(); Request request = new Request.Builder().url(url).build(); httpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); Message message = new Message(); message.what=1; message.obj =string; handler.sendMessage(message); } }); break; case R.id.change: if(!flag){ show(false); flag=true; }else{ show(true); flag=false; } break; } } public void show(boolean flag){ mRv.setLayoutManager(flag ? new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false) : new GridLayoutManager(this,2)); MyAdapter adapter = new MyAdapter(this,list); mRv.setAdapter(adapter); } }//数据布局
<?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.cll.zhoukaomoni2.ShoppingActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp"> <EditText android:id="@+id/et" android:background="#ff3660" android:hint="输入搜索关键词" android:textColorHint="#fff" android:layout_gravity="center" android:layout_width="300dp" android:layout_height="50dp" /> <TextView android:id="@+id/find" android:text="查找" android:textSize="20sp" android:paddingRight="60dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/change" android:text="切换" android:textSize="20sp" android:paddingRight="5dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </LinearLayout>