I am trying to figure it out went through gmail-api developer guide.The message part in gmail-api does not contain any detail about sender any help is appreciated thanks.
Ok I finally did it.Get message payload and then get headers from payload loop through the headers with name "from"
format of header is of the form header=[name:"somename" value:"somevalue"]
here is my code hope it helps someone
private List<MessageReader> getDataFromApi() throws IOException {
String user = "me";
List<MessageReader> labels = new ArrayList<MessageReader>();
ListMessagesResponse listResponse = mActivity.mService.users().messages().list(user).setQ(query).execute();
//int i=0;
for (Message label : listResponse.getMessages()) {
Message m = mActivity.mService.users().messages().get(user, label.getId()).execute();
String a ="";
try{
List<MessagePart> parts =m.getPayload().getParts();
List<MessagePartHeader> headers = m.getPayload().getHeaders();
MessageReader mreader = readParts(parts);
mreader.setDate(m.getInternalDate());
for(MessagePartHeader header:headers){
String name = header.getName();
if(name.equals("From")||name.equals("from")){
mreader.setSender(header.getValue());
break;
}
}
labels.add(mreader);
}catch(Exception e){
a+="The following error occurred:\n" +
e.getMessage();
}
}
return labels;//extractData(labels);
}
private MessageReader readParts(List<MessagePart> parts){
MessageReader mreader = new MessageReader();
int cnt =0;
for(MessagePart part:parts){
try{
String mime = part.getMimeType();
if(mime.contentEquals("text/plain")){
String s = new String(Base64.decodeBase64(part.getBody().getData().getBytes()));
mreader.setText(s);
}else if(mime.contentEquals("text/html")){
String s = new String(Base64.decodeBase64(part.getBody().getData().getBytes()));
mreader.setHtml(s);
}else if(mime.contentEquals("multipart/alternative")){
List<MessagePart> subparts =part.getParts();
MessageReader subreader = readParts(subparts);
mreader.setText(subreader.getText());
mreader.setHtml(subreader.getHtml());
}else if(mime.contentEquals("application/octet-stream")){
cnt++;
mreader.setNo_of_files(cnt);
}
}catch(Exception e){
// get file here
}
}
return mreader;
}
public class MessageReader {
private String text;
private String html;
int no_of_files;
private String sender;
private long date;
// file data to be made
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getHtml() {
return html;
}
public void setHtml(String html) {
this.html = html;
}
public int getNo_of_files() {
return no_of_files;
}
public void setNo_of_files(int no_of_files) {
this.no_of_files = no_of_files;
}
}
You could first list messages, to get the id of messages.
Request:
GET https://www.googleapis.com/gmail/v1/users/me/messages
Response:
{
"messages": [
{
"id": "1504f80fcf4ceb5f",
"threadId": "1504f80fcf4ceb5f"
}, ...
]
}
Then, you could ask for the sender of these messages in a second request, by getting the actual message.
Request:
format = metadata
metadataHeaders = From
fields = payload/headers
GET https://www.googleapis.com/gmail/v1/users/me/messages/1504f80fcf4ceb5f?format=metadata&metadataHeaders=From&fields=payload%2Fheaders
Response:
{
"payload": {
"headers": [
{
"name": "From",
"value": "YouTube <noreply#youtube.com>"
}
]
}
}
Assuming that you already got message id using ListMessagesResponse,
here is how you can get sender, i.e. From field:
public String getFrom(String userId, String messageId) throws IOException {
Message message = mService.users().messages().get(userId, messageId).execute();
List<MessagePartHeader> headers = message.getPayload().getHeaders();
String from="";
for (MessagePartHeader header:headers){
if(header.getName().equals("From")){
from=header.getValue();
break;
}
}
return from;
}
From documentation we can know that Users.messages will give us the message with following properties
{
"id": string,
"threadId": string,
"labelIds": [
string
],
"snippet": string,
"historyId": unsigned long,
"internalDate": long,
"payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": string,
"value": string
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
]
},
"sizeEstimate": integer,
"raw": bytes
}
From the above properties we can use payload.headers[] to know the Sender mail.
The payload.headers[] will contain array of dictionaries of form {"name":string , "value":string}. To get the sender's mail, we have to find the dictionary with name == 'From' and its corresponding value will give us the sender's mail
I am attaching my python code for reference
#Call the Gmail API
#Before calling the API you have to create service using User credentials
results = service.users().messages().list(userId='me',labelIds = ['INBOX']).execute()
msgs = results.get('messages',[])
for msg in msgs:
mg = service.users().messages().get(userId='me', id=msg['id']).execute()
msg_headers = mg['payload']['headers']
for k in range(len(msg_headers)):
if msg_headers[k]['name'] == 'From':
print(msg_headers[k]['value'])
print("\n")
Related
i'm using retrofit for service calls.it's return 5 data sets.each data set contain 6 values.each data set contain one actual values and others are null.how can get actual values without null values?
POST man response is like bellow
{
"status": true,
"message": "successfully count new orders",
"data": [
{
"order_count": "3"
},
{
"pending_order": "0"
},
{
"schedule": "2017-11-18 12:00",
"id_order": "272",
"reference": "KB-838704"
},
{
"deli_orders": "0"
},
{
"store_count": "0"
}
]
}
Webservice call
WebserviceAPI apiService =retrofit.create(WebserviceAPI.class);
Call<OrderResponse> call = apiService.getSummery("countnew",token_acces);
call.enqueue(new Callback<OrderResponse>() {
#Override
public void onResponse(Call<OrderResponse> call, Response<OrderResponse> response) {
OrderResponse result = response.body();
List<Order> data=result.getData();
status=result.isStatus();
msg= result.getMessage();
for (Order a:data)
{
try {
Log.d("ress",""+a.getOrder_count()+" : "+a.getPending_order()+" : "+a.getReference()
+" : "+a.getSchedule()+" : "+a.getDeli_orders()+" : "+a.getStore_count());
//update
if( !(a.getOrder_count().contentEquals("null"))){
Log.d("ress","new order "+a.getOrder_count());
//txtNewOrder.setText(a.getOrder_count());
}
if( !(a.getPending_order().contentEquals("null"))){
Log.d("ress","pending "+a.getPending_order());
// txtPendingOrder.setText(a.getPending_order());
}
if( !(a.getReference().contentEquals("null"))){
Log.d("ress","reference "+a.getReference());
//txtRecentOrder.setText(a.getReference());
}
if( !(a.getSchedule().contentEquals("null"))){
Log.d("ress","time "+a.getSchedule());
//txtRemainingTime.setText(a.getSchedule());
}
if( !(a.getDeli_orders().contentEquals("null"))){
Log.d("ress"," delivered "+a.getDeli_orders());
//txtDeliveredOrder.setText(a.getDeli_orders());
}
if( !(a.getStore_count().contentEquals("null"))){
Log.d("ress","store "+a.getStore_count());
//txtStoreOrder.setText(a.getStore_count());
}
}catch (NullPointerException e){
}
}
}
#Override
public void onFailure(Call<OrderResponse> call, Throwable t) {
Log.d("fragres",""+t.getMessage());
}
});
Update : Order.java
public class Order {
String order_count;
String pending_order;
String schedule;
String id_order;
String reference;
String deli_orders;
String store_count;
public String getOrder_count() {
return order_count;
}
public void setOrder_count(String order_count) {
this.order_count = order_count;
}
public String getPending_order() {
return pending_order;
}
public void setPending_order(String pending_order) {
this.pending_order = pending_order;
}
public String getSchedule() {
return schedule;
}
public void setSchedule(String schedule) {
this.schedule = schedule;
}
public String getId_order() {
return id_order;
}
public void setId_order(String id_order) {
this.id_order = id_order;
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public String getDeli_orders() {
return deli_orders;
}
public void setDeli_orders(String deli_orders) {
this.deli_orders = deli_orders;
}
public String getStore_count() {
return store_count;
}
public void setStore_count(String store_count) {
this.store_count = store_count;
}
}
Logcat
when i check null inside for loop,it's display only first value (num 3)
i want to get :
1st value from 1 data set,2 nd value from 2nd data set,3rd and 4th value from 3 data set,5th value from 4th data set,6th value from 5th data set
Im trying to find an easy way to parse JSON data that are in an array. Ive been able to do this before with pure JSON Objects and OkHttp.
What I plan to do is make a simple recycler view that each row will contain the data from the JSON Array.
Note: there will be keys in each array that may or may not have a "viewers" parameter. If it does not exist I plan to make a default value of 0.
JSON example:
{
status_code: 200,
status_text: "OK",
errors: [ ],
data: {
items: [
{
stream: "channel1",
status: 1,
channel: 527,
provider: "akamai"
},
{
stream: "channel2",
status: 2,
channel: 565,
provider: "akamai",
viewers: 3
},
I have a class that stores the data (not sure if needed in the case of a JSON Array) called CurrentStreams.java
package com.example.concurrent;
public class CurrentStreams {
private String mStream;
private int mStatus;
private int mChannel;
private String mProvider;
private int mViewers;
public String getStream() {
return mStream;
}
public void setStream(String stream) {
mStream = stream;
}
public int getStatus() {
return mStatus;
}
public void setStatus(int status) {
mStatus = status;
}
public int getChannel() {
return mChannel;
}
public void setChannel(int channel) {
mChannel = channel;
}
public String getProvider() {
return mProvider;
}
public void setProvider(String provider) {
mProvider = provider;
}
public int getViewers() {
return mViewers;
}
public void setViewers(int viewers) {
mViewers = viewers;
}
}
In my MainActivity I initiate a new OkHttpClient and create the request and pass a method called getCurrentDetails(jsonData) which contains the following
private CurrentStreams getCurrentDetails(String jsonData) throws JSONException {
//New JSON Array
JSONArray jsonArray = new JSONArray(jsonData);
for (int i = 0; i < jsonArray.length(); i++){
JSONObject items = jsonArray.getJSONObject(i);
String streamName = items.getString("stream_name");
int status = items.getInt("status");
int channelId = items.getInt("channel_id");
String cdn = items.getString("cdn");
int viewers = items.getInt("viewers");
}
}
I assume im kinda lost at how to properly handle the JSON Array and save the data to then later populate the view with rows of data. Any help is greatly appreciated in advanced.
I would suggest to use gson.
You maybe want to take a look at this answer or this Tutorial
I send JSON data from android app. In server side I have issue - Web API server controller doesn't get parametrs. From client side I send json data like this:
{ "TypeID ": "1",
"FullName": "Alex",
"Title": "AlexTitle",
"RegionID": "1",
"CityID ": "1",
"Phone1": "+7(705)105-78-70"
}
Any help appreciated, please if you have some information, idea let me know, thank you!
This is controller
[System.Web.Http.HttpPost]
public HttpResponseMessage PostRequisition([FromBody]string requisition)
{
Requisition postReq = new Requisition();
if (!String.IsNullOrEmpty(requisition))
{
dynamic arr = JValue.Parse(requisition);
//PostReq model = JsonConvert.DeserializeObject<PostReq>(requisition);
postReq.FullName = arr.FullName;
postReq.CityID = Convert.ToInt32(arr.CityID);
postReq.RegionID = Convert.ToInt32(arr.RegionID);
postReq.TypeID = Convert.ToInt32(arr.TypeID);
postReq.UserID = 8;
postReq.Title = arr.Title;
postReq.Date = Convert.ToDateTime(arr.Date, CultureInfo.CurrentCulture);
postReq.Decription = arr.Description;
postReq.Phone1 = arr.Phone1;
postReq.Activate = false;
postReq.ClickCount = 0;
try
{
db.Requisition.Add(postReq);
db.SaveChanges();
Message msg = new Message();
msg.Date = DateTime.Now;
msg.Type = "POST";
msg.Text = "OK";
db.Message.Add(msg);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, postReq);
}
catch (Exception ex)
{
Message msg = new Message();
msg.Date = DateTime.Now;
msg.Type = "POST";
msg.Text = "ERROR";
db.Message.Add(msg);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, ex.Message);
}
}
else
{
Message msg = new Message();
msg.Date = DateTime.Now;
msg.Type = "POST";
msg.Text = "null";
db.Message.Add(msg);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, "null");
}
}
Your problem is simple. You are sending a JSON object but are expecting a string in your POST action. Simple way to fix this is creating a class that maps to your JSON object:
public class RequisitionViewModel
{
public int TypeID {get; set;}
public string FullName {get; set;}
public string Title {get; set;}
public int RegionID {get; set;}
public int CityID {get; set;}
public string Phone1 {get; set;}
}
Then, change your action signature to:
[FromBody]RequisitionViewModel requisition)
You also don't need all the converting in your code:
postReq.FullName = requisition.FullName;
postReq.CityID = requisition.CityID;
//other fields...
This is kind of weird issue i am having,
I am getting response from web service in this manner
"course": [
{
"id": 1,
"cou_name": "Engineering",
"L1": [
{
"id": 3,
"cou_name": "B.Tech",
},
{
"id": 4,
"cou_name": "M.Tech",
}]
},
{ ...... } ]
I have created POJO class to handle this response in this way
public class FilterCourseDetail {
public static final String ID = "id";
public static final String COU_NAME = "cou_name";
public static final String LEVEL_ONE = "L1";
public int id;
public String courseName;
public ArrayList<FilterCourseDetail> mLevelOneArrayList = new ArrayList<>();
public FilterCourseDetail(JSONObject object) {
setId(object.optInt(ID));
setCourseName(object.optString(COU_NAME));
try {
JSONArray jsonLevelOneArray = object.optJSONArray(LEVEL_ONE);
if(jsonLevelOneArray != null){
for (int i = 0; i < jsonLevelOneArray.length(); i++) {
mLevelOneArrayList.add(new FilterCourseDetail(jsonLevelOneArray.getJSONObject(i)));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//Normal getter setter
public ArrayList<FilterCourseDetail> getLevelOneArray() {
return this.mLevelOneArrayList;
}
}
And i am storing it in following manner
private ArrayList<FilterCourseDetail> mFilterCourseList = new ArrayList<>();
JSONArray jsonSearchFilterArray = jsonSearchFilterObject.getJSONArray("course");
for (int i = 0; i < jsonSearchFilterArray.length(); i++) {
mFilterCourseList.add(new FilterCourseDetail(jsonSearchFilterArray.getJSONObject(i)));
}
Retrieving it :
private ArrayList<FilterCourseDetail> mFilterLevelOneArray = new ArrayList<>();
mFilterLevelOneArray = mFilterCourseList.get(position).getLevelOneArray();
Problem :
When i check the size of mFilterCourseList for first time, it's fine,
but using it again by
mFilterLevelOneArray = mFilterCourseList.get(position).getLevelOneArray();
and checking the size, it returns 0, means it's automatically been cleared.
I don't understand this. also is this the proper way to handle Inner JSONArray ? please help me out when i am wrong
i'm developing an android application using quickblox api. I'm using chat SMACK functionality (1 to 1 chat) and i try receive message from chat. I can retrieve the message text, but i can't get customs parameters.
Here is a screen about my message variable:
The values i need are values stored in 'map' : tempo, telefono, stato, nome , prefisso.. how i can retrieve that? I just try to search on quickblox documentation, but i have an api error when try to implement DefaultPacketExtension.
Thank you
Here is an example how to do it using QuickBlox Android SDK 1.1
To send message with additional parameters:
Map<String, Object> addinfoParams = new HashMap<String, Object>();
addinfoParams.put(Consts.AGE, 22);
addinfoParams.put(Consts.TYPE, "actor");
final String BODY = "Hey QuickBlox!";
Message message = createMsgWithAdditionalInfo(USER_ID, BODY, addinfoParams);
Log.i(TAG, "message: " + message.toXML());
try {
qbPrivateChat.sendMessage(USER_ID, message);
} catch (XMPPException e) {
e.printStackTrace();
}
...
private Message createMsgWithAdditionalInfo(int userId, String body, Map<?, ?> addinfoParams){
Message message = new Message(QBChatUtils.getChatLoginFull(userId), Message.Type.chat);
String addInfo = ToStringHelper.toString(addinfoParams, "", Consts.ESCAPED_AMPERSAND);
MessageExtension messageExtension = new MessageExtension(Consts.QB_INFO, "");
try {
messageExtension.setValue(Consts.ADDITIONAL_INFO, addInfo);
} catch (BaseServiceException e) {
e.printStackTrace();
}
message.addExtension(messageExtension);
message.setBody(body);
return message;
}
To receive message and get custom parameters:
chatMessageListener = new ChatMessageListener() {
#Override
public void processMessage(Message message) {
String from = message.getFrom();
String messageBody = message.getBody();
List<MessageExtension> messageExtensions = message.getExtensions();
}
#Override
public boolean accept(Message.Type type) {
switch (type) {
case normal:
case chat:
case groupchat:
return true;
default:
return false;
}
}
};
More info in Chat snippets https://github.com/QuickBlox/quickblox-android-sdk/blob/master/snippets/src/com/quickblox/snippets/modules/SnippetsChat.java