Skip to content

Commit

Permalink
Added post request functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
F4pl0 committed Sep 7, 2019
1 parent 6948bd0 commit 54f6035
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
15 changes: 13 additions & 2 deletions app/src/main/java/com/f4pl0/farlaexample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}
}
77 changes: 77 additions & 0 deletions farla/src/main/java/com/f4pl0/farla/FarlaPostRequest.java
Original file line number Diff line number Diff line change
@@ -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<String, String> params = new HashMap<String, String>();

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<String>()
{
@Override
public void onResponse(String response) {

}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
}
}
) {
@Override
protected Map<String, String> getParams()
{
return params;
}
};
requestQueue.add(postRequest);
}
}

0 comments on commit 54f6035

Please sign in to comment.