summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/jopek/applicationdevrification/Requests.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/com/jopek/applicationdevrification/Requests.java')
-rw-r--r--app/src/main/java/com/jopek/applicationdevrification/Requests.java105
1 files changed, 105 insertions, 0 deletions
diff --git a/app/src/main/java/com/jopek/applicationdevrification/Requests.java b/app/src/main/java/com/jopek/applicationdevrification/Requests.java
new file mode 100644
index 0000000..3f494b6
--- /dev/null
+++ b/app/src/main/java/com/jopek/applicationdevrification/Requests.java
@@ -0,0 +1,105 @@
+package com.jopek.applicationdevrification;
+
+import android.os.AsyncTask;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.function.Consumer;
+
+//class RequestImage extends AsyncTask<String, Void, Bitmap> {
+// ImageView bmImage;
+// Consumer<Bitmap> cb;
+//
+// public void setRemoteSrc(ImageView bmImage, String url) {
+// this.bmImage = bmImage;
+// this.execute(url);
+// }
+//
+// public void getBitmap(String url, Consumer<Bitmap> cb) {
+// this.cb = cb;
+// this.execute(url);
+// }
+//
+// protected Bitmap doInBackground(String... urls) {
+// String urldisplay = urls[0];
+// Bitmap bmp = null;
+// try {
+// InputStream in = new URL(urldisplay).openStream();
+// bmp = BitmapFactory.decodeStream(in);
+// } catch (Exception e) {
+// e.printStackTrace();
+// }
+// return bmp;
+// }
+//
+// protected void onPostExecute(Bitmap result) {
+// if (cb == null)
+// bmImage.setImageBitmap(result);
+// else cb.accept(result);
+// }
+//}
+
+public class Requests extends AsyncTask<String, Integer, String> {
+ Consumer<String> cb;
+ Consumer<JSONObject> cbJson;
+ boolean retJson = false;
+
+ public void get(String url, Consumer<String> cb) {
+ this.cb = cb;
+ this.execute(url);
+ }
+
+ public void getJson(String url, Consumer<JSONObject> cb) {
+ this.cbJson = cb;
+ this.retJson = true;
+ this.execute(url);
+ }
+
+ protected String doInBackground(String... urls) {
+ InputStream in = null;
+ try {
+ URL url = new URL(urls[0]);
+ in = url.openStream();
+ BufferedReader rd = new BufferedReader(new InputStreamReader(in));
+ String content = "", line;
+ while ((line = rd.readLine()) != null) {
+ content += line + "\n";
+ }
+ in.close();
+ return content;
+ } catch (IOException exception) {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ exception.printStackTrace();
+ return "ERROR";
+ }
+ }
+
+ protected void onProgressUpdate(Integer... progress) {
+ }
+
+ protected void onPostExecute(String result) {
+ // this is executed on the main thread after the process is over
+ // update your UI here
+ if (retJson) {
+ try {
+ cbJson.accept(new JSONObject(result));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ } else {
+ cb.accept(result);
+ }
+ }
+}