MySoulIsCry

Untitled

Dec 18th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. <uses-permission android:name="android.permission.INTERNET" />
  2.  
  3.  
  4. public class GetAsync extends AsyncTask<String, String, String> {
  5.  
  6. private Map<String, String> methods;
  7. private JSONObject jsonObject;
  8. private CallBackJSON callBackJSON;
  9.  
  10. public GetAsync(Map<String, String> methods, CallBackJSON callBackJSON) {
  11. this.methods = methods;
  12. this.callBackJSON = callBackJSON;
  13. }
  14.  
  15. @Override
  16. protected String doInBackground(String... strings) {
  17. try {
  18. URL url = new URL(strings[0]);
  19. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  20. connection.setRequestMethod("GET");
  21. for (String key : methods.keySet()) connection.addRequestProperty(key, methods.get(key));
  22. InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
  23. BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
  24. StringBuilder builder = new StringBuilder();
  25. String line;
  26. while ((line = bufferedReader.readLine()) != null){
  27. builder.append(line);
  28. }
  29. jsonObject = new JSONObject(builder.toString());
  30. }catch (Exception e){ e.printStackTrace();}
  31. return null;
  32. }
  33.  
  34. @Override
  35. protected void onPostExecute(String s) {
  36. super.onPostExecute(s);
  37. try {
  38. callBackJSON.onCallBack(jsonObject);
  39. } catch (JSONException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44.  
  45.  
  46.  
  47.  
  48. public class PostAsync extends AsyncTask<String, String, String> {
  49.  
  50. private JSONObject jsonObject;
  51. private CallBackJSON callBack;
  52. private JSONObject result;
  53.  
  54.  
  55. public PostAsync(JSONObject jsonObject, CallBackJSON callBack) {
  56. this.jsonObject = jsonObject;
  57. this.callBack = callBack;
  58. }
  59.  
  60. @Override
  61. protected String doInBackground(String... strings) {
  62. try {
  63. URL url = new URL(strings[0]);
  64. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  65. httpURLConnection.setRequestMethod("POST");
  66. httpURLConnection.setDoInput(true);
  67. httpURLConnection.setDoOutput(true);
  68. httpURLConnection.addRequestProperty("Content-Type", "application/json");
  69. httpURLConnection.setRequestProperty("Token", User.getInstance().getToken() + "");
  70. httpURLConnection.connect();
  71. OutputStream outputStream = httpURLConnection.getOutputStream();
  72. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
  73. writer.write(jsonObject.toString());
  74. writer.flush();
  75. InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
  76. BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
  77. String line;
  78. StringBuilder builder = new StringBuilder();
  79. while ((line = bufferedReader.readLine()) != null) {
  80. builder.append(line);
  81. }
  82. result = new JSONObject(builder.toString());
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. }
  86. return null;
  87. }
  88.  
  89. @Override
  90. protected void onPostExecute(String s) {
  91. super.onPostExecute(s);
  92. try {
  93. callBack.onCallBack(result);
  94. } catch (JSONException e) {
  95. e.printStackTrace();
  96. }
  97. }
  98.  
  99.  
  100. }
  101.  
  102.  
  103. //image
  104. InputStream in = new URL(strings[0]).openStream();
  105. bitmap = BitmapFactory.decodeStream(in);
  106.  
  107.  
  108.  
  109. //adapter
  110.  
  111.  
  112. public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductHolder> {
  113.  
  114. private List<IProduct> list;
  115. private LayoutInflater layoutInflater;
  116.  
  117. public ProductAdapter(List<IProduct> list, Context context) {
  118. this.list= list;
  119. this.layoutInflater = LayoutInflater.from(context);
  120. }
  121.  
  122. public void setProducts(List<IProduct> list) {
  123. this.list= list;
  124. }
  125.  
  126. @Override
  127. public ProductHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  128. View view = layoutInflater.inflate(R.layout.product_item, parent, false);
  129. return new ProductHolder(view);
  130. }
  131.  
  132. @RequiresApi(api = Build.VERSION_CODES.O)
  133. @Override
  134. public void onBindViewHolder(ProductHolder holder, int position) {
  135.  
  136. }
  137.  
  138.  
  139. @Override
  140. public int getItemCount() {
  141.  
  142.  
  143. return колво значений;
  144. }
  145.  
  146. public class ProductHolder extends RecyclerView.ViewHolder{
  147.  
  148. public ProductHolder(View itemView) {
  149. super(itemView);
  150.  
  151. }
  152.  
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment