Tuesday, 18 September 2012

Use XML to reduce access time - Android

I was working with language translation in phone gap. In that case I was in need of 120 labels for translation. Out of 120, 60 are English and remaining are of other language. I was inserting all values in database ones on the very first start of application, because of that it was taking much time to start for first time. After that when I was using those labels in my app, there was much delay. Means all elements on page like button, text boxes were coming first and then labels. Because of that buttons were remaining empty first and then after some time text was appearing on that.

In order to solve this problem I am using XML database for fast access. Now I created simple XML file as given below,

english_translation_test.xml

<xml>
    <lbl1>Label - 1</ lbl1 >
    <lbl2>Label - 2</ lbl2 >
    <lbl3>Label - 3</ lbl3 >
</xml>

In this way I designed simple XML file, one for English and another for different language. I placed this file at place www => xml => english_translation_test.xml
In order to access this file I am using following code. In this code remember one thing that path must be relative to www folder. Path is in red color.


$.get('xml/english_translation_test.xml ', function(rawXml) {
getPlainXml(rawXml);
});

function  getPlainXml(xml)
{
        $('#label_1').html($(xml).find("lbl1").text());

$('#label_2').html($(xml).find("lbl2").text());

$('#label_3').html($(xml).find("lbl3").text());
}

Now in this case if language is different then use another language that's it. And you will get fast access to all your labels. You may need this process while code optimization and performance issues.

Background jerk problem because of keypad - Phone gap

In our phone gap application many times we encounter the problem of background. In this problem background image moves up when soft keypad comes up. In some cases all elements on screen like buttons, text fields moves up more than necessary.
In order to keep your screen and background fix you can use following solutions,

1. For keeping your elements on screen fix, you can use "overflow: hidden;" in your css for that element. You can apply this css to the div in which all elements are present.

2. By using above solution elements on screen will become fix but background image may move up. In order to fix background as well as elements you have to add just one line code to your activity tag present in manifest file. 

Code:
 <activity
            android:windowSoftInputMode="adjustPan"
            android:name=".Test"
            android:label="Test"
            android:configChanges="orientation|keyboardHidden" >
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
 </activity>
Here you have to add line in red color. You can try different options of that property.

Monday, 10 September 2012

SQL injection attack

SQL injection attack is one of the old attack. I found it interesting so giving short description on it. It is mainly related with web sites. This attack is because of few user input unchecked conditions. This is also called as blind attack where attacker is not running with any knowledge of database. Here I am giving few examples.

1. General attack:
 If you have to enter just one field in text box like password then here is attack.
Generally we know that whatever text we are going to enter in text box will appear in WHERE clause of SQL.
Ex. Password : test123
In this case query will something look like.
SELECT * FROM member WHERE password = 'test123'
Now see how can we attack using some SQL like input. Instead of entering only password think about following input,
Password : test123' OR 'X' = 'X
Now query will formed something like this,
SELECT * FROM member WHERE password = 'test123' OR 'X' = 'X'
where red and bold text is user input which is valid for text field but actually should not.

2. Finding some user:
Now consider case where we know email address. And whatever that data we are entering into text field is going to SELECT clause like,
SELECT * FROM members WHERE eaddr = 'Our_input'
Now in this case think about input,
email@xyz.com' OR full_name LIKE '%je%
Then query will be,
SELECT * FROM members WHERE eaddr = 'email@xyz.com' OR full_name LIKE '%je%'
Here it is assumed that field name is known. After that you can refine LIKE clause with % to get exact user.

In this way you can guess different attack tricks. But now a days every web site handle these kind of attacks.
Enjoy this....

Friday, 7 September 2012

Tools For Developers

Here I am giving list of useful tools for development. All these tools are free. You can download these tools easily from internet. Here I am giving short description, you read in details.

1. Firebug:
Firebug is one of the add-on which comes with mozilla firefox browser. Just go to "Tools" option of browser. In that select "Add-on". And search "firebug". Select the one which is having symbol like given below.
Using this you can easily develop css for your web page and can debug with scripts like java script.

2. Colorzilla:
This is another add-on with mozilla firefox. In the same way go to "Tools" then "Add-on". Now search colorzilla. Select one which is having symbol like given below.
You can use this add-on for getting hex code of color of any component on web page.

3. Measure it:
You have to search this add-on at the same place. With symbol like this,

You can use this for measuring height and width of components on web page. You can use this only for web pages opened in mozilla firefox.

4. Jing:
You can use this tool for taking screen shots and measuring any component on your computer screen. When you will start this tool it will give you pointer to measure and taking screen shots. Just search this tool on google.

5. SQLite database browser:
This tool will help you to browse through the SQlLite database. I have used this tool for phone gap development, where I used to take database file out from eclipse and open it in SQLite database browser for analysis.

6. Beyond Compare:
Beyond compare is a tool for comparing different versions of files, folders. It is very useful when number of people are working on the same project. You can go for google search for this. Symbol is like this,



7. Team box:
Team box is one of the online project management tool for simple projects. You will find this tool at http://teambox.com/. This is good for small team, small project. It allows you to create tasks and then you can have conversation for that task.

8. Rally dev:
This is another online project management software. It is designed for handling complex projects with big teams. You can find details at http://rallydev.com/. It provides more facilities to user from design to testing.

9. JDeveloper:
http://www.oracle.com/technetwork/developer-tools/jdev/overview/index.html
Check out this link for more details. It is one of new tool by oracle for java.

Enjoy these tools.

Wednesday, 5 September 2012

Android - PHP server connectivity

In android php server connectivity is very easy task. Here I am giving android code which will communicate with php script on wamp server. At first let's have a look at android client code.

1. Android Client:
At first you have to create array list with name value pair. Here is code for that,

Code:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name1", "value1"));
nameValuePairs.add(new BasicNameValuePair("name2", "value2"));

In this way you can put any number of name value pairs. You can access these values at server side means in php script in $_REQUEST array by there name. Means "value1" can be accessed using $_REQUEST['name1'].
Now we can send this array list to server as given below.

Code:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ip_address_of_server/test.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is1 = entity.getContent();
byte[]b = new byte[200];
is1.read(b);
String responce= new String(b);

In response you will get return value from server. If your response from server is in JSON format then you can parse that as given below,

Code:

JSONObject jo=new JSONObject(responce);
String res = jo.get("result");

Where "result" is name that you are returning from server.

2. Server Part:
At server you have to write php script. Here is sample code,

Code:
test.php

<?php
$done = array('result' => 'success', 'value1' => $_REQUEST['name1']);
echo json_encode($done);
?>

This script will return value related with "name1".

At android client in "result" you will get "success" message and in "value1" you will get respected value.



Tuesday, 4 September 2012

Database problem in phonegap

In phone gap many times you can see that database operations are not working properly. In many cases I found that we are redirecting to some other page just after the database operation. Here is block of code,

insertData(data1, data2);
window.location = "abc.html";

And here is insertData function,


function insertDatas(data1, data2) {
        //Here I am assuming db is initialized properly.
db.transaction(insertHere, errorDataTable, successDataTable);
    function insertHere(tx) {
        var query = "insert into dataTable values('" + data1 + "','" + data2 + "')";
        tx.executeSql(query);
    }
    function errorDataTable(err){
          alert("Error : " + err);
    }
    function successDataTable() {
          alert("Success");
    }
}

Now here sometimes it may happen that, values are going to database. At place you may have update query In case of update values may not updated in database.

In order to fix this problem safe way to code in phonegap is to commit database after your operations. Here is code block with commit statement which is doing the same functionality as above but safely,

insertData(data1, data2);
//After this statement instead of redirecting call commit,

db.transaction(commit, errorCommit, successCommit);
 function commit(tx){
        tx.executeSql("COMMIT",[], commited, errorCommit);
 }
         
 function commited() {
        console.log('commit successful..');
        window.location = "abc.html";
}
         
function errorCommit() {
}
function successCommit() {
         console.log('commit successful.. redirecting');
        window.location = "abc.html"
}

So in phonegap always it is safe to commit your database. I have faced this problem with update statement.

Saturday, 1 September 2012

Maintain Your Data & Alerts

This blog is specially for the people who want to maintain there data in sophisticated manner and want to have alerts for specific thing on the web. Here I am giving few easy and free ways to maintain your data and alerts. You can use these services easily. For every facility you will need google log in.

1. Alerts:
At the top of google page select "More" option. In this select "Even more" option. It take you to the new page. Here you have to find "Alert" option. Click that "Alert" option. Again it will take you to new form. First test box will be of "Search query" in that text box simple add about what you want to get alerts. Suppose I want alerts on java technology the my search query will be "Java technology". Select remaining option as you wish. Finally click "Create new alert". It also provides you facility to manage your alerts properly. Alerts will be given to you via email.

2. Docs:
You will find this option in "Even more" page as given above. Here you will be able to maintain your documents, presentations and spreadsheets. It will allow you share link with specific people and will allow your group to edit that document in synchronized manner. It allows you to maintain these documents properly.

3. Books:
You will find this option in "Even more" page as given above. Here you will be able to maintain your online library. It will allow you to create your own book library. Ones you are running with your library you can add book to it easily. While browsing books on google books at top it will give you option of adding books to your library. In learn more section you will get detailed information.

4. Reader:
You will find this option in "More" tab on google page. It will allow you to keep track of particular blog or something like that. On the main page of reader it will show you option of "Sbscribe". Click that and add url in specified format. Ex. "tech-world-brij.blogspot.in". Now on update of it, you will get notification.

5. Panoramio:
You will find this option in "Even more" page. Here you will be able to explore photos of world. It allows user to upload photos with location. It will provide you facility to share your photos with your friends. You get all details on the same page.

6. Groups:
You will find this option in "Even more" page. It will allow you to create your own discussion groups. You will be able to set access level to your group. It is interesting one. See it's details.

I am going to add more things shortly.....