|
@@ -0,0 +1,287 @@
|
|
|
|
+Подключение retrofit
|
|
|
|
+implementation 'com.squareup.retrofit2:retrofit:2.9.0'
|
|
|
|
+implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
|
|
|
|
+
|
|
|
|
+table model
|
|
|
|
+прописать поля
|
|
|
|
+сделать 2 конструктора
|
|
|
|
+сделать конструктор для parcel (source.readInt())
|
|
|
|
+сделать геттеры и сеттеры
|
|
|
|
+сделать describeContents, writeToParcel(dest.writeInt(ID))
|
|
|
|
+прописать creator: public static final Parceable.Creator<tableGamesModel> CREATOR = Parceable.Creator<tableGamesModel>(){}
|
|
|
|
+там переопределить 2 метода createFromParcel (возвращает return new tableGamesModel(source)) и newArray(вместо 0 поставить size)
|
|
|
|
+
|
|
|
|
+adapter
|
|
|
|
+private context mContext;
|
|
|
|
+List<tableModel> tableList;
|
|
|
|
+
|
|
|
|
+Проставить поля генерейт
|
|
|
|
+
|
|
|
|
+public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
+ View v = View.inflate(mContext, R.layout.item_layout, null);
|
|
|
|
+ TextView title = v.findViewById(R.id.titleTV);
|
|
|
|
+ TextView cost = v.findViewById(R.id.costTV);
|
|
|
|
+ TextView stock = v.findViewById(R.id.stockTV);
|
|
|
|
+ TextView store = v.findViewById(R.id.storeTV);
|
|
|
|
+ tableGamesModel games = gamesList.get(position);
|
|
|
|
+ title.setText(games.getTitle());
|
|
|
|
+ cost.setText(String.valueOf(games.getCost()));
|
|
|
|
+ stock.setText(String.valueOf(games.getStockAvailability()));
|
|
|
|
+ store.setText(String.valueOf(games.getAvailabilityInStore()));
|
|
|
|
+ return v;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+retrofit
|
|
|
|
+создаем интерфейс, делаем импорты
|
|
|
|
+import retrofit2.Call;
|
|
|
|
+import retrofit2.http.Body;
|
|
|
|
+import retrofit2.http.DELETE;
|
|
|
|
+import retrofit2.http.GET;
|
|
|
|
+import retrofit2.http.POST;
|
|
|
|
+import retrofit2.http.PUT;
|
|
|
|
+import retrofit2.http.Path;
|
|
|
|
+import retrofit2.http.Query;
|
|
|
|
+Пример написания методов:
|
|
|
|
+ @GET("apps/{id}")
|
|
|
|
+ Call<DataModal> getData(@Query("id") int id);
|
|
|
|
+
|
|
|
|
+ @POST("items")
|
|
|
|
+ Call<DataModal> createPost(@Body DataModal dataModal);
|
|
|
|
+
|
|
|
|
+ @PUT("apps/{id}")
|
|
|
|
+ Call<DataModal> updateData(@Path("id") int id, @Body DataModal dataModal);
|
|
|
|
+
|
|
|
|
+ @DELETE("apps/{id}")
|
|
|
|
+ Call<Void> deleteData(@Path("id") int id);
|
|
|
|
+
|
|
|
|
+для вывода в листвью
|
|
|
|
+
|
|
|
|
+ private adapterView adapter;
|
|
|
|
+ private List<tableGamesModel> gamesList = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ ListView lvApps = findViewById(R.id.gamesLV);
|
|
|
|
+ adapter = new adapterView(main_Activity.this, gamesList);
|
|
|
|
+ lvApps.setAdapter(adapter);
|
|
|
|
+
|
|
|
|
+ private class fillList extends AsyncTask<Void, Void, String>
|
|
|
|
+{
|
|
|
|
+ @Override
|
|
|
|
+ protected String doInBackground(Void... voids)
|
|
|
|
+ {
|
|
|
|
+ try {
|
|
|
|
+ URL url =new URL("https://ngknn.ru:5001/NGKNN/МамшеваЮС/Экзамен/api/TableGames/sortByTableGames?typeOfSort={typeOfSort}&nameProduct={nameProduct}");
|
|
|
|
+ HttpURLConnection connection =(HttpURLConnection)url.openConnection();
|
|
|
|
+
|
|
|
|
+ BufferedReader reader =new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
+
|
|
|
|
+ StringBuffer result = new StringBuffer();
|
|
|
|
+ String line = "";
|
|
|
|
+
|
|
|
|
+ while((line = reader.readLine()) != null)
|
|
|
|
+ {
|
|
|
|
+ result.append(line);
|
|
|
|
+ }
|
|
|
|
+ return result.toString();
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ protected void onPostExecute(String s)
|
|
|
|
+ {
|
|
|
|
+ super.onPostExecute(s);
|
|
|
|
+ try {
|
|
|
|
+ gamesList.clear();
|
|
|
|
+ JSONArray temp =new JSONArray();
|
|
|
|
+ for (int i=0;i <temp.length();i++)
|
|
|
|
+ {
|
|
|
|
+ JSONObject gameJS = temp.getJSONObject(i);
|
|
|
|
+ tableGamesModel tempArr = new tableGamesModel(
|
|
|
|
+ gameJS.getInt("ID"),
|
|
|
|
+ gameJS.getString("Title"),
|
|
|
|
+ gameJS.getInt("Cost"),
|
|
|
|
+ gameJS.getInt("StockAvailability"),
|
|
|
|
+ gameJS.getInt("AvailabilityInStore"),
|
|
|
|
+ gameJS.getString("Description"),
|
|
|
|
+ gameJS.getString("Reviews"),
|
|
|
|
+ gameJS.getString("Image")
|
|
|
|
+ );
|
|
|
|
+ gamesList.add(tempArr);
|
|
|
|
+ adapter.notifyDataSetInvalidated();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e){
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+private Bitmap getUserImage(String encodedImg)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ if(encodedImg!=null&& !encodedImg.equals("null")) {
|
|
|
|
+ byte[] bytes = Base64.decode(encodedImg, Base64.DEFAULT);
|
|
|
|
+ return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+Retrofit retrofit = new Retrofit.Builder()
|
|
|
|
+ .baseUrl("https://ngknn.ru:5001/NGKNN/МорозовАВ/api/")
|
|
|
|
+ .addConverterFactory(GsonConverterFactory.create())
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ RetrofitAPI retrofitAPI = retrofit.create(RetrofitAPI.class);
|
|
|
|
+ Call<DataModal> call = retrofitAPI.getData(MainActivity.index);
|
|
|
|
+ call.enqueue(new Callback<DataModal>() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onResponse(Call<DataModal> call, Response<DataModal> response) {
|
|
|
|
+ id = response.body().getApp_id();
|
|
|
|
+ appName.setText(response.body().getAppName());
|
|
|
|
+ appAgeLimit.setText(Integer.valueOf(response.body().getAppAgeLimit()).toString());
|
|
|
|
+ appPrice.setText(Double.valueOf(response.body().getAppPrice()).toString());
|
|
|
|
+ encodedImage = response.body().getAppImage();
|
|
|
|
+ if(response.body().getAppImage() == null)
|
|
|
|
+ {
|
|
|
|
+ image.setImageResource(R.drawable.empty);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ image.setImageBitmap(getImgBitmap(response.body().getAppImage()));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onFailure(Call<DataModal> call, Throwable t) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+image = findViewById(R.id.appImageIV);
|
|
|
|
+ image.setOnClickListener(v -> {
|
|
|
|
+ Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
|
|
|
+ intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
+ pickImg.launch(intent); });
|
|
|
|
+
|
|
|
|
+private final ActivityResultLauncher<Intent> pickImg = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
|
|
|
|
+ if (result.getResultCode() == RESULT_OK) {
|
|
|
|
+ if (result.getData() != null) {
|
|
|
|
+ Uri uri = result.getData().getData();
|
|
|
|
+ try {
|
|
|
|
+ InputStream is = getContentResolver().openInputStream(uri);
|
|
|
|
+ Bitmap bitmap = BitmapFactory.decodeStream(is);
|
|
|
|
+ image.setImageBitmap(bitmap);
|
|
|
|
+ encodedImage = encodeImage(bitmap);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ private String encodeImage(Bitmap bitmap) {
|
|
|
|
+ int prevW = 150;
|
|
|
|
+ int prevH = bitmap.getHeight() * prevW / bitmap.getWidth();
|
|
|
|
+ Bitmap b = Bitmap.createScaledBitmap(bitmap, prevW, prevH, false);
|
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
|
+ b.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
|
|
|
|
+ byte[] bytes = byteArrayOutputStream.toByteArray();
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
+ return Base64.getEncoder().encodeToString(bytes);
|
|
|
|
+ }
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Bitmap getImgBitmap(String encodedImg) {
|
|
|
|
+ if (encodedImg != null) {
|
|
|
|
+ byte[] bytes = new byte[0];
|
|
|
|
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
|
|
|
+ bytes = Base64.getDecoder().decode(encodedImg);
|
|
|
|
+ }
|
|
|
|
+ return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
|
|
|
|
+ }
|
|
|
|
+ return BitmapFactory.decodeResource(editApp.this.getResources(),
|
|
|
|
+ R.drawable.empty);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+private void deleteRecord(int index)
|
|
|
|
+ {
|
|
|
|
+ saveChangesBtn.setEnabled(false);
|
|
|
|
+ deleteBtn.setEnabled(false);
|
|
|
|
+ deletePhoto.setEnabled(false);
|
|
|
|
+
|
|
|
|
+ Retrofit retrofit = new Retrofit.Builder()
|
|
|
|
+ .baseUrl("https://ngknn.ru:5001/NGKNN/МорозовАВ/api/")
|
|
|
|
+ .addConverterFactory(GsonConverterFactory.create())
|
|
|
|
+ .build();
|
|
|
|
+ RetrofitAPI retrofitAPI = retrofit.create(RetrofitAPI.class);
|
|
|
|
+ Call call = retrofitAPI.deleteData(index);
|
|
|
|
+ call.enqueue(new Callback<DataModal>() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onResponse(Call<DataModal> call, Response<DataModal> response) {
|
|
|
|
+ if(!response.isSuccessful())
|
|
|
|
+ {
|
|
|
|
+ Toast.makeText(editApp.this, "При удание записи возникла ошибка", Toast.LENGTH_SHORT).show();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ saveChangesBtn.setEnabled(false);
|
|
|
|
+ deleteBtn.setEnabled(false);
|
|
|
|
+ deletePhoto.setEnabled(false);
|
|
|
|
+
|
|
|
|
+ Toast.makeText(editApp.this, "Удаление прошло успешно", Toast.LENGTH_SHORT).show();
|
|
|
|
+ startActivity(new Intent(editApp.this, MainActivity.class));
|
|
|
|
+ finish();
|
|
|
|
+ }
|
|
|
|
+ @Override
|
|
|
|
+ public void onFailure(Call<DataModal> call, Throwable t) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+private void putChanges(int id, String appName, double appPrice, int appAgeLimit, String picture) {
|
|
|
|
+
|
|
|
|
+ saveChangesBtn.setEnabled(false);
|
|
|
|
+ deleteBtn.setEnabled(false);
|
|
|
|
+ deletePhoto.setEnabled(false);
|
|
|
|
+
|
|
|
|
+ Retrofit retrofit = new Retrofit.Builder()
|
|
|
|
+ .baseUrl("https://ngknn.ru:5001/ngknn/морозовав/api/")
|
|
|
|
+ .addConverterFactory(GsonConverterFactory.create())
|
|
|
|
+ .build();
|
|
|
|
+ RetrofitAPI retrofitAPI = retrofit.create(RetrofitAPI.class);
|
|
|
|
+ DataModal modal = new DataModal(id, appName, appPrice, appAgeLimit, picture);
|
|
|
|
+ Call<DataModal> call = retrofitAPI.updateData(MainActivity.index, modal);
|
|
|
|
+ call.enqueue(new Callback<DataModal>() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onResponse(Call<DataModal> call, Response<DataModal> response) {
|
|
|
|
+ if(response.isSuccessful())
|
|
|
|
+ {
|
|
|
|
+ saveChangesBtn.setEnabled(true);
|
|
|
|
+ deleteBtn.setEnabled(true);
|
|
|
|
+ deletePhoto.setEnabled(true);
|
|
|
|
+
|
|
|
|
+ Toast.makeText(editApp.this, "Изменение прошло успешно", Toast.LENGTH_SHORT).show();
|
|
|
|
+ startActivity(new Intent(editApp.this, MainActivity.class));
|
|
|
|
+ finish();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ Toast.makeText(editApp.this, "Произошла ошибка", Toast.LENGTH_SHORT).show();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onFailure(Call<DataModal> call, Throwable t) {
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|