I know that the response from the pubnub history() is:
[["message","Message","message"],"Start Time Token", "End Time Token"]
im creating an string to receive the response:
String msg = response.toString();
And this should give me the full array, but now to retrieve the first message im doing this:
String[] msgOne = msg[0];
And this is not working.
for pubnub history method , the response is a org.json.JSONArray so to get the messages array you can use something like this.
JSONArray messages = (JSONArray)( ((JSONArray)response).get(0));
JSONArray class here http://www.json.org/javadoc/ provides more info about the methods that you can use on messages variable.
Related
I have coded a bot to send a message response to a given command, !a. I want to store additional data on the message object, so that if the user reacts to that message the bot can read the hidden data property and know the original message resulted from !a.
Ideas I had:
Create a custom property on the message object: Message.Custom_Prop
Hijack an unused property: Maybe Message.webhookId?
Store hidden text in the form of an embed or message content.
I haven't been able to get any of these to work though.
The best way that I can think of is to store an array of message IDs that were triggered by !a. Something like this:
// At the start of your script
const commandResponses = [];
// Logic for sending the command response
message.channel.send("response here").then(msg => commandResponses.push(msg.id));
// Checking whether the message was a response
if (commandResponses.includes(reaction.message.id)) {
// It's a response so do stuff here
}
(Not tested so it's possible that this won't work)
I am trying to get JSON response from server and then display it in my android but failing to do so due to parsing error
my server response jsonformat= {
"Result": "Login Success",
"User_Name:": "Ayan"
}
But in android I can't ge the second object and it is returned as null
enter image description here
In your response, you have the field like "User_Name:" or did you wrote the response wrong?
JsonObject object = new JsonObject(response);
String result = object.getString("Result");
String userName = object.getString("User_Name:");
check this #Ayan
I am trying to get JSON response from server and then display it in my android but failing to do so due to parsing error
Android error
Caused by: org.json.JSONException: Value [{"Result":"Login Success","User_Name:":"Ayan"}] of type org.json.JSONArray cannot be converted to JSONObject
enter image description here
In your response, you have the field like "User_Name:" or did you wrote the response wrong?
check the second field clearly ["User_Name:":"Ayan"]
JsonObject object = new JsonObject(response);
String result = object.getString("Result");
String userName = object.getString("User_Name:");
It should match as like the field else it will give the exception
try this
JSONArray jsonarray = new JSONArray(response);
JsonObject object = jsonarray.getJSONObject(0);
String result = object.getString("Result");
String userName = object.getString("User_Name:");
I used to interact with the Gmail API since past year using these tests https://developers.google.com/gmail/api/v1/reference/users/messages/list#try-it but now this examples are failing because seems there are more messages but the next iteration is coming empty.
Problem is in this part of the code:
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(userId=user_id, q=query,
pageToken=page_token).execute()
messages.extend(response['messages'])
The error is raised when trying to access the response['messages'] as the unique key in the reponse is 'resultSizeEstimate' and is 0. Sounds like the page_token is pointing to a next empty page.
Is someone experiencing this issue as well?
If your last page perfectly contains the last email with that particular query, you will get a nextPageToken to a page with a response like this:
{
"resultSizeEstimate": 0
}
The easiest way around this is to just add a check if messages is part of the response:
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(userId=user_id, q=query, pageToken=page_token).execute()
if 'messages' in response:
messages.extend(response['messages'])
My fellow co-worker backend programmer said that he has configure an API that expect to receive something like this from my mobile app:
[{"id":50},{"id":60}]
I'm using Alamofire which receive param dictionary to be sent. But I believe this is also the same mechanism using NSURLSession or any other third party plugins.
The question is: how should I construct the dictionary to send an array of ids, like how a HTTP form can have several text field with the same id, and then it will be received on the server end as an array of id? What I've tried so far and all fails:
param.setValue(50, forKey:"id");
param.setValue(60, forKey:"id");
// this only send the last single value
param.setValue([50, 60], forKey:"id");
// this results in error (415 unsupported media type)
param.setValue(50, forKey:"id[0]");
param.setValue(60, forKey:"id[1]");
// this also results in error (415 unsupported media type)
How can I send this correctly just like how web form send? Thanks.
I think keyword for your question is "Alamofire send json"
If your server accepts json data, you can do like this:
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let values = [["id":50],["id":60]]
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(values, options: [])
Alamofire.request(request)
.responseJSON { response in
// do whatever you want here
}
Good luck!
The problem with the first method is you're overwriting the value for the key id, that's the reason why it only sends the last value. Try sending the params as an array.
let dict = NSMutableArray()
param.setValue(50, forKey:"id")
dict.addObject(param)
param.setValue(60, forKey:"id")
dict.addObject(param)
Pass the dict as the parameter for the request method.