How to handle dynamic error response in android using retrofit? - android

I am currently working on new android project with feathersjs back-end. Before error response normal format like,
{
"code": 123,
"message": "An error occurred!"
}
But now structure change dynamically according to the api request body. For Example I have three input field in my UI like Name, age, email after hitting api if wrong data error response will be like,
{
"code":123,
"error":{
"name":"Name is wrong",
"age":"Age is greater than 18 is mandatory",
"email:"Invalid email id"
}
}
So I need to link Error response to correct Edittext Field and set Error Message. How I link this ?. But Request Body field name and error response field name are same.
Request Body structure :
{
"data":{
"name":"xxx",
"age":"24",
"email:"xx#gmail.com"
}
}
Detail Explanation with Example Code will be more helpful.

Related

Connection between Android and AWS SageMaker with AWS Lambda

I set up a connection between Android and AWS Lambda which has the endpoint set to SageMaker. I am using the REST API during the connection, the AWS Cognito plug is set to be accessed without authorization.
I make a connection as described here:
https://aws.amazon.com/blogs/machine-learning/call-an-amazon-sagemaker-model-endpoint-using-amazon-api-gateway-and-aws-lambda/
My question is how to send this data:
{"data":"13.49,22.3,86.91,561.0,0.08752,0.07697999999999999,0.047510000000000004,0.033839999999999995,0.1809,0.057179999999999995,0.2338,1.3530000000000002,1.735,20.2,0.004455,0.013819999999999999,0.02095,0.01184,0.01641,0.001956,15.15,31.82,99.0,698.8,0.1162,0.1711,0.2282,0.1282,0.2871,0.06917000000000001"}
And how to view the received response later. Anyone know how to do it or where I can find tips on how to do it?
If I understand correctly, this is your system flow:
POST some data from your Android device
It gets received by API Gateway
And continues through to AWS Lambda
In AWS Lambda the data is extracted, and passed to Sage Maker
Creating a POST using AWS Amplify
To POST data form the Android device, follow the Amplify API (REST) category documentation.
Specifically, you can do something like:
val options = RestOptions.builder()
.addPath("/prod/predictbreastcancer")
.addBody("{\"data\":\"13.49,22.3,86.91,561.0,0.08752,0.07697999999999999,0.047510000000000004,0.033839999999999995,0.1809,0.057179999999999995,0.2338,1.3530000000000002,1.735,20.2,0.004455,0.013819999999999999,0.02095,0.01184,0.01641,0.001956,15.15,31.82,99.0,698.8,0.1162,0.1711,0.2282,0.1282,0.2871,0.06917000000000001\"}".toByteArray())
.build()
Amplify.API.post(options,
{ Log.i("Demo", "POST response = $it") },
{ Log.e("Demo", "POST failed", it) }
)
Creating POST body from EditText content
You mentioned you're using an EditText widget to gather the input data. I assume a user can enter a comma-separated list of values like 0.44, 6.11, etc.
To extract it's content and build the POST body, you can do:
val input = findViewById(R.id.input) as EditText
val body = JSONObject()
.put("data", input.text)
.toString()
.replaceAll("\"", "\\\"")
Displaying response in another Activity
Skimming the blog you referenced, I can't see an example of the response body content. But, here's how you can retrieve response JSON and pass it to a new activity.
Amplify.API.post(options,
{ response ->
val intent = Intent(this, YourOtherActivity::class.java)
intent.putExtra("json", response.data.asString())
runOnUiThread { startActivity(intent) }
},
{ /* handle error ... */ }
)
In YourOtherActivity, access the extra data in onCreate() like so:
val json = intent.getStringExtra("json")

Twilio SMS body doesn't concatenate URL parameters in Azure Logic App

I'm currently building a logic app in Azure to send text messages with Twilio. Everything is working smoothly except that I can't send complex URL in the body.
For exemple if I send this : https://example.com?id=26 I will recieve the the correct string, but the parameter is not concatenated correctly with the domain in the clickable link.
So when I click the link in the text message it only opens the https://example.com . The message I'm sending is built like this :
"Send_Text_Message_(SMS)": {
"inputs": {
"body": {
"body": "#{triggerBody()?['message']}#{triggerBody()?['url']}#{body('Insert_row')?['Id']}",
"from": "xxx-xxx-xxxx",
"to": "#triggerBody()?['phone']"
},
message: Hello please click this link
url :https://example.com?id=
id: 26
so the final body would be like : [message][url][id]
I've tried sending a simple string like https://example.com?id=8 to see if it was the "complex" body concatenation that was failing, but it's still not working.
Can anyone help :) ?
For anyone that would be stuck with this here's how I tricked android :
I added a / after the .com so now the url I'm sending is :
https://example.com/?id=36

How to get all the posts of a facebook page?

I want to get all the posts of a facebook page. I am following the link https://developers.facebook.com/docs/graph-api/reference/v2.8/post
For example URL : http://graph.facebook.com/v2.8/{page_id}/feed%20HTTP/1.1
I get the response as :
{"error":
{
"message": "Unknown path components: /feed HTTP/1.1",
"type": "OAuthException",
"code": 2500,
"fbtrace_id": "Em5dm7wPr9k"
}
}
I got the page_id from my facebook page.
Is my URL correct? Is this the correct way to get all the posts?
This is the correct way: https://developers.facebook.com/tools/explorer/?method=GET&path=bladauhu%2Ffeed&version=v2.8
You don´t need the Page ID, but you need to get rid of the " HTTP/1.1" in the API call. It´s just http://graph.facebook.com/v2.8/{page_id}/feed.

How to import a message with the Gmail API in Java

I've studied this page:
https://developers.google.com/gmail/api/v1/reference/users/messages/import
I have some messages I will be keeping track of in my Android program. I will be keeping a record of the messages I will be deleting and I would like to import those messages slated for deletion into a new folder (In Gmail API folders are called Labels).
I tried the test form:
POST https://www.googleapis.com/gmail/v1/users/omitted%40gmail.com/messages/import?deleted=false&internalDateSource=dateHeader&neverMarkSpam=true&processForCalendar=false&key={YOUR_API_KEY}
{
"payload": {
"body": {
"data": "hellio"
}
}
}
Response:
400 OK
- SHOW HEADERS -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidArgument",
"message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required"
}
],
"code": 400,
"message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required"
}
}
There is a problem sending the request but I am not sure at all what it means. What I need is some Java that sends a request that will import a message where I can specify the body, received date and subject line. Is it possible?
Thank you.
P.S. I just wanted to delete the attachments not the whole message but after reading the Gmail API documents it looks like this is not possible and is only possible through IMAP. The reason I am asking this question is so that I don't have to move my whole program to IMAP.
You are pretty close with your code. What you need to do is write a mail compliant with the RFC 2822 standard, and Base64-encode said mail and make it URL-safe (replace all + with - and all / with _) before inserting it.
You are better at Java than me, but if I would make a correct message in the browser's Developer Console, it could look something like this:
var encodedMail = btoa(
"Date: Thu, 1 Jan 1970 12:00:00 -0000\n" +
"From: emtholin#gmail.com\n" +
"To: emtholin#gmail.com\n" +
"Subject: Example Subject\n\n" +
"This is the body of the mail"
).replace(/\+/g, '-').replace(/\//g, '_');
Then I just insert this mail with the same request you issued:
POST https://www.googleapis.com/gmail/v1/users/me/messages?internalDateSource=dateHeader&key={YOUR_API_KEY}
{
"raw": "RGF0ZTogVGh1LCAxIEphbiAxOTcwIDEyOjAwOjAwIC0wMDAwCkZyb206IGVtdGhvbGluQGdtYWlsLmNvbQpUbzogZW10aG9saW5AZ21haWwuY29tClN1YmplY3Q6IEV4YW1wbGUgU3ViamVjdAoKVGhpcyBpcyB0aGUgYm9keSBvZiB0aGUgbWFpbA=="
}
As you can see, this worked great!

Android push notification error in Corona

Even by using sample project provided in Corona SDk , I get a notification with error 400. I guess my json data is correct. Following is the code for Json message.
local jsonMessage =
[[
"registration_ids": ["]] .. tostring(googleRegistrationId) .. [["],
"data":
{
"alert": "Hello World!",
"sound": "default"
}
}
]]
This is the message on my device.
Based on the 400 error code, the problem must be your JSON :
400
Only applies for JSON requests. Indicates that the request could not
be parsed as JSON, or it contained invalid fields (for instance,
passing a string where a number was expected). The exact failure
reason is described in the response and the problem should be
addressed before the request can be retried.
With all the square brackets and html tag, it's really hard to understand from your question how your JSON actually looks like.
Anyway, here's how it should look like :
{
"registration_ids": ["some reg id"],
"data":
{
"alert": "Hello World!",
"sound": "default"
}
}
I solved this error by changing the format of my json and checking the format on this link http://jsonlint.com/# . This was a great help and also I replace alert icon by custom icon by using this:
http://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar.html

Categories

Resources