Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to make a "Hello World" AWS API Gateway GET API call from Android using Generated SDK

I'm trying to get the AWS API gateway generated SDK to work, I've created an API gateway GET API which works fine when I hit the url from a browser/httpclient, but not with Android.

With Android, I'm trying to use the Android SDK that is generated by AWS API Gateway. The documented example I found is very unclear to me, it uses types and methods which do not exist in the AWS SDK:

http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk-android.html

For example:

//     If the API exposes a 'GET /{a}/{b}/{op}'  method, you can call the following SDK method to invoke the request,

Result.output = client.aBOpGet(a, b, c)

I do not know what aBOpGET is suppose to be, nor do I know what Result is.

while I was able to follow along to build/import the jars, I couldn't follow their code example. So, I came up with this based on browsing the SDK code:

import android.os.AsyncTask;
import android.util.Log;

import com.amazonaws.mobileconnectors.apigateway.ApiClientFactory;
import com.amazonaws.mobileconnectors.apigateway.ApiRequest;
import com.amazonaws.mobileconnectors.apigateway.ApiResponse;

import Client.LambdaMicroserviceClient;


public class APIGatewayHandler extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        //gateway
        ApiClientFactory factory = new ApiClientFactory();
        final LambdaMicroserviceClient client = factory.build(LambdaMicroserviceClient.class);

        ApiRequest request = new ApiRequest("/QueryNetSumMinute");


        ApiResponse apiResponse = client.execute(request);
        int responseCode = apiResponse.getStatusCode();

        android.util.Log.v("Request: ", request.toString());
        android.util.Log.v("response Code: ", String.valueOf(responseCode));
        android.util.Log.v("response Status Text: ",    apiResponse.getStatusText());
        //android.util.Log.v("responseCode: ", String.valueOf(responseCode));
        return null;
    }

    @Override
    public void onPostExecute(Void var) {
        Log.d("onPostExecute", "complete");
    }
}

That runs fine, but on the client response I get a 403 Forbidden error when invoked.

I have info logging enabled on API gateway and I can see detailed information from an http request sent by a browser, but I see nothing from this Android code.

What I suspect is happening, is that the client is hitting the BASE URL of the API without specifying the method "/QueryNetSumMinute" which from testing from a browser does indeed return 403

like image 754
RandomUser Avatar asked Oct 15 '25 17:10

RandomUser


1 Answers

Got it working, incase this helps anyone see below. I had to add request.getPath("/MyAPIName"), and some debugging code:

import android.os.AsyncTask;
import android.util.Log;

import com.amazonaws.mobileconnectors.apigateway.ApiClientFactory;
import com.amazonaws.mobileconnectors.apigateway.ApiRequest;
import com.amazonaws.mobileconnectors.apigateway.ApiResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import Client.LambdaMicroserviceClient;


public class APIGatewayHandler extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        //gateway
        ApiClientFactory factory = new ApiClientFactory();
        final LambdaMicroserviceClient client = factory.build(LambdaMicroserviceClient.class);

        ApiRequest request = new ApiRequest();
request.withPath("/QueryNetSumMinute");

        ApiResponse apiResponse = client.execute(request);
        int responseCode = apiResponse.getStatusCode();

            String responseBody = "empty";
        try {
            responseBody   = convertStreamToString(apiResponse.getRawContent());
        } catch (Exception e){
            Log.d("ERROR ", " failed reading response ");
        }

        android.util.Log.v("Request: ", request.toString());
        android.util.Log.v("response Code: ", String.valueOf(responseCode));
        android.util.Log.v("response Status Text: ",    apiResponse.getStatusText());
        android.util.Log.v("responseBody: ", responseBody);
        return null;
    }

    @Override
    public void onPostExecute(Void var) {
        Log.d("onPostExecute", "complete");
    }

    private String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('\n');
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}
like image 100
RandomUser Avatar answered Oct 18 '25 07:10

RandomUser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!