From 54f60351d9f9731693a063189a6281e747ba0f4c Mon Sep 17 00:00:00 2001 From: f4pl0 Date: Sat, 7 Sep 2019 16:07:29 +0200 Subject: [PATCH] Added post request functionality --- app/build.gradle | 2 +- .../com/f4pl0/farlaexample/MainActivity.java | 15 +++- .../com/f4pl0/farla/FarlaPostRequest.java | 77 +++++++++++++++++++ 3 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 farla/src/main/java/com/f4pl0/farla/FarlaPostRequest.java diff --git a/app/build.gradle b/app/build.gradle index 52554c8..da6af71 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.2' + implementation 'com.github.F4pl0:FARLA:0.1.3' } diff --git a/app/src/main/java/com/f4pl0/farlaexample/MainActivity.java b/app/src/main/java/com/f4pl0/farlaexample/MainActivity.java index 7a18b93..066593d 100644 --- a/app/src/main/java/com/f4pl0/farlaexample/MainActivity.java +++ b/app/src/main/java/com/f4pl0/farlaexample/MainActivity.java @@ -5,7 +5,6 @@ import android.os.Bundle; import android.widget.Toast; -import com.f4pl0.farla.FARLA; import com.f4pl0.farla.FarlaGetRequest; public class MainActivity extends AppCompatActivity { @@ -14,6 +13,18 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - new FarlaGetRequest(this). + new FarlaGetRequest(this) + .setURL("https://google.com") + .setListener(new FarlaGetRequest.onGetRequestListener() { + @Override + public void onSuccess(String response) { + Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show(); + } + + @Override + public void onFailure(int error) { + + } + }).execute(); } } diff --git a/farla/src/main/java/com/f4pl0/farla/FarlaPostRequest.java b/farla/src/main/java/com/f4pl0/farla/FarlaPostRequest.java new file mode 100644 index 0000000..702cf9b --- /dev/null +++ b/farla/src/main/java/com/f4pl0/farla/FarlaPostRequest.java @@ -0,0 +1,77 @@ +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.StringRequest; +import com.android.volley.toolbox.Volley; + +import java.util.HashMap; +import java.util.Map; + +public class FarlaPostRequest { + + Context context; + RequestQueue requestQueue; + String URL = ""; + onPostRequestListener listener; + Map params = new HashMap(); + + public FarlaPostRequest(Context context) { + this.context = context; + requestQueue = Volley.newRequestQueue(context); + } + + public interface onPostRequestListener{ + void onSuccess(String response); + void onFailure(int error); + } + + public FarlaPostRequest addParam(String key, String value){ + params.put(key, value); + return this; + } + + public FarlaPostRequest removeParam(String key){ + params.remove(key); + return this; + } + + public FarlaPostRequest setURL(String URL){ + this.URL = URL; + return this; + } + + public FarlaPostRequest setListener(onPostRequestListener listener){ + this.listener = listener; + return this; + } + + public void execute(){ + StringRequest postRequest = new StringRequest(Request.Method.POST, URL, + new Response.Listener() + { + @Override + public void onResponse(String response) { + + } + }, + new Response.ErrorListener() + { + @Override + public void onErrorResponse(VolleyError error) { + } + } + ) { + @Override + protected Map getParams() + { + return params; + } + }; + requestQueue.add(postRequest); + } +}