diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..b507fd2 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +OrangeSale_04 \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 181e1ab..84a195a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -4,8 +4,8 @@ android { compileSdkVersion 30 buildToolsVersion "30.0.2" defaultConfig { - applicationId "com.example.orangesale_05" - minSdkVersion 23 + applicationId "com.example.orangesale_end" + minSdkVersion 26 targetSdkVersion 30 versionCode 1 versionName "1.0" @@ -21,7 +21,6 @@ android { sourceCompatibility = 1.8 targetCompatibility = 1.8 } - } dependencies { @@ -32,4 +31,6 @@ dependencies { androidTestImplementation 'androidx.test.ext:junit:1.1.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' implementation 'liji.library.dev:citypickerview:1.1.0' + implementation "com.squareup.okhttp3:okhttp:4.2.2" + implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.12' } diff --git a/app/src/androidTest/java/com/example/orangesale_05/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/example/orangesale_end/ExampleInstrumentedTest.java similarity index 81% rename from app/src/androidTest/java/com/example/orangesale_05/ExampleInstrumentedTest.java rename to app/src/androidTest/java/com/example/orangesale_end/ExampleInstrumentedTest.java index e315c38..f6450c2 100644 --- a/app/src/androidTest/java/com/example/orangesale_05/ExampleInstrumentedTest.java +++ b/app/src/androidTest/java/com/example/orangesale_end/ExampleInstrumentedTest.java @@ -1,4 +1,4 @@ -package com.example.orangesale_05; +package com.example.orangesale_end; import android.content.Context; @@ -22,6 +22,6 @@ public class ExampleInstrumentedTest { // Context of the app under test. Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); - assertEquals("com.example.orangesale_05", appContext.getPackageName()); + assertEquals("com.example.orangesale_end", appContext.getPackageName()); } } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e797e03..cf08926 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,12 +1,13 @@ - + package="com.example.orangesale_end"> + @@ -20,6 +21,10 @@ + + \ No newline at end of file diff --git a/app/src/main/java/com/example/orangesale_05/dataoperation/OrangeDatabase.java b/app/src/main/java/com/example/orangesale_05/dataoperation/OrangeDatabase.java deleted file mode 100644 index 7989a62..0000000 --- a/app/src/main/java/com/example/orangesale_05/dataoperation/OrangeDatabase.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.example.orangesale_05.dataoperation; - -import android.content.ContentValues; -import android.content.Context; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; -import android.os.Bundle; - -import androidx.annotation.Nullable; - -public class OrangeDatabase extends SQLiteOpenHelper { - public OrangeDatabase(@Nullable Context context) { - super(context, "orange.db3", null, 1); - } - - @Override - public void onCreate(SQLiteDatabase db) { - //创建用户表 - String sql = "create table orange_user(id integer primary key autoincrement, username varchar(50), password varchar(50),sex varchar(10),city carchar(50))"; - db.execSQL(sql); - } - - @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - - } - - /** - * 插入数据 - * - * @param sqLiteDatabase - * @param username - * @param password - * @param sex - * @param city - */ - public void insertUser(SQLiteDatabase sqLiteDatabase, String username, String password, String sex, String city) { - ContentValues contentValues = new ContentValues(); - contentValues.put("username", username); - contentValues.put("password", password); - contentValues.put("sex", sex); - contentValues.put("city", city); - sqLiteDatabase.insert("orange_user", null, contentValues); - sqLiteDatabase.close(); - } - - /** - * 查询数据 - * - * @param sqLiteDatabase - * @param bundle - * @return - */ - public Bundle queryUserInfo(SQLiteDatabase sqLiteDatabase, Bundle bundle) { - String username = bundle.getString("username"); - Cursor cursor = sqLiteDatabase.rawQuery("select * from orange_user where username=?", new String[]{username}); - if (cursor != null) { - while (cursor.moveToNext()) { - bundle.putString("sex", cursor.getString(3)); - bundle.putString("city", cursor.getString(4)); - } - } - cursor.close(); - sqLiteDatabase.close(); - return bundle; - } -} diff --git a/app/src/main/java/com/example/orangesale_05/fragment/ProductFragment.java b/app/src/main/java/com/example/orangesale_05/fragment/ProductFragment.java deleted file mode 100644 index e95c4e4..0000000 --- a/app/src/main/java/com/example/orangesale_05/fragment/ProductFragment.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.example.orangesale_05.fragment; - -import android.app.Fragment; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import com.example.orangesale_05.R; - -public class ProductFragment extends Fragment { - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - View view = LayoutInflater.from(getActivity()).inflate(R.layout.content_product, container, false); - return view; - } -} diff --git a/app/src/main/java/com/example/orangesale_05/fragment/ShoppingCartFragment.java b/app/src/main/java/com/example/orangesale_05/fragment/ShoppingCartFragment.java deleted file mode 100644 index b0fd574..0000000 --- a/app/src/main/java/com/example/orangesale_05/fragment/ShoppingCartFragment.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.example.orangesale_05.fragment; - -import android.app.Fragment; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import com.example.orangesale_05.R; - -public class ShoppingCartFragment extends Fragment { - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - View view = LayoutInflater.from(getActivity()).inflate(R.layout.content_shopping, container, false); - return view; - } -} diff --git a/app/src/main/java/com/example/orangesale_05/MainActivity.java b/app/src/main/java/com/example/orangesale_end/MainActivity.java similarity index 55% rename from app/src/main/java/com/example/orangesale_05/MainActivity.java rename to app/src/main/java/com/example/orangesale_end/MainActivity.java index 5e567d3..4141925 100644 --- a/app/src/main/java/com/example/orangesale_05/MainActivity.java +++ b/app/src/main/java/com/example/orangesale_end/MainActivity.java @@ -1,9 +1,8 @@ -package com.example.orangesale_05; +package com.example.orangesale_end; 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; @@ -11,9 +10,12 @@ import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; -import com.example.orangesale_05.activity.IndexActivity; -import com.example.orangesale_05.activity.RegisterActivity; -import com.example.orangesale_05.dataoperation.OrangeDatabase; +import com.example.orangesale_end.activity.IndexActivity; +import com.example.orangesale_end.activity.RegisterActivity; +import com.example.orangesale_end.entity.OrangeUser; +import com.example.orangesale_end.netrequest.OkHttpUser; + +import java.io.IOException; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button registerButton, loginButton; @@ -23,7 +25,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.user_login); - init(); } @@ -36,19 +37,11 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe 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(); - } + Thread thread = new Thread(runnable); + thread.start(); break; } + } //界面组件初始化 @@ -63,18 +56,37 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe /** * 登录验证 - * - * @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; + 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(); + } } - return false; - } + }; } diff --git a/app/src/main/java/com/example/orangesale_05/activity/CategoryActivity.java b/app/src/main/java/com/example/orangesale_end/activity/CategoryActivity.java similarity index 89% rename from app/src/main/java/com/example/orangesale_05/activity/CategoryActivity.java rename to app/src/main/java/com/example/orangesale_end/activity/CategoryActivity.java index 9b82d77..72689f6 100644 --- a/app/src/main/java/com/example/orangesale_05/activity/CategoryActivity.java +++ b/app/src/main/java/com/example/orangesale_end/activity/CategoryActivity.java @@ -1,4 +1,4 @@ -package com.example.orangesale_05.activity; +package com.example.orangesale_end.activity; import android.app.Activity; import android.app.FragmentManager; @@ -8,10 +8,10 @@ import android.widget.ListView; import androidx.annotation.Nullable; -import com.example.orangesale_05.R; -import com.example.orangesale_05.adapter.Adapter; -import com.example.orangesale_05.entity.Product; -import com.example.orangesale_05.fragment.SetDetailFragment; +import com.example.orangesale_end.R; +import com.example.orangesale_end.adapter.Adapter; +import com.example.orangesale_end.entity.Product; +import com.example.orangesale_end.fragment.SetDetailFragment; import java.math.BigDecimal; import java.util.ArrayList; diff --git a/app/src/main/java/com/example/orangesale_05/activity/IndexActivity.java b/app/src/main/java/com/example/orangesale_end/activity/IndexActivity.java similarity index 77% rename from app/src/main/java/com/example/orangesale_05/activity/IndexActivity.java rename to app/src/main/java/com/example/orangesale_end/activity/IndexActivity.java index 987ab18..07bffb4 100644 --- a/app/src/main/java/com/example/orangesale_05/activity/IndexActivity.java +++ b/app/src/main/java/com/example/orangesale_end/activity/IndexActivity.java @@ -1,4 +1,4 @@ -package com.example.orangesale_05.activity; +package com.example.orangesale_end.activity; import android.app.Activity; import android.app.FragmentTransaction; @@ -9,11 +9,12 @@ import android.widget.LinearLayout; import androidx.annotation.Nullable; -import com.example.orangesale_05.R; -import com.example.orangesale_05.fragment.IndexFragment; -import com.example.orangesale_05.fragment.PearsonFragment; -import com.example.orangesale_05.fragment.ProductFragment; -import com.example.orangesale_05.fragment.ShoppingCartFragment; +import com.example.orangesale_end.R; +import com.example.orangesale_end.fragment.IndexFragment; +import com.example.orangesale_end.fragment.PearsonFragment; +import com.example.orangesale_end.fragment.ProductFragment; +import com.example.orangesale_end.fragment.ShoppingCartFragment; +import com.example.orangesale_end.service.TimeService; public class IndexActivity extends Activity implements View.OnClickListener { private IndexFragment indexFragment; @@ -28,6 +29,8 @@ public class IndexActivity extends Activity implements View.OnClickListener { setContentView(R.layout.activity_main); init(); initIndexFragment(); + Thread thread = new Thread(runnable); + thread.start(); } /** @@ -71,7 +74,7 @@ public class IndexActivity extends Activity implements View.OnClickListener { if (indexFragment == null) { indexFragment = new IndexFragment(); } - transaction.replace(R.id.main_content, indexFragment); + transaction.replace(R.id.main_content0, indexFragment); transaction.commit(); } @@ -84,7 +87,7 @@ public class IndexActivity extends Activity implements View.OnClickListener { if (productFragment == null) { productFragment = new ProductFragment(); } - transaction.replace(R.id.main_content, productFragment); + transaction.replace(R.id.main_content0, productFragment); transaction.commit(); } @@ -97,7 +100,7 @@ public class IndexActivity extends Activity implements View.OnClickListener { if (shoppingCartFragment == null) { shoppingCartFragment = new ShoppingCartFragment(); } - transaction.replace(R.id.main_content, shoppingCartFragment); + transaction.replace(R.id.main_content0, shoppingCartFragment); transaction.commit(); } @@ -113,8 +116,13 @@ public class IndexActivity extends Activity implements View.OnClickListener { pearsonFragment = new PearsonFragment(); pearsonFragment.setArguments(bundle); } - transaction.replace(R.id.main_content, pearsonFragment); + transaction.replace(R.id.main_content0, pearsonFragment); transaction.commit(); } + Runnable runnable = () -> { + Intent intent = new Intent(IndexActivity.this, TimeService.class); + startService(intent); + }; + } diff --git a/app/src/main/java/com/example/orangesale_05/activity/RegisterActivity.java b/app/src/main/java/com/example/orangesale_end/activity/RegisterActivity.java similarity index 51% rename from app/src/main/java/com/example/orangesale_05/activity/RegisterActivity.java rename to app/src/main/java/com/example/orangesale_end/activity/RegisterActivity.java index 9be3092..bfa98b7 100644 --- a/app/src/main/java/com/example/orangesale_05/activity/RegisterActivity.java +++ b/app/src/main/java/com/example/orangesale_end/activity/RegisterActivity.java @@ -1,10 +1,9 @@ -package com.example.orangesale_05.activity; +package com.example.orangesale_end.activity; -import android.content.ContentValues; import android.content.Intent; -import android.database.sqlite.SQLiteDatabase; import android.graphics.Color; import android.os.Bundle; +import android.os.Looper; import android.view.View; import android.widget.Button; import android.widget.EditText; @@ -16,8 +15,11 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.lljjcoder.citypickerview.widget.CityPicker; -import com.example.orangesale_05.R; -import com.example.orangesale_05.dataoperation.OrangeDatabase; +import com.example.orangesale_end.R; +import com.example.orangesale_end.entity.OrangeUser; +import com.example.orangesale_end.netrequest.OkHttpUser; + +import java.io.IOException; public class RegisterActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener { private EditText usernameEdit, passwordEdit, surePasswordEdit; @@ -60,7 +62,9 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL break; case R.id.reg_register: //注册验证方法 - validateRegister(); + //validateRegister(); + Thread thread = new Thread(runnable); + thread.start(); break; } @@ -106,44 +110,6 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL } - /** - * 注册验证 - */ - public void validateRegister() { - String username = usernameEdit.getText().toString(); - String password = passwordEdit.getText().toString(); - String surePassword = surePasswordEdit.getText().toString(); - String city = cityText.getText().toString(); - //判断两次密码是否输入一致 - if (password.equals(surePassword)) { - //这里也可以再进行其它的验证,如是否符合要求等,符合就进行插入数据库操作 - if (!username.equals("") || !password.equals("")) { - if (!city.equals("")) { - Bundle bundle = new Bundle(); - bundle.putString("username", username); - bundle.putString("password", password); - bundle.putString("sex", sexStr); - bundle.putString("city", city); - //本节将使用安卓自身带的SQLite数据库的方式存储注册的信息(之前直接传输显示在界面是的方式) - OrangeDatabase orangeDatabase = new OrangeDatabase(RegisterActivity.this); - SQLiteDatabase sqLiteDatabase = orangeDatabase.getWritableDatabase(); - insertData(sqLiteDatabase, bundle); - Intent intent = new Intent(RegisterActivity.this, IndexActivity.class); - intent.putExtras(bundle); - startActivity(intent); - } else { - Toast.makeText(RegisterActivity.this, "请选择地址", Toast.LENGTH_SHORT).show(); - } - } else { - Toast.makeText(RegisterActivity.this, "账号或密码未填写", Toast.LENGTH_SHORT).show(); - } - } else { - Toast.makeText(RegisterActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show(); - } - - - } - @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //根据用户选择来改变sex的值 @@ -151,18 +117,60 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL } /** - * 插入数据库的值 - * - * @param sqLiteDatabase - * @param bundle + * 注册验证 */ - private void insertData(SQLiteDatabase sqLiteDatabase, Bundle bundle) { - ContentValues contentValues = new ContentValues(); - contentValues.put("username", bundle.getString("username")); - contentValues.put("password", bundle.getString("password")); - contentValues.put("sex", bundle.getString("sex")); - contentValues.put("city", bundle.getString("city")); - sqLiteDatabase.insert("orange_user", null, contentValues); - sqLiteDatabase.close(); - } + Runnable runnable = new Runnable() { + @Override + public void run() { + String username = usernameEdit.getText().toString(); + String password = passwordEdit.getText().toString(); + String surePassword = surePasswordEdit.getText().toString(); + String city = cityText.getText().toString(); + //判断两次密码是否输入一致 + if (password.equals(surePassword)) { + //这里也可以再进行其它的验证,如是否符合要求等,符合就进行插入数据库操作 + if (!username.equals("") || !password.equals("")) { + if (!city.equals("")) { + Bundle bundle = new Bundle(); + bundle.putString("username", username); + bundle.putString("password", password); + bundle.putString("sex", sexStr); + bundle.putString("city", city); + OrangeUser orangeUser = new OrangeUser(); + orangeUser.setUsername(username); + orangeUser.setPassword(password); + orangeUser.setCity(city); + orangeUser.setSex(sexStr); + //将使用安卓自身带的SQLite数据库换成调用okhttp的方式 + OkHttpUser okHttpUser = new OkHttpUser(); + try { + if (okHttpUser.userRegister(orangeUser)) { + Intent intent = new Intent(RegisterActivity.this, IndexActivity.class); + intent.putExtras(bundle); + startActivity(intent); + } else { + Looper.prepare(); + Toast.makeText(RegisterActivity.this, "注册失败!", Toast.LENGTH_SHORT).show(); + Looper.loop(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } else { + Looper.prepare(); + Toast.makeText(RegisterActivity.this, "请选择地址", Toast.LENGTH_SHORT).show(); + Looper.loop(); + } + } else { + Looper.prepare(); + Toast.makeText(RegisterActivity.this, "账号或密码未填写", Toast.LENGTH_SHORT).show(); + Looper.loop(); + } + } else { + Looper.prepare(); + Toast.makeText(RegisterActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show(); + Looper.loop(); + } + } + }; } diff --git a/app/src/main/java/com/example/orangesale_05/activity/UserActivity.java b/app/src/main/java/com/example/orangesale_end/activity/UserActivity.java similarity index 87% rename from app/src/main/java/com/example/orangesale_05/activity/UserActivity.java rename to app/src/main/java/com/example/orangesale_end/activity/UserActivity.java index 836ae96..6fc5977 100644 --- a/app/src/main/java/com/example/orangesale_05/activity/UserActivity.java +++ b/app/src/main/java/com/example/orangesale_end/activity/UserActivity.java @@ -1,4 +1,4 @@ -package com.example.orangesale_05.activity; +package com.example.orangesale_end.activity; import android.app.Activity; import android.content.Intent; @@ -10,7 +10,7 @@ import android.widget.TextView; import androidx.annotation.Nullable; -import com.example.orangesale_05.R; +import com.example.orangesale_end.R; public class UserActivity extends Activity implements View.OnClickListener { private ImageView userIconImage; @@ -38,7 +38,7 @@ public class UserActivity extends Activity implements View.OnClickListener { userPayLine = findViewById(R.id.user_pay); userSettingLine = findViewById(R.id.user_setting); userGeneralLine = findViewById(R.id.user_general); - userSearchProductLine = findViewById(R.id.user_searchProduct); +// userSearchProductLine = findViewById(R.id.user_searchProduct); userSearchProductLine.setOnClickListener(this); setData(); } @@ -57,10 +57,10 @@ public class UserActivity extends Activity implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { - case R.id.user_searchProduct: + /*case R.id.user_searchProduct: Intent intent1 = new Intent(UserActivity.this, CategoryActivity.class); startActivity(intent1); - break; + break;*/ } } } diff --git a/app/src/main/java/com/example/orangesale_05/adapter/Adapter.java b/app/src/main/java/com/example/orangesale_end/adapter/Adapter.java similarity index 92% rename from app/src/main/java/com/example/orangesale_05/adapter/Adapter.java rename to app/src/main/java/com/example/orangesale_end/adapter/Adapter.java index 9b9fc45..6aea22f 100644 --- a/app/src/main/java/com/example/orangesale_05/adapter/Adapter.java +++ b/app/src/main/java/com/example/orangesale_end/adapter/Adapter.java @@ -1,4 +1,4 @@ -package com.example.orangesale_05.adapter; +package com.example.orangesale_end.adapter; import android.content.Context; import android.graphics.Color; @@ -9,7 +9,7 @@ import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; -import com.example.orangesale_05.R; +import com.example.orangesale_end.R; import java.util.List; diff --git a/app/src/main/java/com/example/orangesale_end/adapter/ListViewAdapter.java b/app/src/main/java/com/example/orangesale_end/adapter/ListViewAdapter.java new file mode 100644 index 0000000..599edb9 --- /dev/null +++ b/app/src/main/java/com/example/orangesale_end/adapter/ListViewAdapter.java @@ -0,0 +1,78 @@ +package com.example.orangesale_end.adapter; + +import android.content.Context; +import android.graphics.Color; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.example.orangesale_end.R; +import com.example.orangesale_end.entity.Condition; + +import java.util.List; + +public class ListViewAdapter extends BaseAdapter { + private List conditionList; + private LayoutInflater layoutInflater; + private int selectedPosition = -1; + private int selectColor = Color.GRAY; + + public ListViewAdapter(Context context, List conditionList) { + this.conditionList = conditionList; + this.layoutInflater = LayoutInflater.from(context); + } + + @Override + public int getCount() { + return conditionList.size(); + } + + @Override + public Object getItem(int position) { + return conditionList.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + ViewHolder viewHolder; + if (convertView == null) { + convertView = layoutInflater.inflate(R.layout.product_condition_item, null); + viewHolder = new ViewHolder(); + viewHolder.imageView = convertView.findViewById(R.id.condition_icon); + viewHolder.jiange = convertView.findViewById(R.id.image_jiange); + viewHolder.textView = convertView.findViewById(R.id.condition_name); + viewHolder.linearLayout = convertView.findViewById(R.id.item_bg); + convertView.setTag(viewHolder); + } else { + viewHolder = (ViewHolder) convertView.getTag(); + } + Condition condition = conditionList.get(position); + if (condition != null) { + viewHolder.imageView.setBackgroundResource(condition.getConditionIcon()); + viewHolder.textView.setText(condition.getConditionName()); + viewHolder.jiange.setBackgroundColor(Color.rgb(207, 207, 207)); + if (selectedPosition == position) { + viewHolder.linearLayout.setBackgroundColor(selectColor); + } + + } + return convertView; + } + + class ViewHolder { + ImageView imageView, jiange; + TextView textView; + LinearLayout linearLayout; + } + + +} diff --git a/app/src/main/java/com/example/orangesale_05/adapter/ProductAdapter.java b/app/src/main/java/com/example/orangesale_end/adapter/ProductAdapter.java similarity index 63% rename from app/src/main/java/com/example/orangesale_05/adapter/ProductAdapter.java rename to app/src/main/java/com/example/orangesale_end/adapter/ProductAdapter.java index dec9822..0c9633a 100644 --- a/app/src/main/java/com/example/orangesale_05/adapter/ProductAdapter.java +++ b/app/src/main/java/com/example/orangesale_end/adapter/ProductAdapter.java @@ -1,7 +1,6 @@ -package com.example.orangesale_05.adapter; +package com.example.orangesale_end.adapter; import android.content.Context; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -9,28 +8,29 @@ import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; -import com.example.orangesale_05.R; -import com.example.orangesale_05.entity.Product; +import com.example.orangesale_end.R; +import com.example.orangesale_end.entity.OrangeProductPack; import java.util.List; public class ProductAdapter extends BaseAdapter { - private List productList; + //private List productList; + private List orangeProductList; private LayoutInflater layoutInflater; - public ProductAdapter(Context context, List productList) { - this.productList = productList; + public ProductAdapter(Context context, List orangeProductList) { + this.orangeProductList = orangeProductList; this.layoutInflater = LayoutInflater.from(context); } @Override public int getCount() { - return productList.size(); + return orangeProductList.size(); } @Override public Object getItem(int position) { - return productList.get(position); + return orangeProductList.get(position); } @Override @@ -51,12 +51,11 @@ public class ProductAdapter extends BaseAdapter { } else { viewHolder = (ViewHolder) convertView.getTag(); } - Product product = productList.get(position); - Log.i("product", "getView: "+product.toString()); + OrangeProductPack product = orangeProductList.get(position); if (product != null) { - viewHolder.productImage.setBackgroundResource(product.getImageUrlId()); - viewHolder.productName.setText(product.getProductName()); - viewHolder.productPrice.setText(String.valueOf(product.getProductPrice())); + viewHolder.productImage.setImageBitmap(product.getImgBitmap()); + viewHolder.productName.setText(product.getName()); + viewHolder.productPrice.setText(String.format("%s元", String.valueOf(product.getPrice()))); } return convertView; } @@ -65,4 +64,5 @@ public class ProductAdapter extends BaseAdapter { ImageView productImage; TextView productName, productPrice; } + } diff --git a/app/src/main/java/com/example/orangesale_end/entity/Condition.java b/app/src/main/java/com/example/orangesale_end/entity/Condition.java new file mode 100644 index 0000000..f4f2771 --- /dev/null +++ b/app/src/main/java/com/example/orangesale_end/entity/Condition.java @@ -0,0 +1,30 @@ +package com.example.orangesale_end.entity; + +public class Condition { + private Integer conditionIcon; + private String conditionName; + + public Integer getConditionIcon() { + return conditionIcon; + } + + public void setConditionIcon(Integer conditionIcon) { + this.conditionIcon = conditionIcon; + } + + public String getConditionName() { + return conditionName; + } + + public void setConditionName(String conditionName) { + this.conditionName = conditionName; + } + + @Override + public String toString() { + return "Condition{" + + "conditionIcon=" + conditionIcon + + ", conditionName='" + conditionName + '\'' + + '}'; + } +} diff --git a/app/src/main/java/com/example/orangesale_end/entity/OrangeMessage.java b/app/src/main/java/com/example/orangesale_end/entity/OrangeMessage.java new file mode 100644 index 0000000..c2885e9 --- /dev/null +++ b/app/src/main/java/com/example/orangesale_end/entity/OrangeMessage.java @@ -0,0 +1,52 @@ +package com.example.orangesale_end.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 + '\'' + + '}'; + } +} diff --git a/app/src/main/java/com/example/orangesale_end/entity/OrangeProduct.java b/app/src/main/java/com/example/orangesale_end/entity/OrangeProduct.java new file mode 100644 index 0000000..d938977 --- /dev/null +++ b/app/src/main/java/com/example/orangesale_end/entity/OrangeProduct.java @@ -0,0 +1,64 @@ +package com.example.orangesale_end.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; + } +} diff --git a/app/src/main/java/com/example/orangesale_end/entity/OrangeProductPack.java b/app/src/main/java/com/example/orangesale_end/entity/OrangeProductPack.java new file mode 100644 index 0000000..b59eafd --- /dev/null +++ b/app/src/main/java/com/example/orangesale_end/entity/OrangeProductPack.java @@ -0,0 +1,66 @@ +package com.example.orangesale_end.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; + } +} diff --git a/app/src/main/java/com/example/orangesale_05/entity/OrangeUser.java b/app/src/main/java/com/example/orangesale_end/entity/OrangeUser.java similarity index 91% rename from app/src/main/java/com/example/orangesale_05/entity/OrangeUser.java rename to app/src/main/java/com/example/orangesale_end/entity/OrangeUser.java index 48a19e8..b8a3d35 100644 --- a/app/src/main/java/com/example/orangesale_05/entity/OrangeUser.java +++ b/app/src/main/java/com/example/orangesale_end/entity/OrangeUser.java @@ -1,4 +1,4 @@ -package com.example.orangesale_05.entity; +package com.example.orangesale_end.entity; public class OrangeUser { private Integer id; diff --git a/app/src/main/java/com/example/orangesale_05/entity/Product.java b/app/src/main/java/com/example/orangesale_end/entity/Product.java similarity index 91% rename from app/src/main/java/com/example/orangesale_05/entity/Product.java rename to app/src/main/java/com/example/orangesale_end/entity/Product.java index 411aac3..ffd8d80 100644 --- a/app/src/main/java/com/example/orangesale_05/entity/Product.java +++ b/app/src/main/java/com/example/orangesale_end/entity/Product.java @@ -1,4 +1,4 @@ -package com.example.orangesale_05.entity; +package com.example.orangesale_end.entity; import java.math.BigDecimal; diff --git a/app/src/main/java/com/example/orangesale_end/entity/ShoppingCart.java b/app/src/main/java/com/example/orangesale_end/entity/ShoppingCart.java new file mode 100644 index 0000000..3144dee --- /dev/null +++ b/app/src/main/java/com/example/orangesale_end/entity/ShoppingCart.java @@ -0,0 +1,44 @@ +package com.example.orangesale_end.entity; + +public class ShoppingCart { + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public Integer getProductId() { + return ProductId; + } + + public void setProductId(Integer productId) { + ProductId = productId; + } + + public Integer getNumber() { + return number; + } + + public void setNumber(Integer number) { + this.number = number; + } + + private Integer id; + private Integer userId; + private Integer ProductId; + /** + * 购买数量 + */ + private Integer number; +} diff --git a/app/src/main/java/com/example/orangesale_05/fragment/IndexFragment.java b/app/src/main/java/com/example/orangesale_end/fragment/IndexFragment.java similarity index 72% rename from app/src/main/java/com/example/orangesale_05/fragment/IndexFragment.java rename to app/src/main/java/com/example/orangesale_end/fragment/IndexFragment.java index 1c3c327..6a6c49e 100644 --- a/app/src/main/java/com/example/orangesale_05/fragment/IndexFragment.java +++ b/app/src/main/java/com/example/orangesale_end/fragment/IndexFragment.java @@ -1,6 +1,7 @@ -package com.example.orangesale_05.fragment; +package com.example.orangesale_end.fragment; import android.app.Fragment; +import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -12,20 +13,25 @@ import android.widget.SearchView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import com.example.orangesale_05.R; -import com.example.orangesale_05.adapter.ProductAdapter; -import com.example.orangesale_05.entity.Product; +import com.example.orangesale_end.R; +import com.example.orangesale_end.adapter.ProductAdapter; +import com.example.orangesale_end.entity.OrangeProductPack; +import com.example.orangesale_end.entity.Product; +import com.example.orangesale_end.netrequest.OkHttpClientProduct; +import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; + public class IndexFragment extends Fragment implements View.OnClickListener { private SearchView searchView; private LinearLayout orangeLine, youziLine, juziLine, xiguaLine, liLine, appleLine, lemonLine, mangguoLine; private GridView gridView; - private List productList; private ProductAdapter productAdapter; + private List productList; + private List list = new ArrayList<>(); @Nullable @Override @@ -41,7 +47,7 @@ public class IndexFragment extends Fragment implements View.OnClickListener { private void init(View view) { searchView = view.findViewById(R.id.searchView); searchView.setOnClickListener(this); - orangeLine = view.findViewById(R.id.baocui); + orangeLine = view.findViewById(R.id.bacui); orangeLine.setOnClickListener(this); youziLine = view.findViewById(R.id.sanmingzhi); youziLine.setOnClickListener(this); @@ -55,12 +61,11 @@ public class IndexFragment extends Fragment implements View.OnClickListener { lemonLine.setOnClickListener(this); mangguoLine = view.findViewById(R.id.shousimianbao); mangguoLine.setOnClickListener(this); - mangguoLine = view.findViewById(R.id.taosu); - mangguoLine.setOnClickListener(this); + appleLine = view.findViewById(R.id.taosu); + appleLine.setOnClickListener(this); gridView = view.findViewById(R.id.index_famous_gridview); - initData(); - productAdapter = new ProductAdapter(getActivity(), productList); - gridView.setAdapter(productAdapter); + //initData(); + new SearchProductTask().execute(); } @@ -114,5 +119,29 @@ public class IndexFragment extends Fragment implements View.OnClickListener { productList.add(product5); productList.add(product6); productList.add(product7); + + } + + /** + * 发送网络请求获取数据 + */ + class SearchProductTask extends AsyncTask> { + + @Override + protected List doInBackground(Void... voids) { + OkHttpClientProduct clientProduct = new OkHttpClientProduct(); + try { + list = clientProduct.getProductPack(); + } catch (IOException e) { + e.printStackTrace(); + } + return list; + } + + @Override + protected void onPostExecute(List orangeProducts) { + productAdapter = new ProductAdapter(getActivity(), list); + gridView.setAdapter(productAdapter); + } } } diff --git a/app/src/main/java/com/example/orangesale_05/fragment/PearsonFragment.java b/app/src/main/java/com/example/orangesale_end/fragment/PearsonFragment.java similarity index 93% rename from app/src/main/java/com/example/orangesale_05/fragment/PearsonFragment.java rename to app/src/main/java/com/example/orangesale_end/fragment/PearsonFragment.java index 8f997bf..2027e96 100644 --- a/app/src/main/java/com/example/orangesale_05/fragment/PearsonFragment.java +++ b/app/src/main/java/com/example/orangesale_end/fragment/PearsonFragment.java @@ -1,4 +1,4 @@ -package com.example.orangesale_05.fragment; +package com.example.orangesale_end.fragment; import android.app.Fragment; import android.os.Bundle; @@ -12,7 +12,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import com.example.orangesale_05.R; +import com.example.orangesale_end.R; public class PearsonFragment extends Fragment implements View.OnClickListener { private ImageView userIconImage; diff --git a/app/src/main/java/com/example/orangesale_end/fragment/ProductFragment.java b/app/src/main/java/com/example/orangesale_end/fragment/ProductFragment.java new file mode 100644 index 0000000..6b8584b --- /dev/null +++ b/app/src/main/java/com/example/orangesale_end/fragment/ProductFragment.java @@ -0,0 +1,102 @@ +package com.example.orangesale_end.fragment; + +import android.app.Fragment; +import android.os.AsyncTask; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.GridView; +import android.widget.Spinner; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.example.orangesale_end.R; +import com.example.orangesale_end.adapter.ListViewAdapter; +import com.example.orangesale_end.adapter.ProductAdapter; +import com.example.orangesale_end.entity.Condition; +import com.example.orangesale_end.entity.OrangeProductPack; +import com.example.orangesale_end.netrequest.OkHttpClientProduct; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class ProductFragment extends Fragment { + private Spinner conditonListSpinner; + private ListViewAdapter listViewAdapter; + private List conditionList; + private GridView productGridView; + private List orangeProductList = new ArrayList<>(); + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + View view = LayoutInflater.from(getActivity()).inflate(R.layout.content_product, container, false); + init(view); + return view; + } + + /** + * 组件初始化方法 + * + * @param view + */ + private void init(View view) { + conditonListSpinner = view.findViewById(R.id.product_select_condition); + initCondList(); + listViewAdapter = new ListViewAdapter(getActivity(), conditionList); + conditonListSpinner.setAdapter(listViewAdapter); + productGridView = view.findViewById(R.id.product_list); + new SearchProductTask().execute(); + } + + /** + * 初始化conditionList + */ + private void initCondList() { + conditionList = new ArrayList<>(); + Condition allCondition = new Condition(); + allCondition.setConditionIcon(R.drawable.all); + allCondition.setConditionName("全部"); + Condition saleCondition = new Condition(); + saleCondition.setConditionIcon(R.drawable.salenum); + saleCondition.setConditionName("按销量高低排序"); + Condition timeCondition = new Condition(); + timeCondition.setConditionIcon(R.drawable.time); + timeCondition.setConditionName("按上市时间排序"); + Condition priceCondition = new Condition(); + priceCondition.setConditionIcon(R.drawable.price); + priceCondition.setConditionName("按商品价格排序"); + conditionList.add(allCondition); + conditionList.add(saleCondition); + conditionList.add(timeCondition); + conditionList.add(priceCondition); + } + + /** + * 发送网络请求获取数据 + */ + class SearchProductTask extends AsyncTask> { + + @Override + protected List doInBackground(Void... voids) { + OkHttpClientProduct clientProduct = new OkHttpClientProduct(); + try { + orangeProductList = clientProduct.getProductPack(); + } catch (IOException e) { + e.printStackTrace(); + } + return orangeProductList; + } + + @Override + protected void onPostExecute(List orangeProducts) { + ProductAdapter productAdapter = new ProductAdapter(getActivity(), orangeProductList); + productGridView.setAdapter(productAdapter); + } + } + + +} diff --git a/app/src/main/java/com/example/orangesale_05/fragment/SetDetailFragment.java b/app/src/main/java/com/example/orangesale_end/fragment/SetDetailFragment.java similarity index 89% rename from app/src/main/java/com/example/orangesale_05/fragment/SetDetailFragment.java rename to app/src/main/java/com/example/orangesale_end/fragment/SetDetailFragment.java index 7b9e60f..2541eac 100644 --- a/app/src/main/java/com/example/orangesale_05/fragment/SetDetailFragment.java +++ b/app/src/main/java/com/example/orangesale_end/fragment/SetDetailFragment.java @@ -1,4 +1,4 @@ -package com.example.orangesale_05.fragment; +package com.example.orangesale_end.fragment; import android.annotation.SuppressLint; import android.app.Fragment; @@ -13,8 +13,8 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import com.example.orangesale_05.R; -import com.example.orangesale_05.activity.CategoryActivity; +import com.example.orangesale_end.R; +import com.example.orangesale_end.activity.CategoryActivity; import java.util.Objects; diff --git a/app/src/main/java/com/example/orangesale_end/fragment/ShoppingCartFragment.java b/app/src/main/java/com/example/orangesale_end/fragment/ShoppingCartFragment.java new file mode 100644 index 0000000..6dcfd22 --- /dev/null +++ b/app/src/main/java/com/example/orangesale_end/fragment/ShoppingCartFragment.java @@ -0,0 +1,61 @@ +package com.example.orangesale_end.fragment; + +import android.app.Fragment; +import android.app.FragmentTransaction; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.example.orangesale_end.R; +import com.example.orangesale_end.entity.ShoppingCart; + +import java.util.ArrayList; +import java.util.List; + +public class ShoppingCartFragment extends Fragment implements View.OnClickListener { + private Button walkButton; + private List cartList = new ArrayList<>(); + private ProductFragment productFragment; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + View view; + if (cartList.size() > 0) { + //有商品 + view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_have_product, container, false); + } else { + view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_no_product, container, false); + } + init(view); + return view; + } + + /** + * 组件初始化 + */ + private void init(View view) { + walkButton = view.findViewById(R.id.random_search); + walkButton.setOnClickListener(this); + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.random_search: + //开启事务,fragment的控制是由事务来实现的 + FragmentTransaction transaction = getFragmentManager().beginTransaction(); + if (productFragment == null) { + productFragment = new ProductFragment(); + } + transaction.replace(R.id.main_content0, productFragment); + transaction.commit(); + break; + } + } +} diff --git a/app/src/main/java/com/example/orangesale_end/netrequest/OkHttpClientProduct.java b/app/src/main/java/com/example/orangesale_end/netrequest/OkHttpClientProduct.java new file mode 100644 index 0000000..7708172 --- /dev/null +++ b/app/src/main/java/com/example/orangesale_end/netrequest/OkHttpClientProduct.java @@ -0,0 +1,80 @@ +package com.example.orangesale_end.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.example.orangesale_end.entity.OrangeProduct; +import com.example.orangesale_end.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 getProduct() throws IOException { + OkHttpClient okHttpClient = new OkHttpClient(); + Request request = new Request.Builder() + .url("http://10.21.221.183:8081/orange/product/search") + .build(); + Response response = okHttpClient.newCall(request).execute(); + JSONObject jsonObject = JSON.parseObject(Objects.requireNonNull(response.body()).string()); + List list = JSON.parseObject(jsonObject.getString("data"), new TypeReference>() { + }); + 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 getProductPack() throws IOException { + List packList = new ArrayList<>(); + List 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; + } +} diff --git a/app/src/main/java/com/example/orangesale_end/netrequest/OkHttpMessage.java b/app/src/main/java/com/example/orangesale_end/netrequest/OkHttpMessage.java new file mode 100644 index 0000000..92e772e --- /dev/null +++ b/app/src/main/java/com/example/orangesale_end/netrequest/OkHttpMessage.java @@ -0,0 +1,39 @@ +package com.example.orangesale_end.netrequest; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.example.orangesale_end.entity.OrangeMessage; + +import java.io.IOException; +import java.util.Objects; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +public class OkHttpMessage { + + /** + * 消费消息 + * + * @return + */ + public OrangeMessage consumeMessage() throws IOException { + OkHttpClient okHttpClient = new OkHttpClient(); + Request request = new Request.Builder() + .url("http://10.21.221.183:8081/orange/message/consumeMessage") + .build(); + Response response = okHttpClient.newCall(request).execute(); + JSONObject jsonObject = JSON.parseObject(Objects.requireNonNull(response.body()).string()); + OrangeMessage orangeMessage = JSON.toJavaObject(jsonObject.getJSONObject("data"), OrangeMessage.class); + /** + * 消费完消息以后,设置消息为已读 + */ + Request request1 = new Request.Builder() + .url("http://10.21.221.183:8081/orange/message/updateMessage") + .build(); + okHttpClient.newCall(request1).execute(); + return orangeMessage; + } + +} diff --git a/app/src/main/java/com/example/orangesale_end/netrequest/OkHttpUser.java b/app/src/main/java/com/example/orangesale_end/netrequest/OkHttpUser.java new file mode 100644 index 0000000..d190cc6 --- /dev/null +++ b/app/src/main/java/com/example/orangesale_end/netrequest/OkHttpUser.java @@ -0,0 +1,71 @@ +package com.example.orangesale_end.netrequest; + +import android.util.Log; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.example.orangesale_end.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://10.21.221.183:8081/orange/user/login") + .post(requestBody) + .build(); + + Response response = okHttpClient.newCall(request).execute(); + + String responseStr = response.body().string(); + System.out.println("responseStr = " + responseStr); + JSONObject jsonObject = JSON.parseObject(responseStr); + 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://10.21.221.183: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"); + } + + +} diff --git a/app/src/main/java/com/example/orangesale_end/service/TimeService.java b/app/src/main/java/com/example/orangesale_end/service/TimeService.java new file mode 100644 index 0000000..b3e916f --- /dev/null +++ b/app/src/main/java/com/example/orangesale_end/service/TimeService.java @@ -0,0 +1,105 @@ +package com.example.orangesale_end.service; + +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; +import android.os.IBinder; +import android.util.Log; + +import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; + +import com.example.orangesale_end.R; +import com.example.orangesale_end.entity.OrangeMessage; +import com.example.orangesale_end.netrequest.OkHttpMessage; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Objects; +import java.util.Timer; +import java.util.TimerTask; + +public class TimeService extends Service { + private static Timer timer = null; + private NotificationManager manager; + private NotificationCompat.Builder builder; + + @Nullable + @Override + public IBinder onBind(Intent intent) { + return null; + } + + /** + * 清除通知 + */ + private void cleanAllNotification() { + manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + manager.cancelAll(); + if (timer != null) { + timer.cancel(); + timer = null; + } + + } + + /** + * 添加通知 + */ + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + long period = 60 * 1000; //1分钟一个周期 + if (timer == null) { + timer = new Timer(); + } + timer.schedule(new TimerTask() { + @Override + public void run() { + new ConsumeMessageTask().execute(); + } + }, 0, period); + + return super.onStartCommand(intent, flags, startId); + } + + class ConsumeMessageTask extends AsyncTask { + @Override + protected Void doInBackground(Void... voids) { + /** + * NotificationChannel是android8.0新增的特性,如果App的targetSDKVersion>=26, + * 没有设置channel通知渠道的话,就会导致通知无法展示。 + * 报错内容:Failed to post notification on channel “null” + */ + OkHttpMessage okHttpMessage = new OkHttpMessage(); + OrangeMessage orangeMessage; + try { + orangeMessage = okHttpMessage.consumeMessage(); + if (!Objects.isNull(orangeMessage)) { + Log.i("orangeMessage", "run: " + orangeMessage.toString()); + NotificationChannel channel = new NotificationChannel("channel", "通知", NotificationManager.IMPORTANCE_DEFAULT); + manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + manager.createNotificationChannel(channel); + builder = new NotificationCompat.Builder(TimeService.this); + Date date = new Date(); + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + builder.setContentTitle("来自橙一色的系统通知消息" + " " + formatter.format(date)); + builder.setSmallIcon(R.drawable.chengzi); + builder.setChannelId("channel"); + builder.setContentText(orangeMessage.getContent()); + builder.setAutoCancel(true); + builder.setDefaults(Notification.DEFAULT_ALL); + Notification notification = builder.build(); + manager.notify(1, notification); + } + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + } +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_background.xml b/app/src/main/res/drawable-v24/ic_launcher_background.xml deleted file mode 100644 index eed7a42..0000000 --- a/app/src/main/res/drawable-v24/ic_launcher_background.xml +++ /dev/null @@ -1,170 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/drawable-v24/address.png b/app/src/main/res/drawable/address.png similarity index 100% rename from app/src/main/res/drawable-v24/address.png rename to app/src/main/res/drawable/address.png diff --git a/app/src/main/res/drawable/all.png b/app/src/main/res/drawable/all.png new file mode 100644 index 0000000..131c4fd Binary files /dev/null and b/app/src/main/res/drawable/all.png differ diff --git a/app/src/main/res/drawable-v24/apple.png b/app/src/main/res/drawable/apple.png similarity index 100% rename from app/src/main/res/drawable-v24/apple.png rename to app/src/main/res/drawable/apple.png diff --git a/app/src/main/res/drawable-v24/applep.png b/app/src/main/res/drawable/applep.png similarity index 100% rename from app/src/main/res/drawable-v24/applep.png rename to app/src/main/res/drawable/applep.png diff --git a/app/src/main/res/drawable-v24/arrow_down.png b/app/src/main/res/drawable/arrow_down.png similarity index 100% rename from app/src/main/res/drawable-v24/arrow_down.png rename to app/src/main/res/drawable/arrow_down.png diff --git a/app/src/main/res/drawable-v24/arrow_left.png b/app/src/main/res/drawable/arrow_left.png similarity index 100% rename from app/src/main/res/drawable-v24/arrow_left.png rename to app/src/main/res/drawable/arrow_left.png diff --git a/app/src/main/res/drawable-v24/arrow_right.png b/app/src/main/res/drawable/arrow_right.png similarity index 100% rename from app/src/main/res/drawable-v24/arrow_right.png rename to app/src/main/res/drawable/arrow_right.png diff --git a/app/src/main/res/drawable-v24/button_login.xml b/app/src/main/res/drawable/button_login.xml similarity index 100% rename from app/src/main/res/drawable-v24/button_login.xml rename to app/src/main/res/drawable/button_login.xml diff --git a/app/src/main/res/drawable-v24/button_register.xml b/app/src/main/res/drawable/button_register.xml similarity index 100% rename from app/src/main/res/drawable-v24/button_register.xml rename to app/src/main/res/drawable/button_register.xml diff --git a/app/src/main/res/drawable/cart.png b/app/src/main/res/drawable/cart.png new file mode 100644 index 0000000..f2e1190 Binary files /dev/null and b/app/src/main/res/drawable/cart.png differ diff --git a/app/src/main/res/drawable-v24/chengzi.png b/app/src/main/res/drawable/chengzi.png similarity index 100% rename from app/src/main/res/drawable-v24/chengzi.png rename to app/src/main/res/drawable/chengzi.png diff --git a/app/src/main/res/drawable-v24/city.png b/app/src/main/res/drawable/city.png similarity index 100% rename from app/src/main/res/drawable-v24/city.png rename to app/src/main/res/drawable/city.png diff --git a/app/src/main/res/drawable/dangao.JPG b/app/src/main/res/drawable/dangao.png similarity index 100% rename from app/src/main/res/drawable/dangao.JPG rename to app/src/main/res/drawable/dangao.png diff --git a/app/src/main/res/drawable/ic_launcher_foreground.xml b/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..971add5 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable-v24/index.png b/app/src/main/res/drawable/index.png similarity index 100% rename from app/src/main/res/drawable-v24/index.png rename to app/src/main/res/drawable/index.png diff --git a/app/src/main/res/drawable-v24/index_menu.xml b/app/src/main/res/drawable/index_menu.xml similarity index 100% rename from app/src/main/res/drawable-v24/index_menu.xml rename to app/src/main/res/drawable/index_menu.xml diff --git a/app/src/main/res/drawable-v24/juzi.png b/app/src/main/res/drawable/juzi.png similarity index 100% rename from app/src/main/res/drawable-v24/juzi.png rename to app/src/main/res/drawable/juzi.png diff --git a/app/src/main/res/drawable-v24/juzip.png b/app/src/main/res/drawable/juzip.png similarity index 100% rename from app/src/main/res/drawable-v24/juzip.png rename to app/src/main/res/drawable/juzip.png diff --git a/app/src/main/res/drawable-v24/lemon.png b/app/src/main/res/drawable/lemon.png similarity index 100% rename from app/src/main/res/drawable-v24/lemon.png rename to app/src/main/res/drawable/lemon.png diff --git a/app/src/main/res/drawable-v24/lemonp.png b/app/src/main/res/drawable/lemonp.png similarity index 100% rename from app/src/main/res/drawable-v24/lemonp.png rename to app/src/main/res/drawable/lemonp.png diff --git a/app/src/main/res/drawable-v24/li.png b/app/src/main/res/drawable/li.png similarity index 100% rename from app/src/main/res/drawable-v24/li.png rename to app/src/main/res/drawable/li.png diff --git a/app/src/main/res/drawable-v24/mangguo.png b/app/src/main/res/drawable/mangguo.png similarity index 100% rename from app/src/main/res/drawable-v24/mangguo.png rename to app/src/main/res/drawable/mangguo.png diff --git a/app/src/main/res/drawable-v24/orange.png b/app/src/main/res/drawable/orange.png similarity index 100% rename from app/src/main/res/drawable-v24/orange.png rename to app/src/main/res/drawable/orange.png diff --git a/app/src/main/res/drawable-v24/password.png b/app/src/main/res/drawable/password.png similarity index 100% rename from app/src/main/res/drawable-v24/password.png rename to app/src/main/res/drawable/password.png diff --git a/app/src/main/res/drawable-v24/pearson.png b/app/src/main/res/drawable/pearson.png similarity index 100% rename from app/src/main/res/drawable-v24/pearson.png rename to app/src/main/res/drawable/pearson.png diff --git a/app/src/main/res/drawable/price.png b/app/src/main/res/drawable/price.png new file mode 100644 index 0000000..c7a125c Binary files /dev/null and b/app/src/main/res/drawable/price.png differ diff --git a/app/src/main/res/drawable-v24/product.png b/app/src/main/res/drawable/product.png similarity index 100% rename from app/src/main/res/drawable-v24/product.png rename to app/src/main/res/drawable/product.png diff --git a/app/src/main/res/drawable/salenum.png b/app/src/main/res/drawable/salenum.png new file mode 100644 index 0000000..87ed2c9 Binary files /dev/null and b/app/src/main/res/drawable/salenum.png differ diff --git a/app/src/main/res/drawable-v24/search.png b/app/src/main/res/drawable/search.png similarity index 100% rename from app/src/main/res/drawable-v24/search.png rename to app/src/main/res/drawable/search.png diff --git a/app/src/main/res/drawable-v24/setting.png b/app/src/main/res/drawable/setting.png similarity index 100% rename from app/src/main/res/drawable-v24/setting.png rename to app/src/main/res/drawable/setting.png diff --git a/app/src/main/res/drawable-v24/sex.png b/app/src/main/res/drawable/sex.png similarity index 100% rename from app/src/main/res/drawable-v24/sex.png rename to app/src/main/res/drawable/sex.png diff --git a/app/src/main/res/drawable-v24/shoppingcart.png b/app/src/main/res/drawable/shoppingcart.png similarity index 100% rename from app/src/main/res/drawable-v24/shoppingcart.png rename to app/src/main/res/drawable/shoppingcart.png diff --git a/app/src/main/res/drawable-v24/shuiguo.png b/app/src/main/res/drawable/shuiguo.png similarity index 100% rename from app/src/main/res/drawable-v24/shuiguo.png rename to app/src/main/res/drawable/shuiguo.png diff --git a/app/src/main/res/drawable-v24/spinner_drop_down_shape.xml b/app/src/main/res/drawable/spinner_drop_down_shape.xml similarity index 100% rename from app/src/main/res/drawable-v24/spinner_drop_down_shape.xml rename to app/src/main/res/drawable/spinner_drop_down_shape.xml diff --git a/app/src/main/res/drawable-v24/sure_apssword.png b/app/src/main/res/drawable/sure_apssword.png similarity index 100% rename from app/src/main/res/drawable-v24/sure_apssword.png rename to app/src/main/res/drawable/sure_apssword.png diff --git a/app/src/main/res/drawable-v24/tab_menu_text.xml b/app/src/main/res/drawable/tab_menu_text.xml similarity index 100% rename from app/src/main/res/drawable-v24/tab_menu_text.xml rename to app/src/main/res/drawable/tab_menu_text.xml diff --git a/app/src/main/res/drawable/tiantianquan.JPG b/app/src/main/res/drawable/tiantianquan.JPG deleted file mode 100644 index c1c4f25..0000000 Binary files a/app/src/main/res/drawable/tiantianquan.JPG and /dev/null differ diff --git a/app/src/main/res/drawable/time.png b/app/src/main/res/drawable/time.png new file mode 100644 index 0000000..8a672eb Binary files /dev/null and b/app/src/main/res/drawable/time.png differ diff --git a/app/src/main/res/drawable-v24/user.png b/app/src/main/res/drawable/user.png similarity index 100% rename from app/src/main/res/drawable-v24/user.png rename to app/src/main/res/drawable/user.png diff --git a/app/src/main/res/drawable-v24/user_account.png b/app/src/main/res/drawable/user_account.png similarity index 100% rename from app/src/main/res/drawable-v24/user_account.png rename to app/src/main/res/drawable/user_account.png diff --git a/app/src/main/res/drawable-v24/user_general.png b/app/src/main/res/drawable/user_general.png similarity index 100% rename from app/src/main/res/drawable-v24/user_general.png rename to app/src/main/res/drawable/user_general.png diff --git a/app/src/main/res/drawable-v24/user_icon.png b/app/src/main/res/drawable/user_icon.png similarity index 100% rename from app/src/main/res/drawable-v24/user_icon.png rename to app/src/main/res/drawable/user_icon.png diff --git a/app/src/main/res/drawable-v24/user_pay.png b/app/src/main/res/drawable/user_pay.png similarity index 100% rename from app/src/main/res/drawable-v24/user_pay.png rename to app/src/main/res/drawable/user_pay.png diff --git a/app/src/main/res/drawable-v24/user_sex.png b/app/src/main/res/drawable/user_sex.png similarity index 100% rename from app/src/main/res/drawable-v24/user_sex.png rename to app/src/main/res/drawable/user_sex.png diff --git a/app/src/main/res/drawable-v24/xigua.png b/app/src/main/res/drawable/xigua.png similarity index 100% rename from app/src/main/res/drawable-v24/xigua.png rename to app/src/main/res/drawable/xigua.png diff --git a/app/src/main/res/drawable-v24/xiguap.png b/app/src/main/res/drawable/xiguap.png similarity index 100% rename from app/src/main/res/drawable-v24/xiguap.png rename to app/src/main/res/drawable/xiguap.png diff --git a/app/src/main/res/drawable-v24/youzi.png b/app/src/main/res/drawable/youzi.png similarity index 100% rename from app/src/main/res/drawable-v24/youzi.png rename to app/src/main/res/drawable/youzi.png diff --git a/app/src/main/res/drawable-v24/youzip.png b/app/src/main/res/drawable/youzip.png similarity index 100% rename from app/src/main/res/drawable-v24/youzip.png rename to app/src/main/res/drawable/youzip.png diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8efbc79..b9990ed 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -15,7 +15,7 @@ --> diff --git a/app/src/main/res/layout/cart_have_product.xml b/app/src/main/res/layout/cart_have_product.xml new file mode 100644 index 0000000..c1d9101 --- /dev/null +++ b/app/src/main/res/layout/cart_have_product.xml @@ -0,0 +1,15 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/cart_no_product.xml b/app/src/main/res/layout/cart_no_product.xml new file mode 100644 index 0000000..9f4a2f1 --- /dev/null +++ b/app/src/main/res/layout/cart_no_product.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + +