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;
}
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;
}
}
}
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;
}
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 {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String method = request.method();
if (method.equals("GET")) {
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")) {
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) {
MediaType mediaType = MediaType.parse("image/*");
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();
httpUtils.okPost(url, params);
httpUtils.setOkLoadListener(new OkLoadListener() {
@Override
public void okLoadSuccess(String json) {
Log.d(TAG, "okLoadSuccess: " + json);
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);
httpUtils.setOkLoadListener(new OkLoadListener() {
@Override
public void okLoadSuccess(String json) {
Log.d(TAG, "okLoadSuccess: " + json);
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);
httpUtils.setOkLoadListener(new OkLoadListener() {
@Override
public void okLoadSuccess(String json) {
Log.d(TAG, "okLoadSuccess: " + json);
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) {
Map<String, String> map = new HashMap<>();
map.put("mobile", iMainView.getMobile());
map.put("password", iMainView.getPassword());
iModel.login(HttpConfig.login_url, map, new LoginListener() {
@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) {
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();
}
@Override
public String getPassword() {
return password.getText().toString();
}
@Override
public void regSuccess() {
Toast.makeText(RegActivity.this, "注册成功---", Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegActivity.this, GoodsListActivity.class));
}
@Override
public void regError() {
Toast.makeText(RegActivity.this, "注册失败---", Toast.LENGTH_SHORT).show();
}
}