How to implement aws api gateway into android? - android

So i deployed an api and imported the sdk into android. I tried to perform a simple GET method. Where I input a userName and it returns to me the user's lastName.
The lambda function (backend function) is called, so i am able to connect to it, but im not able to get the output.
Here is my class:
public class SampleGet extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
gatewayButton = (Button) findViewById(R.id.cognito_gatewayButton);
gatewayButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
gateWayAsyncTask gateWayAsyncTask = new gateWayAsyncTask();
gateWayAsyncTask.execute();
}
});
}
class gateWayAsyncTask extends AsyncTask<Void, Void, Void>
{
private String userName;
#Override
protected Void doInBackground(Void... params)
{
//gateway
creatingUser = clientFactory.build(MyUserClient.class);
userName = creatingUser.rootGet("SampleUserName").getLastName;
return null;
}
#Override
public void onPostExecute(Void var)
{
Log.d("gateway","gateway succeded!");
if( userName == null)
{
Log.d("gateway","username is null");
}
else if(userName.equals(""))
{
Log.d("gateway","username is empty");
}
else
{
Log.d("gateway",userName);
}
}
}
In the sdk i generated from api gateway here is the client class:
#com.amazonaws.mobileconnectors.apigateway.annotation.Service(endpoint = "https://ow2zhiry2b.execute-api.us-west-2.amazonaws.com/test5")
public interface MyUserClient {
/**
* A generic invoker to invoke any API Gateway endpoint.
* #param request
* #return ApiResponse
*/
com.amazonaws.mobileconnectors.apigateway.ApiResponse execute(com.amazonaws.mobileconnectors.apigateway.ApiRequest request);
/**
*
*
* #param username
* #return AndroidOutput
*/
#com.amazonaws.mobileconnectors.apigateway.annotation.Operation(path = "/", method = "GET")
AndroidOutput rootGet(
#com.amazonaws.mobileconnectors.apigateway.annotation.Parameter(name = "username", location = "query")
String username);
}
Here is the AndroidOuput class which was also generated:
public class AndroidOutput {
#com.google.gson.annotations.SerializedName("items")
private String items = null;
/**
* Gets items
*
* #return items
**/
public String getItems() {
return items;
}
/**
* Sets the value of items.
*
* #param items the new value
*/
public void setItems(String items) {
this.items = items;
}
}
So the log that comes out is username is null and i have no idea why. I have checked the cloudwatch, and indeed my backend lambda function is running. And when i test the api in gateway it ran there. If anyone could please help me thatd be awesome!!

If you're familiar with Retrofit and don't want to switch to AWS sdk, you may want to consider adding AWS Gateway OkHttp Interceptor to your OkHttpClient.

Have you considered using the AWS Gateway for Android?

Related

I can't seem to understand why my updateById route doesn't work on Android Studio

I'm programming a password manager app with a MySql database and a spring boot back-end.
My routes on the spring boot back-end work perfectly and I tested them with postman. Most of them also work in my android studio app with the help of retrofit. But the last one that I need is updateAccountById() with a Put request.
This Put route works with postman but won't work in my android studio app and I can't seem to find why.
This is my repository methods in the spring-boot back-end (There is an #Service with the class but stackoverflow doesn't like it) :
public class AccountDao {
#Autowired
private AccountRepository accountRepository;
/** Create new account **/
public Account save(Account account){
return accountRepository.save(account);
}
/** Delete account **/
public void deleteById(int id){
accountRepository.deleteById(id);
}
/** Get all accounts **/
public List<Account> getAllAccounts() {
List<Account> accounts = new ArrayList<>();
Streamable.of(accountRepository.findAll()).get().forEach(accounts::add);
return accounts;
}
/** Get accounts by category **/
public List<Account> getAccountsByCategory(int id){
List<Account> accountsByCategory = new ArrayList<>();
Streamable.of(accountRepository.findAll()).get().filter(account -> account.getCategoryId() == id).forEach(accountsByCategory::add);
return accountsByCategory;
}
/** Get account by id **/
public Account getAccountById(int id){
return accountRepository.findById(id).get();
}
/** Get favorites accounts **/
public List<Account> getFavoriteAccounts(){
List<Account> accounts = new ArrayList<>();
Streamable.of(accountRepository.findAll()).get().filter(Account::getFavorite).forEach(accounts::add);
return accounts;
}
}
Then this is my rest-api routes in my controller (There is an #Restcontroller) :
public class Controller {
/** DAO initialization **/
#Autowired
CategoryDao categoryDao = new CategoryDao();
#Autowired
AccountDao accountDao = new AccountDao();
/** REST routes **/
/** Categories routes **/
/* Get one category by id */
#GetMapping("/category/{id}")
public Category getCategoryById(#PathVariable int id) {
return categoryDao.getCategoryById(id);
}
/* Get all categories route */
#GetMapping("/categories")
public List<Category> getCategories(){
return categoryDao.getAllCategory();
}
/* Create a new category */
#PostMapping("/category/save")
public Category newCategory(#RequestBody Category category){
return categoryDao.save(category);
}
/* Delete a category */
#DeleteMapping("/category/delete/{categoryId}")
public void delCategory(#PathVariable int categoryId){
categoryDao.deleteById(categoryId);
}
/** Accounts routes **/
/* Get accounts by category */
#GetMapping("accounts/{categoryId}")
public List<Account> getCategory(#PathVariable int categoryId){
return accountDao.getAccountsByCategory(categoryId);
}
/* Get one account by ID */
#GetMapping("/account/{accountId}")
public Account getAccount(#PathVariable int accountId){
return accountDao.getAccountById(accountId);
}
#GetMapping("accounts/favorite")
public List<Account> favorites(){
return accountDao.getFavoriteAccounts();
}
/* Create new account */
#PostMapping("/account/save")
public Account newAccount(#RequestBody Account account){
return accountDao.save(account);
}
/* Edit an account */
#PutMapping("/update/{accountId}")
public ResponseEntity<Account> updateAccount(#PathVariable int accountId, #RequestBody Account accountDetails){
Account account = accountDao.getAccountById(accountId);
account.setCategoryId(accountDetails.getCategoryId());
account.setName(accountDetails.getName());
account.setUserName(accountDetails.getUserName());
account.setPassword(accountDetails.getPassword());
account.setUrl(accountDetails.getUrl());
account.setFavorite(accountDetails.getFavorite());
Account updateAccount = accountDao.save(account);
return ResponseEntity.ok().body(updateAccount);
}
/* Delete an Account */
#DeleteMapping("/account/delete/{accountId}")
public void delAccount(#PathVariable int accountId){
accountDao.deleteById(accountId);
}
}
Finally, this is my retrofit routes on Android Studio with the way I use it :
public interface AccountApi {
#GET("/account/{categoryId}")
Call<Account> getAccount(#Path("categoryId") int id);
#GET("/accounts/{categoryId}")
Call<List<Account>> getAccountsByCategory(#Path("categoryId") int id);
#GET("/accounts/favorite")
Call<List<Account>> getFavoriteAccounts();
#POST("/account/save")
Call<Account> saveAccount(#Body Account account);
#PUT("/update/{accountId}")
Call<Account> updateAccount(#Path("accountId") int id, #Body Account account);
#DELETE("/account/delete/{id}")
Call<ResponseBody> deleteAccount(#Path("id") int id);
}
This is in an OnClickListener :
accountApi.getAccount(getArguments().getInt("accountID")).enqueue(new Callback<Account>() {
#Override
public void onResponse(Call<Account> call, Response<Account> response) {
edit_name.setText(response.body().getName());
edit_username.setText(response.body().getUserName());
edit_password.setText(response.body().getPassword());
edit_url.setText(response.body().getUrl());
edit_favorite.setChecked(response.body().getFavorite());
}
#Override
public void onFailure(Call<Account> call, Throwable t) {
}
});

How to do SNS Push Notification? CreatePlatformEndpointResult returns null

I have followed a tutorial on how to setup SNS Push notification but the CreatePlatformEndpointResult object returns null. I need that so that I can retrieve the endpointArn and send that to the backend. Below is my entire setup. And here is the link to the tutorial: http://www.allcode.com/amazon-sns-push-notification-tutorial-android-using-gcm/
First I retrieve a token from GCM by calling
AWSManager.registerAppToGCM(getApplicationContext())
This is from my AWSManager class
public class AWSManager {
private static final String TAG = AWSManager.class.getSimpleName();
private static final String SNS_ACCESS_KEY_ID = "1234567890"; // I have swapped out the real key
private static final String SNS_SECRET_KEY = "1234567890"; // I have swapped out the real key
private static AmazonSNSClient snsClient;
/**
* Method is used to retrieve SNSClient object
*
* #return snsClient object
*/
public static AmazonSNSClient getSNSClient() {
if (FrameworkUtils.checkIfNull(snsClient)) {
snsClient = new AmazonSNSClient(new BasicAWSCredentials(SNS_ACCESS_KEY_ID, SNS_SECRET_KEY));
snsClient.setRegion(Region.getRegion(Regions.US_WEST_1));
}
return snsClient;
}
/**
* Method is used to register app to GCM
*
* #param context
*/
public static void registerAppToGCM(Context context) {
SharedPref sharedPref = new SharedPref(context, Constants.PREF_FILE_NAME);
String gcmToken = sharedPref.getStringPref(Constants.NOTIFICATION_GCM_TOKEN, "");
if (FrameworkUtils.isStringEmpty(gcmToken)) {
new GCMRegisterTask(context).execute();
}
}
}
Here is the class performing the background task
public class GCMRegisterTask extends AsyncTask<String, Void, Boolean> {
private static final String TAG = GCMRegisterTask.class.getSimpleName();
private Context mContext;
/**
* Constructor
*
* #param context
*/
public GCMRegisterTask(Context context) {
super();
mContext = context;
}
#Override
protected Boolean doInBackground(String... params) {
String token;
try {
token = InstanceID.getInstance(mContext).getToken(mContext.getString(R.string.gcm_project_id), GoogleCloudMessaging.INSTANCE_ID_SCOPE);
SharedPref sharedPref = new SharedPref(mContext, Constants.PREF_FILE_NAME);
sharedPref.setPref(Constants.NOTIFICATION_GCM_TOKEN, token);
Logger.i(TAG, "GCM token successfully stored to prefs: " + token);
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
Once I have successfully retrieved the GCM token. I use new
AWSCreateEndpointTask(mContext).execute(test, token,
"email#gmail.com")
to begin the process of creating the endpoint ARN. test = "arn:aws:sns:region:us-east-1:app/GCM/AppName"
public class AWSCreateEndpointTask extends AsyncTask<String, Void, CreatePlatformEndpointResult> {
Context mContext;
/**
* Constructor
*
* #param context
*/
public AWSCreateEndpointTask(Context context) {
super();
mContext = context;
}
#Override
protected CreatePlatformEndpointResult doInBackground(String[] params) {
if (params.length < 3) {
return null;
}
String arn = params[0];
String gcmToken = params[1];
String userData = params[2];
try {
CreatePlatformEndpointRequest request = new CreatePlatformEndpointRequest();
request.setCustomUserData(userData);
request.setToken(gcmToken);
request.setPlatformApplicationArn(arn);
return AWSManager.getSNSClient().createPlatformEndpoint(request);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
#Override
protected void onPostExecute(CreatePlatformEndpointResult result) {
if (!FrameworkUtils.checkIfNull(result)) {
String endpointArn = result.getEndpointArn();
SharedPref sharedPref = new SharedPref(mContext, Constants.PREF_FILE_NAME);
sharedPref.setPref(Constants.NOTIFICATION_ENDPOINT_ARN, endpointArn);
}
}
Inside of onPostExecute, the returned CreatePlatformEndpointResult object is null. What am I missing or doing incorrectly to cause this?
It turns out that the implementation is correct, the arn value was wrong. For others running into this conflict, make sure the information is correct when trying to get the endpointARN. I updated mine to
arn:aws:sns:us-east-1::app/GCM/
This comes from the developer console.

Google Cloud Endpoints Api call returning null

I'm making an android app that uses a development server to respond api calls.
I set it up using the following guide:
https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/HelloEndpoints
I used the skeleton code provided by google on the example, so my api and the AsyncTask that peforms the calls look like this:
MyEndpoint class:
/** An endpoint class we are exposing */
#Api(
name = "myApi",
version = "v1",
namespace = #ApiNamespace(
ownerDomain = "backend.myapplication.madelenko.example.com",
ownerName = "backend.myapplication.madelenko.example.com",
packagePath=""
)
)
public class MyEndpoint {
/** A simple endpoint method that takes a name and says Hi back */
#ApiMethod(name = "supplyJoke")
public MyBean supplyJoke() {
MyBean response = new MyBean();
response.setData(JokeDispenser.getJoke());
return response;
}
}
MyBean Class:
/** The object model for the data we are sending through endpoints */
public class MyBean {
private String myData;
public String getData() {
return myData;
}
public void setData(String data) {
myData = data;
}
}
The asyncTask:
public class FetchJokeTask extends AsyncTask<Pair<Context,String>, Void, String> {
private static MyApi myApiService = null;
private Context context;
#Override
protected String doInBackground(Pair<Context, String>... params) {
if(myApiService == null) { // Only do this once
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("http://10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
#Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
// end options for devappserver
myApiService = builder.build();
}
context = params[0].first;
String name = params[0].second;
try {
return myApiService.supplyJoke().execute().getData();
} catch (IOException e) {
return e.getMessage();
}
}
#Override
protected void onPostExecute(String result) {
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}
}
For some reason, when I click a button and launch an AsyncTask, I get a 404 error. The toast returns html with the contents of a 404 page.
I used the debugger to find out why and I know that the asyncTask tries to execute this line and fails:
return myApiService.supplyJoke().execute().getData();
Therefore, it returns an error message.
Is there anything wrong with my config?
Please help me to figure it out. Thanks.
P.S.: The problem is that the execute() method throws an IOException. I hope this extra piece gives you some context. Thank you very much.

Android consuming RestService with/without cache

I am currently in the process of creating a high performance mobile application. Now i am looking at various design patterns for consuming rest services. One such pattern that stands out is the Google IO discussion here. How i have am looking at the code to develop this design. I will be using Spring Rest for doing the actual HTTP Rest and serialization to POJO with the Serialization Library. I came across this implementation here, and will be using it as a blue print for my application. Now a major question is here.
public interface HttpMethods {
public Object getForObject(Object ... params);
public Object putForObject(Object ... params);
}
public class LocationsHttpMethods implements HttpMethods{
private final Context mContext;
public LocationsHttpMethods(Context context)
{
mContext=context;
}
#Override
public Location[] getForObject(Object... params) {
return null;
}
#Override
public Object putForObject(Object... params) {
return null;
}
}
My Location is just a pojo class. Now the question that troubles me is that the second link that i have given just uses Boolean to return data. I will be returning an array of something.
package com.confiz.rest.services;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import com.confiz.rest.providers.IProvider;
import com.confiz.rest.providers.LocationsProvider;
public class ProcessorService extends Service
{
private Integer lastStartId;
private final Context mContext = this;
/**
* The keys to be used for the required actions to start this service.
*/
public static class Extras
{
/**
* The provider which the called method is on.
*/
public static final String PROVIDER_EXTRA = "PROVIDER_EXTRA";
/**
* The method to call.
*/
public static final String METHOD_EXTRA = "METHOD_EXTRA";
/**
* The action to used for the result intent.
*/
public static final String RESULT_ACTION_EXTRA = "RESULT_ACTION_EXTRA";
/**
* The extra used in the result intent to return the result.
*/
public static final String RESULT_EXTRA = "RESULT_EXTRA";
}
private final HashMap<String, AsyncServiceTask> mTasks = new HashMap<String, AsyncServiceTask>();
/**
* Identifier for each supported provider.
* Cannot use 0 as Bundle.getInt(key) returns 0 when the key does not exist.
*/
public static class Providers
{
public static final int LOATIONS_PROVIDER = 1;
}
private IProvider GetProvider(int providerId)
{
switch(providerId)
{
case Providers.LOATIONS_PROVIDER:
return new LocationsProvider(this);
}
return null;
}
/**
* Builds a string identifier for this method call.
* The identifier will contain data about:
* What processor was the method called on
* What method was called
* What parameters were passed
* This should be enough data to identify a task to detect if a similar task is already running.
*/
private String getTaskIdentifier(Bundle extras)
{
String[] keys = extras.keySet().toArray(new String[0]);
java.util.Arrays.sort(keys);
StringBuilder identifier = new StringBuilder();
for (int keyIndex = 0; keyIndex < keys.length; keyIndex++)
{
String key = keys[keyIndex];
// The result action may be different for each call.
if (key.equals(Extras.RESULT_ACTION_EXTRA))
{
continue;
}
identifier.append("{");
identifier.append(key);
identifier.append(":");
identifier.append(extras.get(key).toString());
identifier.append("}");
}
return identifier.toString();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// This must be synchronised so that service is not stopped while a new task is being added.
synchronized (mTasks)
{
// stopSelf will be called later and if a new task is being added we do not want to stop the service.
lastStartId = startId;
Bundle extras = intent.getExtras();
String taskIdentifier = getTaskIdentifier(extras);
Log.i("ProcessorService", "starting " + taskIdentifier);
// If a similar task is already running then lets use that task.
AsyncServiceTask task = mTasks.get(taskIdentifier);
if (task == null)
{
task = new AsyncServiceTask(taskIdentifier, extras);
mTasks.put(taskIdentifier, task);
// AsyncTasks are by default only run in serial (depending on the android version)
// see android documentation for AsyncTask.execute()
task.execute((Void[]) null);
}
// Add this Result Action to the task so that the calling activity can be notified when the task is complete.
String resultAction = extras.getString(Extras.RESULT_ACTION_EXTRA);
if (resultAction != "")
{
task.addResultAction(extras.getString(Extras.RESULT_ACTION_EXTRA));
}
}
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
public class AsyncServiceTask extends AsyncTask<Void, Void, Object>
{
private final Bundle mExtras;
private final ArrayList<String> mResultActions = new ArrayList<String>();
private final String mTaskIdentifier;
/**
* Constructor for AsyncServiceTask
*
* #param taskIdentifier A string which describes the method being called.
* #param extras The Extras from the Intent which was used to start this method call.
*/
public AsyncServiceTask(String taskIdentifier, Bundle extras)
{
mTaskIdentifier = taskIdentifier;
mExtras = extras;
}
public void addResultAction(String resultAction)
{
if (!mResultActions.contains(resultAction))
{
mResultActions.add(resultAction);
}
}
#Override
protected Object doInBackground(Void... params)
{
Log.i("ProcessorService", "working " + mTaskIdentifier);
Object result = false;
final int providerId = mExtras.getInt(Extras.PROVIDER_EXTRA);
final int methodId = mExtras.getInt(Extras.METHOD_EXTRA);
if (providerId != 0 && methodId != 0)
{
final IProvider provider = GetProvider(providerId);
if (provider != null)
{
try
{
result = provider.RunTask(methodId, mExtras);
} catch (Exception e)
{
result = false;
}
}
}
return result;
}
#Override
protected void onPostExecute(Object result)
{
// This must be synchronised so that service is not stopped while a new task is being added.
synchronized (mTasks)
{
Log.i("ProcessorService", "finishing " + mTaskIdentifier);
// Notify the caller(s) that the method has finished executing
for (int i = 0; i < mResultActions.size(); i++)
{
Intent resultIntent = new Intent(mResultActions.get(i));
//What to do here
resultIntent.put(Extras.RESULT_EXTRA, true);
//What to do here ends.
resultIntent.putExtras(mExtras);
resultIntent.setPackage(mContext.getPackageName());
mContext.sendBroadcast(resultIntent);
}
// The task is complete so remove it from the running tasks list
mTasks.remove(mTaskIdentifier);
// If there are no other executing methods then stop the service
if (mTasks.size() < 1)
{
stopSelf(lastStartId);
}
}
}
}
}
Now if you browse to the code that contain the AsyncService, and puts the resultIntent.put(Extras.RESULT_EXTRA, true);
Now how should i pass the data back to the intent. I heard Serializable is bad, and Parceable is ugly code. What else can i use. Secondly, where do i add the SQL cache retrieve code. How can i add this code to the framework. Hope i make sense.

Salesforce Rest API with android - NullPointerException # AsyncRequestCallback

I'm trying to get the Salesforce REST API working with Android and new to android programming, followed the sample code to connect with SFDC http://wiki.developerforce.com/page/Getting_Started_with_the_Mobile_SDK_for_Android#Authentication
I'm trying to get a few records from SFDC and display them in the android app, looks like when the Async Call is made at "client.sendAsync(sfRequest, new AsyncRequestCallback()" - NullPointerException is thrown.
I did see a couple of similar issues online, but didn't help me. Hoping if some one would point me in the right direction to troubleshoot this. Thanks much.
public class GetAccountsActivity extends Activity {
private PasscodeManager passcodeManager;
private String soql;
private String apiVersion;
private RestClient client;
private TextView resultText;
private RestRequest sfRequest;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get Api Version
apiVersion = getString(R.string.api_version);
//Create Query
soql = "select id, name from Account limit 10";
// Setup view
setContentView(R.layout.get_accounts_activity);
((TextView) findViewById(R.id.Acc_Title)).setText(apiVersion);
// Passcode manager
passcodeManager = ForceApp.APP.getPasscodeManager();
}
#Override
public void onResume() {
super.onResume();
//Get SFClient
// Login options
String accountType = ForceApp.APP.getAccountType();
LoginOptions loginOptions = new LoginOptions(
null, // login host is chosen by user through the server picker
ForceApp.APP.getPasscodeHash(),
getString(R.string.oauth_callback_url),
getString(R.string.oauth_client_id),
new String[] {"api"});
new ClientManager(this, accountType, loginOptions).getRestClient(this, new RestClientCallback() {
#Override
public void authenticatedRestClient(RestClient client) {
if (client == null) {
ForceApp.APP.logout(GetAccountsActivity.this);
return;
}
GetAccountsActivity.this.client = client;
}
});
//Get Rest Object to query
try {
sfRequest = RestRequest.getRequestForQuery(apiVersion, soql);
//Use SF Rest Client to send the request
client.sendAsync(sfRequest, new AsyncRequestCallback(){
#Override
public void onSuccess(RestRequest request, RestResponse response){
//Check responses and display results
// EventsObservable.get().notifyEvent(EventType.RenditionComplete);
}//end onSuccess
#Override
public void onError(Exception exception) {
//printException(exception);
EventsObservable.get().notifyEvent(EventType.RenditionComplete);
}//End Exception for Async Method
});
}catch (UnsupportedEncodingException e) {
//printHeader("Could Send Query request");
//printException(e);
return;
}
}
}
enter code here
You are calling client.sendAsync from onResume() but client is not set until the authenticatedRestClient callback is called, you need to move your sendAsync call into the authenticatedRestClient callback.

Categories

Resources