Web Post & Get Requests [Extension]

1

– 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);
  }
}

6 Likes

Oh… Good work and will be very useful one to one and all… especially gsheet lovers… :+1::+1::+1::+1:

1 Like

What exactly is the advantage of using this extension compared to the web component?
Can you please provide an example how to use the PostRequest method?
Taifun

2 Likes

I have already sent the file as an example.
The purpose of this extension is to improve myself. I shared it in case it might be useful to others. There is no advantage :slight_smile:

1 Like

Please add a screenshot of some blocks… this saves time for everyone in deciding to use your extension or just the web component

Taifun

is there any method to get,post request more faster than web component to get data

#off-topic
Internet speed :rofl: :rofl:

1 Like

need more speed than internet speed. :slightly_smiling_face:

Impossible to compile into APK
The aia do not working

Export fail at 0%

Your extension need update …

as the author of this extension said

which means, use the web component directly…
Taifun

1 Like

You can see i share.

But he’s sharing a crashing Aia when i open it …:-1::-1:
It not useful…

This extension is not working

hi @OmerGundogar do you plan to fix the bugs?
alternatively we also can hide the extension here in the community, and advice the users to use the web component directly…

Taifun

3 Likes

If the working extension is not working by your updates, I will not bother for every extension. Delete it.