Error using Amazon DynamoDB in Android application (ResourceNotFoundException) - android

I'm trying to follow along this guide without much success. I have created the appropriate IAM role and attached the proper role policy to that role, and I have created a DyanmoDB table called "TaskShareGroups" with Primary Partition key "Group Name" (a String).
I have set up my class as follows:
#DynamoDBTable(tableName = "TaskShareGroups")
public class Group implements Parcelable {
String name;
public Group(String name) {
this.name = name;
}
#Override
public String toString() {
return name;
}
#DynamoDBHashKey(attributeName = "Group Name")
public String getName() {
return name;
}
Here is my code trying to write a Group object to the database.
Runnable saveGroup = new Runnable() {
public void run() {
try {
mapper.save(g);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
};
CognitoCachingCredentialsProvider credentials =
new CognitoCachingCredentialsProvider(
getApplicationContext(),
identity_pool_ID,
Regions.US_EAST_2);
ddbClient = new AmazonDynamoDBClient(credentials);
mapper = new DynamoDBMapper(ddbClient);
g = new Group(gName);
Thread mythread = new Thread(saveGroup);
mythread.start();
The error I get when I run this:
Requested resource not found (Service: AmazonDynamoDB; Status Code: 400; Error Code: ResourceNotFoundException; Request ID: NPBOR9KLFA7T5RLQOID7F7T3KNVV4KQNSO5AEMVJF66Q9ASUAAJG)
I am not quite sure what is going on. I'm not entirely sure I set up my unauthenticated role correctly; my exception seems to indicate that it's having trouble finding the correct table but I can clearly see the table through the console.
Android Studio is also telling me that the constructor I am using for my DynamoDBMapper is depreciated; however this is the approach suggested by the guide above. Could this be the issue?
Let me know if there is any other pertinent information I should add to this document.

You should set your region correctly using the setRegion API on the AmazonDynamoDBClient client. The following is the link to the API reference: http://docs.aws.amazon.com/AWSAndroidSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/AmazonDynamoDB.html#setRegion-com.amazonaws.regions.Region-
Make sure you set the region correctly to where your table is.
Thanks,
Rohan

Related

How do I send data from an android wearable device to a phone in the form of a a simple text file containing data?

I have a wearable app. The app after it finishes has data like time/date, UUID, Geo location, parameters selected displayed in front of me like a Data Report or Log in several TextViews underneath each other. Like a list. I want this data to be transferred from my wearable device to my android phone.
Now I have to ask does the WearOS app the pairs the phone with the watch enables such a thing? Like can the data be sent through it? OR what exactly can I do? I read about Sync data items with the Data Layer API in the documentation, but I'm not sure if the code snippets provided would help achieve what I want.
public class MainActivity extends Activity {
private static final String COUNT_KEY = "com.example.key.count";
private DataClient dataClient;
private int count = 0;
...
// Create a data map and put data in it
private void increaseCounter() {
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
putDataMapReq.getDataMap().putInt(COUNT_KEY, count++);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
Task<DataItem> putDataTask = dataClient.putDataItem(putDataReq);
}
...
}
The data I display in the textviews are called through methods that I call things like: getLocation, getUUID, getDateTime, getSelections, etc... when I click a button I call them in the setOnClickListener. I want this data in the TextViews to be placed in a file or something like that and send them over to the mobile phone from the watch when they're generated.
private void getDateTime()
{
SimpleDateFormat sdf_date = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat sdf_time = new SimpleDateFormat("HH:mm:ss z");
String currentDate= sdf_date.format(new Date());
String currentTime= sdf_time.format(new Date());
textView_date_time.setText("Date: "+currentDate+"\n"+"Time: "+currentTime);
}
#SuppressLint("SetTextI18n")
private void getUUID()
{
// Retrieving the value using its keys the file name
// must be same in both saving and retrieving the data
#SuppressLint("WrongConstant") SharedPreferences sh = getSharedPreferences("UUID_File", MODE_APPEND);
// The value will be default as empty string because for
// the very first time when the app is opened, there is nothing to show
String theUUID = sh.getString(PREF_UNIQUE_ID, uniqueID);
// We can then use the data
textView_UUID.setText("UUID: "+theUUID);
}
#SuppressLint("SetTextI18n")
private void getSelections()
{
textView_data_selected.setText("Tool No.: "+c.getToolNo()+
"\nTool Size: " +c.getToolSizeStr()+
"\nFrom Mode: " +c.getCurrentModeStr()+
"\nGoto Mode: " +c.getModeStr()+
"\nMethod: " +c.getMethodStr()+
"\nBit Duration: " +c.getBitDuration()+
"\nUpper bound" +c.getUpStageValue()+
"\nLower bound: "+c.getDownStageValue());
}
The above are examples of the methods I use to get the data. then I call them here:
gps_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= 26) {
getLocation();
getDateTime();
getUUID();
getSelections();
}
else
{
//ActivityCompat.requestPermissions(get_location.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
Toast.makeText(get_location.this,"Build SDK too low",Toast.LENGTH_SHORT);
}
}
});
Now how do I take all this and send it over from my device to the the phone?
Note: The data report I want to send as a file, I want it done subtly like something done in the background. I don't know what else to do or where to look.
You have two options if you want to use the Data Layer, one is to use the MessageClient API to bundle your data up in a message and send it directly to the handheld. The easiest here would be to create an arbitrary JSONObject and serialize your data as a JSON string you can stuff into a message. For example:
try {
final JSONObject object = new JSONObject();
object.put("heart_rate", (int) event.values[0]);
object.put("timestamp", Instant.now().toString());
new MessageSender("/MessageChannel", object.toString(), getApplicationContext()).start();
} catch (JSONException e) {
Log.e(TAG, "Failed to create JSON object");
}
In my case, I do this in my onSensorChanged implementation, but you can insert this wherever you are updating your text.
MessageSender is just a threaded wrapper around the MessageClient:
import java.util.List;
class MessageSender extends Thread {
private static final String TAG = "MessageSender";
String path;
String message;
Context context;
MessageSender(String path, String message, Context context) {
this.path = path;
this.message = message;
this.context = context;
}
public void run() {
try {
Task<List<Node>> nodeListTask = Wearable.getNodeClient(context.getApplicationContext()).getConnectedNodes();
List<Node> nodes = Tasks.await(nodeListTask);
byte[] payload = message.getBytes();
for (Node node : nodes) {
String nodeId = node.getId();
Task<Integer> sendMessageTask = Wearable.getMessageClient(context).sendMessage(nodeId, this.path, payload);
try {
Tasks.await(sendMessageTask);
} catch (Exception exception) {
// TODO: Implement exception handling
Log.e(TAG, "Exception thrown");
}
}
} catch (Exception exception) {
Log.e(TAG, exception.getMessage());
}
}
}
The other option is to create a nested hierarchy of data items in the Data Layer and implement DataClient.OnDataChangedListener on both sides, such that changes that are written in on one side are automatically synchronized with the other. You can find a good walkthrough on how to do that here.
For your specific case, just packing it in a JSON object would probably be the simplest. The writing out to your preferred file format you can then implement on the handheld side without needing to involve the wear side.

How to validate mobile app facebook/google access token on spring boot 2.0 OAuth2 Authorization server?

I'm trying to validate Android/iOS client phone number or email address using Facebook Account-Kit service. I'm not sure how to validate the authorization code or access token with spring boot based back-end server and return the my own access token.
Between, I have gone thorough this blog https://www.baeldung.com/spring-security-5-oauth2-login, but it session based. I'm not clear how to change this to stateless (e.g /oauth/token).
Could anyone please let me know how to solve the issue ?
Reference : [https://developers.facebook.com/docs/accountkit/graphapi][1]
Here is my code :
#Configuration
#EnableOAuth2Client
public class SocialConfig extends WebSecurityConfigurerAdapter {
#Autowired
OAuth2ClientContext oauth2ClientContext;
private String[] PUBLIC_URL = { "/*", "/api/v1/account/validate", "login/accountkit", "/api/v1/account" };
#Override
protected void configure(HttpSecurity http) throws Exception {
// super.configure(http);
http.authorizeRequests()
.antMatchers(PUBLIC_URL).permitAll()
.anyRequest().authenticated()
.and().csrf()
.disable()
.addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
}
private Filter ssoFilter() {
OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(
"/login/accountkit");
OAuth2ProtectedResourceDetails accountkit = accountKit();
OAuth2RestTemplate template = new OAuth2RestTemplate(accountkit, oauth2ClientContext);
filter.setRestTemplate(template);
UserInfoTokenServices userInfo = new UserInfoTokenServices(accountKitResource().getUserInfoUri(),
accountkit.getClientId());
userInfo.setRestTemplate(template);
filter.setTokenServices(userInfo);
return filter;
}
#Bean
#ConfigurationProperties("accountkit.client")
protected OAuth2ProtectedResourceDetails accountKit() {
AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
resource.setAccessTokenUri("https://graph.accountkit.com/v1.2/me");
resource.setUserAuthorizationUri("https://graph.accountkit.com/v1.2/access_token");
resource.setClientId("AA|xxxx|xxx");
resource.setGrantType("authorization_code");
resource.setTokenName("access_token");
resource.setAuthenticationScheme(AuthenticationScheme.form);
resource.setPreEstablishedRedirectUri("http://localhost:8080/login/accountkit");
return resource;
}
#Bean
#ConfigurationProperties("accountkit.resource")
protected ResourceServerProperties accountKitResource() {
return new ResourceServerProperties();
}
}

adding stripe server code to Backendless Cloud

I am trying to integrate stripe into my android application with the server code set up in Backendless as a Custom API Service. After charging the card, with a successful call back method, I am not able to see that in my payments list on my stripe dashboard. Not sure where the problem is. Any suggestions will be appreciated.Below is what I have so far:
ChargeItem://Charge Class
package com.mbaas.service;
public class ChargeItem {
public String token;
public int price;
public String description;
}
ChargeService //Backendless Service
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Charge;
import java.util.HashMap;
import java.util.Map;
public class ChargeService implements IBackendlessService
{
public boolean makeCharge(ChargeItem charges){
Stripe.apiKey = "my stripe secret key";
// Get the credit card details submitted by the form
String token = charges.token;
double price = charges.price;
String desc = charges.description;
String userId = charges.userId;
String orderId = charges.orderId;
// Create a charge: this will charge the user's card
try {
Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("orderId",orderId);
chargeParams.put("userId",userId);
chargeParams.put("amount", price); // Amount in cents
chargeParams.put("currency", "usd");
chargeParams.put("source", token);
chargeParams.put("description", desc);
#SuppressWarnings("unused")
Charge charge = Charge.create(chargeParams);
}
catch (StripeException e) {
// The card has been declined
return false;
}
return true;
}
}
//My stripe token call back method
private void convertCardToToken(Card card, final Orders order){
Stripe stripe = new Stripe(getApplicationContext(), CustomApplication.PUBLISHABLE_KEY);
stripe.createToken(
card,
new TokenCallback() {
public void onSuccess(Token token) {
// Send token to your server
ChargeItem chargeItem = new ChargeItem();
chargeItem.setToken(token.getId());
chargeItem.setOrderId(order.getObjectId());
chargeItem.setPrice(order.getOrder_price());
chargeItem.setUserName(order.getOwnerId());
chargeItem.setDescription("Delivery Fee");
ChargeService.initApplication();
ChargeService chargeService = ChargeService.getInstance();
chargeService.makeChargeAsync(chargeItem, new AsyncCallback<Boolean>() {
#Override
public void handleResponse(Boolean response) {
Toast.makeText(getApplicationContext(),
"Payment Successful",
Toast.LENGTH_LONG
).show();
}
#Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(getApplicationContext(), fault.getMessage(),
Toast.LENGTH_LONG
).show();
}
});
}
public void onError(Exception error) {
// Show localized error message
Toast.makeText(CheckoutActivity.this,
error.getLocalizedMessage(),
Toast.LENGTH_LONG
).show();
}
}
);
}
I'm not familiar with Backendless so I cannot provide much help here, but there a few issues with your code:
price is a double. To avoid rounding errors, all amounts in Stripe's API are in cents (or more generally, in the smallest unit for the currency you're using), so price should be an int.
userId and orderId are not valid parameters when creating a charge. You likely want to pass these variables as metadata values.
To help you debug further, you should also check your logs in your Stripe account's dashboard at https://dashboard.stripe.com/test/logs?method=not_get. You should see requests to POST /v1/tokens (sent by your Android app) and requests to POST /v1/charges (sent by Backendless).

Custom API in Azure APP Serivce examples searched for Android Client

I need a working example for a custom API for Microsoft Azure App Service.
I could not get any useful or working information/examples for that, or they just show each time different approaches which are outdated?!?!
For now I have a working table controller which gets information from database and returns it back to my Android client. Now I need to define a custom API Controller to get a string back. In the examples they are all sending an object to the service in order to get an object back. I do not want to send anything to the API, just retrieve some information back from a GET Request.
Regards
// EDIT - Added / edited client / server code to Post a String.
You can use the following code to do a GET request on the auto generated API controller Visual Studio creates (ValuesController).
private void getStringFromAzure() throws MalformedURLException {
// Create the MobileService Client object and set your backend URL
String yourURL = "https://yourApp.azurewebsites.net/";
MobileServiceClient mClient = new MobileServiceClient(yourURL, this);
// Your query pointing to yourURL/api/values
ListenableFuture<JsonElement> query = mClient.invokeApi("values", null, GetMethod, null);
// Callback method
Futures.addCallback(query, new FutureCallback<JsonElement>() {
#Override
public void onSuccess(JsonElement jsonElement) {
// You are expecting a String you can just output the result.
final String result = jsonElement.toString();
// Since you are on a async task, you need to show the result on the UI thread
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onFailure(Throwable throwable) {
Log.d(TAG, "onFailure: " + throwable.getMessage());
}
});
}
public void sendString(final String someString) throws MalformedURLException {
// Your query pointing to /api/values/{String}
ListenableFuture<JsonElement> query = mClient.invokeApi("values/" + someString, null, PostMethod, null);
// Callback method
Futures.addCallback(query, new FutureCallback<JsonElement>() {
#Override
public void onSuccess(JsonElement jsonElement) {
// You are expecting a String you can just output the result.
final String result = jsonElement.toString();
}
#Override
public void onFailure(Throwable throwable) { }
});
}
The backend API: (ValuesController)
{
// Use the MobileAppController attribute for each ApiController you want to use
// from your mobile clients
[MobileAppController]
public class ValuesController : ApiController
{
// GET api/values
public string Get()
{
return "Hello World!";
}
// POST api/values/inputString
public string Post(string inputString)
{
return inputString;
}
}
}
You can also send parameters along in the following way:
List<Pair<String, String>> parameters = new ArrayList<>();
parameters.add(new Pair<>("name", "John"));
parameters.add(new Pair<>("password", "fourwordsalluppercase"));
ListenableFuture<JsonElement> query = client.invokeApi("yourAPI", PostMethod, parameters);
Or as json in the body:
JsonObject body = new JsonObject();
body.addProperty("currentPassword", currentPassword);
body.addProperty("password", password);
body.addProperty("confirmPassword", confirmPassword);
ListenableFuture<JsonElement> query = mClient.invokeApi("yourAPI", body, PostMethod, null);
Based on my understanding, I think there are two parts in your question which include as below. And I think you can separately refer to two sections to get the answers and write your own example.
How to define a custom API on Azure Mobile App to retrieve data from database? Please refer to the section Custom APIs to know how to do with Azure Mobile App backend.
How to call a custom API from Android App? Please refer to the section How to: Call a custom API to know how to do with Android SDK.

GAE Android Connected Project JDO Primary key

I have been trying to create a GAE to get and insert data into my Android Application. I used the Eclipse wizard to create both projects (app engine connected android project). I am using JDO and trying to create unique primary keys but it doesn't seem to be doing it for me and I think that it should although i'm probably wrong. The server keeps popping out messages that the primary key should not be 0. Im new to this and was testing it out. Here is the code I feel is relevant (please let me know if more is needed). As to my understanding, it should be getting its key when the server calls makepersistant().
#ApiMethod(name = "insertTrack")
public Track insertTrack(Track track) {
PersistenceManager mgr = getPersistenceManager();
try {
if (containsTrack(track)) {
throw new EntityExistsException("Object already exists");
}
mgr.makePersistent(track);
} finally {
mgr.close();
}
return track;
}
This is what my app is calling:
public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
protected Long doInBackground(Context... contexts) {
Trackendpoint.Builder endpointBuilder = new Trackendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Trackendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
try {
Track track = new Track();
track.setName("1");
track.setDesc("First Track on Server");
Track result = endpoint.insertTrack(track).execute();
} catch (IOException e) {
e.printStackTrace();
}
return (long) 0;
}
}
And the JDO
public class Track {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private long trackID;
private String name;
private String desc;
Any help leading me in the right direction would be awesome. Thanks in advance!

Categories

Resources