diff --git a/app/build.gradle b/app/build.gradle index abc8169..5ed5235 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,5 +26,5 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' - implementation 'com.github.F4pl0:FARLA:0.1.0' + implementation 'com.github.F4pl0:FARLA:0.1.1' } diff --git a/app/src/main/java/com/f4pl0/farlaexample/MainActivity.java b/app/src/main/java/com/f4pl0/farlaexample/MainActivity.java index 63f379e..b6704ed 100644 --- a/app/src/main/java/com/f4pl0/farlaexample/MainActivity.java +++ b/app/src/main/java/com/f4pl0/farlaexample/MainActivity.java @@ -14,7 +14,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FARLA farla = new FARLA(); - farla.GET("http://google.com", "asd", new FARLA.onResult() { + farla.GET("http://webhook.site/7a7ca4d3-e601-4950-83bf-47e7bb769dd0", "asd", new FARLA.onResult() { @Override public void resultSuccess(String response) { Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show(); diff --git a/build.gradle b/build.gradle index 5d5a0f8..88dbac9 100644 --- a/build.gradle +++ b/build.gradle @@ -4,6 +4,7 @@ buildscript { repositories { google() jcenter() + maven { url 'https://jitpack.io' } } dependencies { classpath 'com.android.tools.build:gradle:3.5.0' diff --git a/farla/build.gradle b/farla/build.gradle index c7b1b65..5220390 100644 --- a/farla/build.gradle +++ b/farla/build.gradle @@ -31,4 +31,5 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + implementation 'com.android.volley:volley:1.1.1' } diff --git a/farla/src/main/java/com/f4pl0/farla/FARLA.java b/farla/src/main/java/com/f4pl0/farla/FARLA.java deleted file mode 100644 index 13e24b3..0000000 --- a/farla/src/main/java/com/f4pl0/farla/FARLA.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.f4pl0.farla; - -import java.io.BufferedInputStream; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; - -public class FARLA { - - public static int - MALFORMED_URL = 1, - NETWORK_ERROR = 2; - - public interface onResult{ - void resultSuccess(String response); - void resultFailure(int reason); - } - - public void GET(String URL, String body, onResult onResult){ - try{ - java.net.URL url = new URL(URL); - HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); - InputStream in = new BufferedInputStream(urlConnection.getInputStream()); - onResult.resultSuccess(readStream(in)); - urlConnection.disconnect(); - } - catch (MalformedURLException e){ - onResult.resultFailure(MALFORMED_URL); - }catch (IOException e){ - e.printStackTrace(); - onResult.resultFailure(NETWORK_ERROR); - } - } - - private String readStream(InputStream is) throws IOException { - StringBuilder sb = new StringBuilder(); - BufferedReader r = new BufferedReader(new InputStreamReader(is),1000); - for (String line = r.readLine(); line != null; line =r.readLine()){ - sb.append(line); - } - is.close(); - return sb.toString(); - } -} diff --git a/farla/src/main/java/com/f4pl0/farla/FarlaGetRequest.java b/farla/src/main/java/com/f4pl0/farla/FarlaGetRequest.java new file mode 100644 index 0000000..943bd85 --- /dev/null +++ b/farla/src/main/java/com/f4pl0/farla/FarlaGetRequest.java @@ -0,0 +1,58 @@ +package com.f4pl0.farla; + +import android.content.Context; + +import com.android.volley.Request; +import com.android.volley.RequestQueue; +import com.android.volley.Response; +import com.android.volley.VolleyError; +import com.android.volley.toolbox.JsonObjectRequest; +import com.android.volley.toolbox.StringRequest; +import com.android.volley.toolbox.Volley; + +import org.json.JSONObject; + +public class FarlaGetRequest { + + Context context; + RequestQueue requestQueue; + String URL = ""; + onGetRequestListener listener; + + public FarlaGetRequest(Context context) { + this.context = context; + requestQueue = Volley.newRequestQueue(context); + } + + public interface onGetRequestListener{ + void onSuccess(String response); + void onFailure(int error); + } + + FarlaGetRequest setURL(String URL){ + this.URL = URL; + return this; + } + + FarlaGetRequest setListener(onGetRequestListener listener){ + this.listener = listener; + return this; + } + + void execute(){ + StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, + new Response.Listener() { + @Override + public void onResponse(String response) { + listener.onSuccess(response); + } + }, + new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + } + }); + requestQueue.add(stringRequest); + } + +}