Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <uses-permission android:name="android.permission.INTERNET" />
- public class GetAsync extends AsyncTask<String, String, String> {
- private Map<String, String> methods;
- private JSONObject jsonObject;
- private CallBackJSON callBackJSON;
- public GetAsync(Map<String, String> methods, CallBackJSON callBackJSON) {
- this.methods = methods;
- this.callBackJSON = callBackJSON;
- }
- @Override
- protected String doInBackground(String... strings) {
- try {
- URL url = new URL(strings[0]);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("GET");
- for (String key : methods.keySet()) connection.addRequestProperty(key, methods.get(key));
- InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
- BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
- StringBuilder builder = new StringBuilder();
- String line;
- while ((line = bufferedReader.readLine()) != null){
- builder.append(line);
- }
- jsonObject = new JSONObject(builder.toString());
- }catch (Exception e){ e.printStackTrace();}
- return null;
- }
- @Override
- protected void onPostExecute(String s) {
- super.onPostExecute(s);
- try {
- callBackJSON.onCallBack(jsonObject);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- public class PostAsync extends AsyncTask<String, String, String> {
- private JSONObject jsonObject;
- private CallBackJSON callBack;
- private JSONObject result;
- public PostAsync(JSONObject jsonObject, CallBackJSON callBack) {
- this.jsonObject = jsonObject;
- this.callBack = callBack;
- }
- @Override
- protected String doInBackground(String... strings) {
- try {
- URL url = new URL(strings[0]);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoInput(true);
- httpURLConnection.setDoOutput(true);
- httpURLConnection.addRequestProperty("Content-Type", "application/json");
- httpURLConnection.setRequestProperty("Token", User.getInstance().getToken() + "");
- httpURLConnection.connect();
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
- writer.write(jsonObject.toString());
- writer.flush();
- InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
- BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
- String line;
- StringBuilder builder = new StringBuilder();
- while ((line = bufferedReader.readLine()) != null) {
- builder.append(line);
- }
- result = new JSONObject(builder.toString());
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(String s) {
- super.onPostExecute(s);
- try {
- callBack.onCallBack(result);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- //image
- InputStream in = new URL(strings[0]).openStream();
- bitmap = BitmapFactory.decodeStream(in);
- //adapter
- public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductHolder> {
- private List<IProduct> list;
- private LayoutInflater layoutInflater;
- public ProductAdapter(List<IProduct> list, Context context) {
- this.list= list;
- this.layoutInflater = LayoutInflater.from(context);
- }
- public void setProducts(List<IProduct> list) {
- this.list= list;
- }
- @Override
- public ProductHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View view = layoutInflater.inflate(R.layout.product_item, parent, false);
- return new ProductHolder(view);
- }
- @RequiresApi(api = Build.VERSION_CODES.O)
- @Override
- public void onBindViewHolder(ProductHolder holder, int position) {
- }
- @Override
- public int getItemCount() {
- return колво значений;
- }
- public class ProductHolder extends RecyclerView.ViewHolder{
- public ProductHolder(View itemView) {
- super(itemView);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment