s註冊登入商品展示
阿新 • • 發佈:2019-02-20
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.rookie.lymth.test2.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="登入"
android:textSize="25sp" />
<View
android:layout_width ="match_parent"
android:layout_height="0.75dp"
android:background="#999999" />
<EditText
android:id="@+id/mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="手機號" />
<EditText
android:id ="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密碼" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登入" />
<Button
android:id="@+id/reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="註冊" />
</LinearLayout>
</LinearLayout>
activity_reg.xml
<?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"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="註冊"
android:textSize="25sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.75dp"
android:background="#999999" />
<EditText
android:id="@+id/mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="手機號" />
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密碼" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="註冊" />
</LinearLayout>
</LinearLayout>
goods_list_layout.xml
<?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"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="商品列表"
android:textSize="25sp" />
<ImageView
android:id="@+id/goodslist_change"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:src="@drawable/grid_icon" />
</FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.75dp"
android:background="#999999" />
<ListView
android:visibility="visible"
android:id="@+id/goodslist_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
<GridView
android:id="@+id/goodslist_gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:visibility="gone"></GridView>
</LinearLayout>
item_layout.xml
<?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"
android:orientation="horizontal">
<ImageView
android:id="@+id/item_pic"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:singleLine="true"
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="商品名" />
</LinearLayout>
item_layout_gridview.xml
<?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"
android:orientation="vertical">
<ImageView
android:id="@+id/item_pic"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:singleLine="true"
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="商品名" />
</LinearLayout>
http包
HttpConfig
public class HttpConfig {
//登入介面
public static String login_url = "http://120.27.23.105/user/login";
// 註冊
public static String reg_url = "http://120.27.23.105/user/reg";
// 商品列表介面
public static String goods_list_url = "http://120.27.23.105/product/searchProducts";
}
HttpUtils
public class HttpUtils {
private static final String TAG = "HttpUtils-----";
private static HttpUtils httpUtils;
private final int SUCCESS = 0;
private final int ERROR = 1;
private MyHandler myHandler = new MyHandler();
private OkLoadListener okLoadListener;
public static HttpUtils getHttpUtils() {
if (httpUtils == null) {
httpUtils = new HttpUtils();
}
return httpUtils;
}
//Handler處理執行緒
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SUCCESS:
//成功
String json = (String) msg.obj;
okLoadListener.okLoadSuccess(json);
break;
case ERROR:
//失敗
String error = (String) msg.obj;
okLoadListener.okLoadError(error);
break;
}
}
}
//get
public void okGet(String url) {
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyIntercepter()).build();
Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Message message = myHandler.obtainMessage();
message.what = ERROR;
message.obj = e.getMessage();
myHandler.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message message = myHandler.obtainMessage();
message.what = SUCCESS;
message.obj = response.body().string();
myHandler.sendMessage(message);
}
});
}
public void setOkLoadListener(OkLoadListener okLoadListener) {
this.okLoadListener = okLoadListener;
}
//post
public void okPost(String url, Map<String, String> params) {
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyIntercepter()).build();
FormBody.Builder builder = new FormBody.Builder();
Set<String> keySet = params.keySet();
for (String key :
keySet) {
String value = params.get(key);
builder.add(key, value);
}
FormBody formBody = builder.build();
Request request = new Request.Builder().url(url).post(formBody).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Message message = myHandler.obtainMessage();
message.what = ERROR;
message.obj = e.getMessage();
myHandler.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message message = myHandler.obtainMessage();
message.what = SUCCESS;
message.obj = response.body().string();
myHandler.sendMessage(message);
}
});
}
//攔截器
class MyIntercepter implements Interceptor {
//intercept 攔截
@Override
public Response intercept(Chain chain) throws IOException {
//新增公共引數
//post 取出原來所有的引數,將之加到新的請求體裡面。然後讓請求去執行
Request request = chain.request();
//獲取請求方法
String method = request.method();
if (method.equals("GET")) {//---------------------------GET 攔截
//取出url地址
String url = request.url().toString();
//拼接公共引數
boolean contains = url.contains("?");
if (contains) {
url = url + "&source=android";
} else {
url = url + "?source=android";
}
Request request1 = request.newBuilder().url(url).build();
Response response = chain.proceed(request1);
return response;
} else if (method.equals("POST")) {//---------------------POST 攔截
RequestBody body = request.body();//請求體
if (body instanceof FormBody) {
//建立新的請求體
FormBody.Builder newBuilder = new FormBody.Builder();
for (int i = 0; i < ((FormBody) body).size(); i++) {
String key = ((FormBody) body).name(i);
String value = ((FormBody) body).value(i);
newBuilder.add(key, value);
}
//新增公共引數
newBuilder.add("source", "android");
FormBody newBody = newBuilder.build();
//建立新的請求體
Request request1 = request.newBuilder().post(newBody).build();
//去請求
Response response = chain.proceed(request1);
return response;
}
}
return null;
}
}
//上傳檔案(圖片)
public void upLoadImage(String url, String path) {//url 要上傳的地址。path 要上傳的檔案路徑
//媒體型別
MediaType mediaType = MediaType.parse("image/*");
//multipartbody
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
File file = new File(path);
MultipartBody multipartBody = builder.addFormDataPart("file", file.getName(), RequestBody.create(mediaType, file)).build();
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).post(multipartBody).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "上傳失敗0----: ");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d(TAG, "上傳成功: ");
}
});
}
}
OkLoadListener
public interface OkLoadListener {
// 請求資料成功
void okLoadSuccess(String json);
//請求資料失敗
void okLoadError(String error);
}
model包
GoodsListListener
public interface GoodsListListener {
// 獲取資料成功
void getDataSuccess(String json);
//獲取資料失敗
void getDataError(String error);
}
GoosListBean
自己請求
IModel
public interface IModel {
// 登入
void login(String url, Map<String, String> params, LoginListener loginListener);
// 註冊
void reg(String url, Map<String, String> params, RegListener regListener);
// 獲取商品資料
void getGoodsListData(String url, Map<String, String> params, GoodsListListener goodsListListener);
}
LoginListener
public interface LoginListener {
void loginSuccess(String json);
void loginError(String error);
}
ModelImpl
public class ModelImpl implements IModel {
private static final String TAG = "ModelImpl----";
@Override
public void login(final String url, Map<String, String> params, final LoginListener loginListener) {
//
HttpUtils httpUtils = HttpUtils.getHttpUtils();
//呼叫post請求
httpUtils.okPost(url, params);
// 設定回撥監聽
httpUtils.setOkLoadListener(new OkLoadListener() {
@Override
public void okLoadSuccess(String json) {
Log.d(TAG, "okLoadSuccess: " + json);
//將資料傳入p層
//解析json 如果code是0,成功,否則失敗
Gson gson = new Gson();
UserBean userBean = gson.fromJson(json, UserBean.class);
if (userBean.getCode().equals("0")) {
loginListener.loginSuccess(json);
} else {
loginListener.loginError(json);
}
}
@Override
public void okLoadError(String error) {
loginListener.loginError(error);
}
});
}
//註冊
@Override
public void reg(String url, Map<String, String> params, final RegListener regListener) {
HttpUtils httpUtils = HttpUtils.getHttpUtils();
httpUtils.okPost(url, params);//呼叫post請求
// 設定回撥監聽
httpUtils.setOkLoadListener(new OkLoadListener() {
@Override
public void okLoadSuccess(String json) {
Log.d(TAG, "okLoadSuccess: " + json);
//將資料傳入p層
//解析json 如果code是0,成功,否則失敗
Gson gson = new Gson();
RegBean userBean = gson.fromJson(json, RegBean.class);
if (userBean.getCode().equals("0")) {
regListener.regSuccess(json);
} else {
regListener.regError(json);
}
}
@Override
public void okLoadError(String error) {
regListener.regError(error);
}
});
}
// 獲取商品資料
@Override
public void getGoodsListData(String url, Map<String, String> params, final GoodsListListener goodsListListener) {
HttpUtils httpUtils = HttpUtils.getHttpUtils();
httpUtils.okPost(url, params);//呼叫post請求
// 設定回撥監聽
httpUtils.setOkLoadListener(new OkLoadListener() {
@Override
public void okLoadSuccess(String json) {
Log.d(TAG, "okLoadSuccess: " + json);
//將資料傳入p層
//解析json 如果code是0,成功,否則失敗
Gson gson = new Gson();
GoosListBean goosListBean = gson.fromJson(json, GoosListBean.class);
if (goosListBean.getCode().equals("0")) {
goodsListListener.getDataSuccess(json);
} else {
goodsListListener.getDataError(json);
}
}
@Override
public void okLoadError(String error) {
goodsListListener.getDataError(error);
}
});
}
}
MyAdater
public class MyAdater extends BaseAdapter {
private Context context;
private List<GoosListBean.DataBean> data;
public MyAdater(Context context,List<GoosListBean.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) {
MyViewHolder myViewHolder=null;
if (convertView==null){
convertView=View.inflate(context, R.layout.item_layout,null);
ImageView imageView=convertView.findViewById(R.id.item_pic);
TextView textView = convertView.findViewById(R.id.item_title);
myViewHolder=new MyViewHolder(imageView,textView);
convertView.setTag(myViewHolder);
}else {
myViewHolder= (MyViewHolder) convertView.getTag();
}
//賦值
String images = data.get(position).getImages();
String pic_url= images.split("\\|")[0];
Glide.with(context).load(pic_url).into(myViewHolder.getImageView());
myViewHolder.getTextView().setText(data.get(position).getTitle());
return convertView;
}
class MyViewHolder{
private ImageView imageView;
private TextView textView;
public MyViewHolder(ImageView imageView, TextView textView) {
this.imageView = imageView;
this.textView = textView;
}
public ImageView getImageView() {
return imageView;
}
public void setImageView(ImageView imageView) {
this.imageView = imageView;
}
public TextView getTextView() {
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
}
}
MyAdater2
public class MyAdater2 extends BaseAdapter {
private Context context;
private List<GoosListBean.DataBean> data;
public MyAdater2(Context context, List<GoosListBean.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) {
MyViewHolder myViewHolder=null;
if (convertView==null){
convertView=View.inflate(context, R.layout.item_layout_gridview,null);
ImageView imageView=convertView.findViewById(R.id.item_pic);
TextView textView = convertView.findViewById(R.id.item_title);
myViewHolder=new MyViewHolder(imageView,textView);
convertView.setTag(myViewHolder);
}else {
myViewHolder= (MyViewHolder) convertView.getTag();
}
//賦值
String images = data.get(position).getImages();
String pic_url= images.split("\\|")[0];
Glide.with(context).load(pic_url).into(myViewHolder.getImageView());
myViewHolder.getTextView().setText(data.get(position).getTitle());
return convertView;
}
class MyViewHolder{
private ImageView imageView;
private TextView textView;
public MyViewHolder(ImageView imageView, TextView textView) {
this.imageView = imageView;
this.textView = textView;
}
public ImageView getImageView() {
return imageView;
}
public void setImageView(ImageView imageView) {
this.imageView = imageView;
}
public TextView getTextView() {
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
}
}
RegBean
自己請求
RegListener
public interface RegListener {
void regSuccess(String json);
void regError(String error);
}
UserBean
自己請求
presenter包
Ipresenter
public interface Ipresenter {
// 登入
void loginPresenter(IModel iModel, IMainView iMainView);
// 註冊
void regPresenter(IModel iModel, IRegView iRegView);
// 顯示資料
void showGoodsListToView(IModel iModel, IGoodsListView iGoodsListView);
}
Presenter
public class Presenter implements Ipresenter {
private static final String TAG = "Presenter-----";
//登入
@Override
public void loginPresenter(IModel iModel, final IMainView iMainView) {
//呼叫m請求資料
Map<String, String> map = new HashMap<>();
map.put("mobile", iMainView.getMobile());
map.put("password", iMainView.getPassword());
iModel.login(HttpConfig.login_url, map, new LoginListener() {
//根據回撥結果,決定view的顯示效果
@Override
public void loginSuccess(String json) {
iMainView.loginSuccess();
}
@Override
public void loginError(String error) {
iMainView.loginError();
}
});
}
//註冊
@Override
public void regPresenter(IModel iModel, final IRegView iRegView) {
//呼叫m請求資料
Map<String, String> map = new HashMap<>();
map.put("mobile", iRegView.getMobile());
map.put("password", iRegView.getPassword());
iModel.reg(HttpConfig.reg_url, map, new RegListener() {
@Override
public void regSuccess(String json) {
iRegView.regSuccess();
}
@Override
public void regError(String error) {
iRegView.regError();
}
});
}
// 顯示資料
@Override
public void showGoodsListToView(IModel iModel, final IGoodsListView iGoodsListView) {
Map<String, String> map = new HashMap<>();
map.put("keywords", "筆記本");
map.put("page", "1");
iModel.getGoodsListData(HttpConfig.goods_list_url, map, new GoodsListListener() {
@Override
public void getDataSuccess(String json) {
Gson gson = new Gson();
GoosListBean goosListBean = gson.fromJson(json, GoosListBean.class);
iGoodsListView.showGoodsList(goosListBean.getData());
}
@Override
public void getDataError(String error) {
Log.d(TAG, "失敗---");
}
});
}
}
view包
GoodsListActivity
public class GoodsListActivity extends Activity implements View.OnClickListener, IGoodsListView {
private ListView listView;
private GridView gridView;
private static final String TAG = "GoodsListActivity---";
private boolean flag = true;
private ImageView change;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.goods_list_layout);
//初始化介面
initViews();
//請求書
Presenter presenter = new Presenter();
presenter.showGoodsListToView(new ModelImpl(), this);
}
private void initViews() {
change = findViewById(R.id.goodslist_change);
change.setOnClickListener(this);
listView = findViewById(R.id.goodslist_listview);
gridView = findViewById(R.id.goodslist_gridview);
}
//點選事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.goodslist_change:
if (flag) {
change.setImageResource(R.drawable.lv_icon);
gridView.setVisibility(View.VISIBLE);
listView.setVisibility(View.GONE);
} else {
change.setImageResource(R.drawable.grid_icon);
gridView.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
}
flag = !flag;
break;
}
}
//展示商品列表的方法
@Override
public void showGoodsList(List<GoosListBean.DataBean> data) {
Log.d(TAG, "showGoodsList: " + data);
//展示
MyAdater myAdater = new MyAdater(GoodsListActivity.this, data);
MyAdater2 myAdater2 = new MyAdater2(GoodsListActivity.this, data);
listView.setAdapter(myAdater);
gridView.setAdapter(myAdater2);
}
}
IGoodsListView
public interface IGoodsListView {
//展示商品列表的方法
void showGoodsList(List<GoosListBean.DataBean> data);
}
IMainView
public interface IMainView {
//獲手機號
String getMobile();
//獲取密碼
String getPassword();
//登入成功
void loginSuccess();
//登入失敗
void loginError();
}
IRegView
public interface IRegView {
//獲手機號
String getMobile();
//獲取密碼
String getPassword();
//註冊成功
void regSuccess();
//註冊失敗
void regError();
}
MainActivity
public class MainActivity extends AppCompatActivity implements IMainView, View.OnClickListener {
private EditText mobile;
private EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化介面
initViews();
}
private void initViews() {
mobile = (EditText) findViewById(R.id.mobile);
password = (EditText) findViewById(R.id.pwd);
Button login = (Button) findViewById(R.id.login);
Button reg = (Button) findViewById(R.id.reg);
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 loginSuccess() {
Toast.makeText(MainActivity.this, "成功---", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,GoodsListActivity.class));
}
@Override
public void loginError() {
Toast.makeText(MainActivity.this, "失敗---請註冊", Toast.LENGTH_SHORT).show();
}
//點選事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login:
Presenter presenter = new Presenter();
presenter.loginPresenter(new ModelImpl(),this);
break;
case R.id.reg:
startActivity(new Intent(MainActivity.this,RegActivity.class));
break;
}
}
}
RegActivity
public class RegActivity extends AppCompatActivity implements View.OnClickListener, IRegView {
private EditText mobile;
private EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reg);
//初始化介面
initViews();
}
private void initViews() {
mobile = (EditText) findViewById(R.id.mobile);
password = (EditText) findViewById(R.id.pwd);
Button reg = (Button) findViewById(R.id.reg);
reg.setOnClickListener(this);
}
//點選事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.reg:
Presenter presenter = new Presenter();
presenter.regPresenter(new ModelImpl(), this);
break;
}
}
//獲取手機號
@Override
public String getMobile() {
return mobile.getText().toString();
}
//密碼