Wednesday, May 22, 2013

Sync Adapter for updating SQlite data

Android - Sync local Sqlite database to a remote SQLServer Database.
I have an android application which allows user to enter data and save it to locals device and the data needs to be uploaded periodically to remote SQL Server.
As the first step I developed a contentprovider to wrap the sqlitedb in the first application which was laready operationsl. As I already had a database handelr which extends SQLiteOpenHelper for the application. I developed the content provider with lots of dummy overrides and I kept using SQLiteOpenHelper for my first application. Only interesting area for the first application is its manifest file where the Authority and permissions are defined. Defining this took lots of time as I had to do  lots of trial and error.
     android:authorities="in.xxx.xxx.provider"
     android:exported="true"
     android:grantUriPermissions="true"
      android:readPermission="android.permission.permRead" />
   


First provider has to be within application  tags.
androidname is the file name of my provider class attached with the package name.
auhtoritis is the AUTHORITY declared inside the provider class.
Relevant portion of the provider class is

       //from vogela http://www.vogella.com/articles/AndroidSQLite/article.html
 private static final String AUTHORITY = "in.xxx.xxx.provider";

 private static final String BASE_PATH = "members";
 public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
     + "/" + BASE_PATH);

At this point I had my first application running. This application is responsible for all database operations. Now I need to develop another application which allows me to sync data periodically.

Little bit research and help from Stackoverflow shows that the best approach is to use SyncAdapter.  Sync Adapter has three componenets Account, Authentication and SyncAdapter.

Important links

Concept of SyncAdapter (android.content.AbstractThreadedSyncAdapter)

Connecting The Dots with Android SyncAdapter

Breaking down Google’s SampleSyncAdapter


How to run Sync Immediately

Reg selection of  Records to be updated from Server and vice versa

Idea about Authentication

Important resources



Looking back again at AndroidManifest, that strange meta-data tag in our service is the key piece that establishes the binding between a ContentAuthority and an account. It externally references another xml file (call it whatever you like, something relevant to your app.) Let's look at sync_myapp.xml:
xml version="1.0" encoding="utf-8" ?> 
 
    xmlns:android="http://schemas.android.com/apk/res/android"   
    android:contentAuthority="com.android.contacts"
    android:accountType="com.google" 
    android:userVisible="true" /> 
Okay, so what does this do? It tells Android that the sync adapter we've defined (the class that was called out in the name element of the  tag that includes the  tag that includes this file...) will sync contacts using a com.google style account.
All your contentAuthority strings have to all match, and match with what you're syncing -- This should be a string you define, if you're creating your own database, or you should use some existing device strings if you're syncing known data types (like contacts or calendar events or what have you.) The above ("com.android.contacts") happens to be the ContentAuthority string for contacts type data (surprise, surprise.)
accountType also has to match one of those known account types that are already entered, or it has to match one you're creating (This involves creating a subclass of AccountAuthenticator to get auth on your server... An article worth itself.) Again, "com.google" is the defined string identifying... google.com style account credentials (again, this should not be a surprise.)

Monday, October 29, 2012

Dynamically Loading a Dropdown with JSON data

function getstate(countryId) {
$.ajax(
{ type: "POST",
url: "OfficeData.asmx/Getstates",        
data: "{'countryId':" + (countryId) + "}",        
contentType: "application/json;
charset=utf-8",        
global: false,
async: false,        
dataType: "json",        
success: function(jsonObj)
{            
var listItems= "";
var jsonData = jsonObj.d;    
for (var i = 0; i < jsonData.Table.length; i++)
{
listItems+= " + jsonData.Table[i].statename + "
"; }    
$("#<%=DLState.ClientID%>").html(listItems); //DLState is the dropdown

JSON [JavaScript Object Notation]

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.


  • JSON is syntax for storing and exchanging text information. Much like XML.
  • JSON is smaller than XML, and faster and easier to parse.
JSON Example
{
"employees": [
{ "firstName":"Dibakar" , "lastName":"Ray" },
{ "firstName":"Sujit" , "lastName":"Banerjee" },
{ "firstName":"Anadomohan" , "lastName":"Chakraborty" }
]
}


Working with JSON Data ASP. Net

{"IsMember" : true, "Name" : "Surnider", "Age" : 24}
Define the User type to deserialize the JSON into.

public class User
{
    public bool IsMember { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}
DataContractJsonSerializer serializer =
    new DataContractJsonSerializer(typeof(User));
User user = (User)serializer.ReadObject(responseStream);

bool isMember = user.IsMember;
string name = user.Name;
int age = user.Age;

Saturday, March 28, 2009

Authentication Using LDAP

Intitally I tried to bind to the LDAP using admin password in the DirectoryEntry constructor and using  the Filter to authenticate usergiven userid and password. But failed as couldnot access UserPassword attribute.

Later I used Userid and password directly in the DirectoryEntry constructor. 

Limitation - Clear Text password. Need to fix it.

DirectoryEntry entry = new DirectoryEntry(LDAP://164.XXX.XX.X/o=XXX.in,dc=XXX,dc=in);

entry.Username ="uid="+email.Text+",ou=people,o=XXX Employees,o=XXX Support,o=XXX.in,dc=XXX,dc=in"; //email text box contains the user id and password text contains the password

entry.Password = password.Text;

entry.AuthenticationType = AuthenticationTypes.None;

DirectorySearcher searcher = new DirectorySearcher(entry);

try

{

searcher.Filter = "(uid=" + email.Text + ")";

SearchResult rs = searcher.FindOne();

if (rs != null)

{

//success

divOutput.InnerHtml="";

divOutput.InnerHtml = "Welcome - " + rs.Properties["cn"][0].ToString();

}

else

{

//Failure

divOutput.InnerHtml = "Error authenticating. Check Your User Id or Password";

}

}

catch (Exception ex)

{

divOutput.InnerHtml = "Error authenticating. " + ex.Message;

return;

}