How to properly resolve conflict during azure synchronization from android? - android

I'm in trouble with azure synchronization, I'm able to detect the number of pending operations by mCLient.getSyncContext().getPendingOperations(); but can't resolve them. Any help or suggestion?

When working with in an offline scenario and you have pending operations, you can push them to the server by using the push() function.
It would look something like the following:
try {
MobileServiceSyncContext syncContext = mClient.getSyncContext();
syncContext.push().get();
// here you would do a pull on any table you want to grab data from
}
catch (final MobileServiceConflictException e)
{
// the server item causing the exception can be obtained by calling e.getItem()
JsonObject serverObject = e.getItem();
// on the sync context, call one of the following:
// .cancelAndDiscardItem() to cancel update and discard local item
// .cancelAndUpdateItem() to update the local item with the server's copy
// .cancelAndUpdateItem(JsonObject item) to update the server's item w/ the local
// make sure to call push() again on the sync context
}
All in all - make sure you call push() on the MobileServiceSyncContext and then handle any MobileServiceConflictException that may return.

If I catch for MobileServiceConflictException android studio tells me that the Exception is never thrown in this corresponding try block.
try {
MobileServiceSyncContext syncContext = mClient.getSyncContext();
syncContext.push().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (MobileServiceConflictException e) {
}

Related

Update and retrieve chat history in smack with openfire -Android

I am making a chat application with smack library and openfire as a server but everytime i exit the chat conversation activity between two users and come back, the whole chat gets erased. I have already enabled archive settings to store one to one messages in the server but i do not know how to implement it in the app.
I want to show chats history in recyclerview by the sender and the receiver in the recyclerview.
currently i have implemented this function which caused error
private void setChatHistory(String entityBareId) {
EntityBareJid jid = null;
try {
jid = JidCreate.entityBareFrom(entityBareId);
} catch (XmppStringprepException e) {
e.printStackTrace();
}
MamManager manager = MamManager.getInstanceFor(mConnection);
MamManager.MamQueryResult r = null;
try {
try {
r = manager.mostRecentPage(jid, 10);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotLoggedInException e) {
e.printStackTrace();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
}
if (r.forwardedMessages.size() >= 1) //printing first of them
{
Message message = (Message) r.forwardedMessages.get(0).getForwardedStanza();
Log.i("mam", "message received" + message.getBody());
MessagesData data = new MessagesData("send",message.getBody());
mMessagesData.add(data);
mAdapter = new ConversationAdapter(mMessagesData);
recyclerView.setAdapter(mAdapter);
}
}
Error was
Attempt to read from field 'java.util.List org.jivesoftware.smackx.mam.MamManager$MamQueryResult.forwardedMessages' on a null object reference
At r.forwardedmessages.size()>=1.
Thanks in advance
If you want to keep history of conversation, you must save them in database. MAM just for fetching old conversation from server like when you uninstall or logout the app and decide to reinstall and get old messages.
For getting messages from server be sure you already enabled it, then forwarded messages shouldnt be null. here is a guide to enable it.

Android - where can I see the request url that is generated when setDataSource(..) is called?

I am trying to add headers to my setDataSource() method. Is there any way I could see the request itself that is sent ? I need to do this because I'd like to confirm if the url generated by setDataSource method is correctly formed. I don't however see any apis in the MediaPlayer class that can help me do this. Any direction or a solution would be most appreciated.
For non-file media source, the framework handle it by MediaHTTPConnection which is a hide API. You can change its field VERBOSE to true to see the printed log.
Since it's not exported, we can't use it directly. The following code might helpful, but I am not sure if it works. Run it before setDataSource().
try {
Class mediaServiceClass = Class.forName("android.media.MediaHTTPConnection");
Field field = mediaServiceClass.getDeclaredField("VERBOSE");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.setBoolean(null, true);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
You can refer here to see the detail implementation of MediaHTTPConnection.

Fetch joined members list from group, xmpp;

I'm trying to fetch joined member list from particular group based on room JID, there are couple of method exists, mentioned below:
MultiUserChat.getMembers(); It return XMPPError: forbidden - auth
MultiUserChat.getOccupants(); It return self info only
MultiUserChat.getParticipants(); It returnXMPPError: forbidden - auth
But none of working getting exception XMPPError: forbidden - auth, it seem there is something issue with privacy,which I'm not able to figure out.
Note: JID the name of the room in the form "roomName#service"
As per xmpp documents room/group should be non anonymous to retrieve all members list. So try to set anonymous = false in .yml or configuration file at server side or you can set that also in configuration while creating rooms.
Please not that affiliation should at-least member to retrieve member list
Use this :
multiUserChatManager = MultiUserChatManager.getInstanceFor(XMPPConnection);
MultiUserChat muc =multiUserChatManager.getMultiUserChat("dopee#conference.104.155.155.5");
try {
List<Affiliate> admin=muc.getAdmins();
System.out.println("Admin=====>>>"+admin);
List<String> userlist=muc.getOccupants();
List<Affiliate> member=muc.getMembers();
List<Occupant> memBer=muc.getParticipants();
List<Affiliate> owner=muc.getOwners();
System.out.println("userlist=====>>>"+ userlist.toString());
System.out.println("usercount=====>>>"+ muc.getOccupantsCount());
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}

Android: How to check condition with async task return value

I am doing some processing in async task to check some values on server if the value present server php script returns true otherwise false.
everything is working fine, but whenever i am trying to check condition with the http response string the condition never gets checked..
partial code:
CheckDevice cd = new CheckDevice();
String chk[] = new String[1];
chk[0] = di.imei;
try {
cd.execute(chk).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
cd.getFinalResult(); //This returns string "true" (working outside if);
if(cd.getFinalResult == "true"){
//This loop never executes... why?
regButton.setEnabled(false);
regButton.setText("Device Already Registered");
Toast.makeText(getApplicationContext(), cd.getFinalResult(), Toast.LENGTH_LONG).show();
}
What is the problem? someone please tell...
Do,
cd.getFinalResult().equals("true") //OR
cd.getFinalResult().equalsIgnoreCase("true")
as you cannot compare strings using ( == ).
Check this for details.

Ormlite ObjectCache returning old data

I'm using Ormlite on Android and with the ObjectCache enabled, I get old data back after updating the table with an UpdateBuilder and a ColumnExpression. I have read through the doc and it does not warn against using the UpdateBuilder with the cache enabled.
The settings table should have just 1-5ish rows max. The updateColumnExpression seems like an easy way to allow only one of the rows to be true.
Is this the expected behavior?
public void setActiveSetting(String id)
{
try {
UpdateBuilder<Settings, Integer> updateBuilder2 = getHelper().getSettingsDao().updateBuilder();
updateBuilder2.updateColumnExpression("active", "id = " + id );
updateBuilder2.update();
} catch (SQLException e){
e.printStackTrace();
}
}
And this is the call that returns the outdated data:
public List<Settings> getSettings()
{
List<Settings> settings = null;
try {
settings = getHelper().getSettingsDao().queryForAll();
} catch (SQLException e) {
e.printStackTrace();
}
return settings;
}
And the settings DAO:
public Dao<Settings, Integer> getSettingsDao()
{
if (null == settingsDao) {
try {
settingsDao = getDao(Settings.class);
settingsDao.setObjectCache(true);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
return settingsDao;
}
Disabling the ObjectCache does return the correct data, but this data is fetched quite frequently, so I'd like to keep it.
Thanks
Is this the expected behavior?
Unfortunately, yes. If you had updated the object using dao.update(...); then the cache would know that the object needed to be refreshed. By using the UpdateBuilder to make mass changes to the table, there is no way for the cache to know which objects were affected.
You will need to clear the cache after your call to the UpdateBuilder finishes.

Categories

Resources