@ -1,79 +1,112 @@
|
||||
package com.orangesale.cn;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.orangesale.cn.activity.IndexActivity;
|
||||
import com.orangesale.cn.activity.RegisterActivity;
|
||||
import com.orangesale.cn.dataoperation.OrangeDatabase;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
|
||||
private Button registerButton, loginButton;
|
||||
private EditText usernameText, paswdEdit;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.user_login);
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
switch (v.getId()) {
|
||||
case R.id.register:
|
||||
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
|
||||
startActivity(intent);
|
||||
break;
|
||||
case R.id.login:
|
||||
//注册时,我们引入了数据库,登录这里可以通过数据库进行验证,验证跳转到首页,不通过进行提示
|
||||
if (validateLogin()) {
|
||||
Intent intent1 = new Intent(MainActivity.this, IndexActivity.class);
|
||||
Bundle bundle = new Bundle();
|
||||
OrangeDatabase orangeDatabase = new OrangeDatabase(MainActivity.this);
|
||||
bundle.putString("username", usernameText.getText().toString());
|
||||
bundle = orangeDatabase.queryUserInfo(orangeDatabase.getReadableDatabase(), bundle);
|
||||
intent1.putExtras(bundle);
|
||||
startActivity(intent1);
|
||||
} else {
|
||||
Toast.makeText(MainActivity.this, "账号或者密码错误", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//界面组件初始化
|
||||
private void init() {
|
||||
usernameText = findViewById(R.id.username);
|
||||
paswdEdit = findViewById(R.id.password);
|
||||
loginButton = findViewById(R.id.login);
|
||||
loginButton.setOnClickListener(this);
|
||||
registerButton = findViewById(R.id.register);
|
||||
registerButton.setOnClickListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录验证
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private boolean validateLogin() {
|
||||
String username = usernameText.getText().toString();
|
||||
String password = paswdEdit.getText().toString();
|
||||
OrangeDatabase orangeDatabase = new OrangeDatabase(MainActivity.this);
|
||||
SQLiteDatabase sqLiteDatabase = orangeDatabase.getReadableDatabase();
|
||||
Cursor cursor = sqLiteDatabase.rawQuery("select * from orange_user where username=? and password=?", new String[]{username, password});
|
||||
if (cursor.getCount() > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
package com.orangesale.cn;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.Bundle;
|
||||
import android.os.Looper;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.orangesale.cn.activity.IndexActivity;
|
||||
import com.orangesale.cn.activity.RegisterActivity;
|
||||
import com.orangesale.cn.dataoperation.OrangeDatabase;
|
||||
import com.orangesale.cn.entity.OrangeUser;
|
||||
import com.orangesale.cn.netrequest.OkHttpUser;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
|
||||
private Button registerButton, loginButton;
|
||||
private EditText usernameText, paswdEdit;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.user_login);
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
switch (v.getId()) {
|
||||
case R.id.register:
|
||||
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
|
||||
startActivity(intent);
|
||||
break;
|
||||
case R.id.login:
|
||||
//注册时,我们引入了数据库,登录这里可以通过数据库进行验证,验证跳转到首页,不通过进行提示
|
||||
Thread thread = new Thread(runnable);
|
||||
thread.start();
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//界面组件初始化
|
||||
private void init() {
|
||||
usernameText = findViewById(R.id.username);
|
||||
paswdEdit = findViewById(R.id.password);
|
||||
loginButton = findViewById(R.id.login);
|
||||
loginButton.setOnClickListener(this);
|
||||
registerButton = findViewById(R.id.register);
|
||||
registerButton.setOnClickListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录验证
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private boolean validateLogin() {
|
||||
String username = usernameText.getText().toString();
|
||||
String password = paswdEdit.getText().toString();
|
||||
OrangeDatabase orangeDatabase = new OrangeDatabase(MainActivity.this);
|
||||
SQLiteDatabase sqLiteDatabase = orangeDatabase.getReadableDatabase();
|
||||
Cursor cursor = sqLiteDatabase.rawQuery("select * from orange_user where username=? and password=?", new String[]{username, password});
|
||||
if (cursor.getCount() > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录验证
|
||||
*/
|
||||
Runnable runnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String username = usernameText.getText().toString();
|
||||
String password = paswdEdit.getText().toString();
|
||||
OrangeUser orangeUser = new OrangeUser();
|
||||
orangeUser.setUsername(username);
|
||||
orangeUser.setPassword(password);
|
||||
OkHttpUser okHttpUser = new OkHttpUser();
|
||||
OrangeUser orangeUser1 = null;
|
||||
try {
|
||||
orangeUser1 = okHttpUser.userLogin(orangeUser);
|
||||
if (orangeUser1 != null) {
|
||||
Intent intent1 = new Intent(MainActivity.this, IndexActivity.class);
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("username", usernameText.getText().toString());
|
||||
bundle.putString("password", orangeUser1.getPassword());
|
||||
bundle.putString("city", orangeUser1.getCity());
|
||||
bundle.putString("sex", orangeUser1.getSex());
|
||||
intent1.putExtras(bundle);
|
||||
startActivity(intent1);
|
||||
} else {
|
||||
//解决在子线程中调用Toast的异常情况处理
|
||||
Looper.prepare();
|
||||
Toast.makeText(MainActivity.this, "账号或者密码错误", Toast.LENGTH_SHORT).show();
|
||||
Looper.loop();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.orangesale.cn.entity;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public class OrangeMessage {
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public Timestamp getSendTime() {
|
||||
return sendTime;
|
||||
}
|
||||
|
||||
public void setSendTime(Timestamp sendTime) {
|
||||
this.sendTime = sendTime;
|
||||
}
|
||||
|
||||
public String getUsed() {
|
||||
return used;
|
||||
}
|
||||
|
||||
public void setUsed(String used) {
|
||||
this.used = used;
|
||||
}
|
||||
|
||||
private Integer id;
|
||||
private String content;
|
||||
private Timestamp sendTime;
|
||||
private String used;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OrangeMessage{" +
|
||||
"id=" + id +
|
||||
", content='" + content + '\'' +
|
||||
", sendTime=" + sendTime +
|
||||
", used='" + used + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.orangesale.cn.entity;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public class OrangeProduct {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private BigDecimal price;
|
||||
private String imgUrl;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OrangeProduct{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", price=" + price +
|
||||
", imgUrl='" + imgUrl + '\'' +
|
||||
", addTime=" + addTime +
|
||||
'}';
|
||||
}
|
||||
|
||||
public Timestamp getAddTime() {
|
||||
return addTime;
|
||||
}
|
||||
|
||||
public void setAddTime(Timestamp addTime) {
|
||||
this.addTime = addTime;
|
||||
}
|
||||
|
||||
private Timestamp addTime;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getImgUrl() {
|
||||
return imgUrl;
|
||||
}
|
||||
|
||||
public void setImgUrl(String imgUrl) {
|
||||
this.imgUrl = imgUrl;
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.orangesale.cn.entity;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public class OrangeProductPack {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private Timestamp addTime;
|
||||
private Bitmap imgBitmap;
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
private BigDecimal price;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OrangeProductPack{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", addTime=" + addTime +
|
||||
", imgBitmap=" + imgBitmap +
|
||||
", price=" + price +
|
||||
'}';
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Timestamp getAddTime() {
|
||||
return addTime;
|
||||
}
|
||||
|
||||
public void setAddTime(Timestamp addTime) {
|
||||
this.addTime = addTime;
|
||||
}
|
||||
|
||||
public Bitmap getImgBitmap() {
|
||||
return imgBitmap;
|
||||
}
|
||||
|
||||
public void setImgBitmap(Bitmap imgBitmap) {
|
||||
this.imgBitmap = imgBitmap;
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package com.orangesale.cn.netrequest;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.orangesale.cn.entity.OrangeProduct;
|
||||
import com.orangesale.cn.entity.OrangeProductPack;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class OkHttpClientProduct {
|
||||
|
||||
/**
|
||||
* 查询商品信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<OrangeProduct> getProduct() throws IOException {
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
Request request = new Request.Builder()
|
||||
.url("http://172.16.40.55:8081/orange/product/search")
|
||||
.build();
|
||||
Response response = okHttpClient.newCall(request).execute();
|
||||
JSONObject jsonObject = JSON.parseObject(Objects.requireNonNull(response.body()).string());
|
||||
List<OrangeProduct> list = JSON.parseObject(jsonObject.getString("data"), new TypeReference<List<OrangeProduct>>() {
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图片
|
||||
*
|
||||
* @param imgUrl
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public Bitmap getImageBitMap(String imgUrl) throws IOException {
|
||||
Bitmap bitmap;
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
Request request = new Request.Builder()
|
||||
.url(imgUrl)
|
||||
.build();
|
||||
Response response = okHttpClient.newCall(request).execute();
|
||||
byte[] bytes = Objects.requireNonNull(response.body()).bytes();
|
||||
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
|
||||
return bitmap;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装信息
|
||||
*
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
|
||||
public List<OrangeProductPack> getProductPack() throws IOException {
|
||||
List<OrangeProductPack> packList = new ArrayList<>();
|
||||
List<OrangeProduct> list = getProduct();
|
||||
for (OrangeProduct orangeProduct : list) {
|
||||
OrangeProductPack orangeProductPack = new OrangeProductPack();
|
||||
orangeProductPack.setId(orangeProduct.getId());
|
||||
orangeProductPack.setImgBitmap(getImageBitMap(orangeProduct.getImgUrl()));
|
||||
orangeProductPack.setAddTime(orangeProduct.getAddTime());
|
||||
orangeProductPack.setPrice(orangeProduct.getPrice());
|
||||
packList.add(orangeProductPack);
|
||||
}
|
||||
return packList;
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package com.orangesale.cn.netrequest;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.orangesale.cn.entity.OrangeUser;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class OkHttpUser {
|
||||
|
||||
/**
|
||||
* 用户登录验证
|
||||
*
|
||||
* @param orangeUser
|
||||
*/
|
||||
public OrangeUser userLogin(OrangeUser orangeUser) throws IOException {
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
//数据类型为json格式
|
||||
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
|
||||
//将对象转为JSON字符串
|
||||
String jsonStr = JSONObject.toJSONString(orangeUser);
|
||||
RequestBody requestBody = RequestBody.create(jsonStr, mediaType);
|
||||
Request request = new Request.Builder()
|
||||
.url("http://172.16.40.55:8081/orange/user/login")
|
||||
.post(requestBody)
|
||||
.build();
|
||||
Response response = okHttpClient.newCall(request).execute();
|
||||
JSONObject jsonObject = JSON.parseObject(response.body().string());
|
||||
JSONObject jsonObject1 = jsonObject.getJSONObject("data");
|
||||
OrangeUser orangeUser1 = JSON.toJavaObject(jsonObject1, OrangeUser.class);
|
||||
return orangeUser1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*
|
||||
* @param orangeUser
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean userRegister(OrangeUser orangeUser) throws IOException {
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
//数据类型为json格式
|
||||
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
|
||||
//将对象转为JSON字符串
|
||||
String jsonStr = JSONObject.toJSONString(orangeUser);
|
||||
RequestBody requestBody = RequestBody.create(jsonStr, mediaType);
|
||||
Request request = new Request.Builder()
|
||||
.url("http://172.16.40.55:8081/orange/user/register")
|
||||
.post(requestBody)
|
||||
.build();
|
||||
Response response = okHttpClient.newCall(request).execute();
|
||||
JSONObject jsonObject = JSON.parseObject(response.body().string());
|
||||
Log.i("register", "userRegister: "+jsonObject);
|
||||
return jsonObject.getBoolean("flag");
|
||||
}
|
||||
|
||||
|
||||
}
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.0 KiB |
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.0 KiB |
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 8.6 KiB After Width: | Height: | Size: 8.6 KiB |
Before Width: | Height: | Size: 368 B After Width: | Height: | Size: 368 B |
Before Width: | Height: | Size: 231 B After Width: | Height: | Size: 231 B |
Before Width: | Height: | Size: 233 B After Width: | Height: | Size: 233 B |
Before Width: | Height: | Size: 5.4 KiB After Width: | Height: | Size: 5.4 KiB |
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 790 B After Width: | Height: | Size: 790 B |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 8.0 KiB After Width: | Height: | Size: 8.0 KiB |
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 6.0 KiB After Width: | Height: | Size: 6.0 KiB |
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 3.2 KiB |
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.0 KiB |
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
Before Width: | Height: | Size: 799 B After Width: | Height: | Size: 799 B |
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.0 KiB |
Before Width: | Height: | Size: 402 B After Width: | Height: | Size: 402 B |
Before Width: | Height: | Size: 5.9 KiB After Width: | Height: | Size: 5.9 KiB |
Before Width: | Height: | Size: 554 B After Width: | Height: | Size: 554 B |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 2.6 KiB |
Before Width: | Height: | Size: 6.5 KiB After Width: | Height: | Size: 6.5 KiB |
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 9.3 KiB After Width: | Height: | Size: 9.3 KiB |