– Download –
com.ruwis.getpost.aix (217.5 KB)
Requests_Get_Post.aia (345.6 KB)
Codes
package com.ruwis.getpost;
import android.content.Context;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.util.HashMap;
import java.util.Map;
public class GetPost extends AndroidNonvisibleComponent {
private final Context context;
public GetPost(ComponentContainer container) {
super(container.$form());
context = container.$context();
}
@SimpleEvent
public void Post(String response){
EventDispatcher.dispatchEvent(this, "Post", response);
}
@SimpleEvent
public void Get(String response){
EventDispatcher.dispatchEvent(this, "Get", response);
}
@SimpleEvent
public void Error(String error){
EventDispatcher.dispatchEvent(this, "Error", error);
}
@SimpleFunction(description = "parameters & headers example:\n\"surname\": \"bravo\", \"name\": \"johny\"")
public void PostRequest(String url, String parameters, String headers){
parameters = parameters.replaceAll("\",\"", "\", \"");
headers = headers.replaceAll("\",\"", "\", \"");
RequestQueue requestQueue=Volley.newRequestQueue(context);
String finalParameters = parameters;
String finalHeaders = headers;
StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Post(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Error(error.getMessage());
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params=new HashMap<String, String>();
if (!finalParameters.isEmpty())
{
if (finalParameters.contains("\", \""))
{
//split the String by a comma
String[] parts1 = finalParameters.split("\", \"");
//iterate the parts and add them to a map
for(String part1 : parts1){
//split the employee data by : to get id and name
String[] empdata1 = part1.split(":");
String strId = empdata1[0].trim().replaceAll("\"","");
String strName = empdata1[1].trim().replaceAll("\"","");
//add to map
params.put(strId, strName);
}
}
else {
String[] data = finalParameters.split(":");
String strId = data[0].trim().replaceAll("\"","");
String strName = data[1].trim().replaceAll("\"","");
//add to map
params.put(strId, strName);
}
}
return params;
}
@Override
public Map<String,String> getHeaders() throws AuthFailureError{
Map<String,String> params=new HashMap<String, String>();
if (!finalHeaders.isEmpty())
{
if(finalHeaders.contains("\", \""))
{
//split the String by a comma
String[] parts2 = finalHeaders.split("\", \"");
//iterate the parts and add them to a map
for(String part2 : parts2){
//split the employee data by : to get id and name
String[] empdata2 = part2.split(":");
String strId = empdata2[0].trim().replace("\"","");
String strName = empdata2[1].trim().replace("\"","");
//add to map
params.put(strId, strName);
}
}
else {
String[] data = finalHeaders.split(":");
String strId = data[0].trim().replaceAll("\"","");
String strName = data[1].trim().replaceAll("\"","");
//add to map
params.put(strId, strName);
}
}
return params;
}
};
requestQueue.add(stringRequest);
}
@SimpleFunction
public void SendGetRequests(String url){
RequestQueue queue= Volley.newRequestQueue(context);
StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Get(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Error(error.getMessage());
}
});
queue.add(stringRequest);
}
}