Friday, February 25, 2011

Create web site launcher in Android

In this tutorial we will try to create a simple application to launch specific web address. Like a shortcut, this application will call another android application and launch it, in this case will launch default browser and open specific web address.

We will use the power of Android Intent. Here is example of the code:


import android.content.Intent;
import android.os.Bundle;

public class SiteLauncher extends Activity {

Intent intent;
public SiteLauncher(){
String webAddress="";
intent=new Intent(Intent.ACTION_VIEW);
/** Called when the activity is first created. */


public void onCreate(Bundle savedInstanceState) {




Thursday, February 24, 2011

POST data to server from Android

Today most of application need to communicate with server, in this case client-server architecture is needed. For example facebook client application for Android and iPhone. Both of these application exactly using client-server architecture. There are many protocols for client-server architecture. The most famous protocol is HTTP, in this protocol we can send data using two methods, with POST method or GET method.

In this article we will explore about POST method in android application.

  1. First we must declare these objects

  2. HttpClient client=new DefaultHttpClient();
    HttpPost httpPost=new HttpPost(LOGIN_ADDRESS);
    //LOGIN_ADDRESS is URL string address of login in server, e.g:

  3. Collect data which will be sent in ArrayList

  4. List pairs=new  ArrayList();
    String strUsername=username.getText().toString();
    String strPassword=password.getText().toString();
    pairs.add(new BasicNameValuePair("username", strUsername));
    pairs.add(new BasicNameValuePair("password", strPassword));

  5. Call setEntity method from HttpPost object/instance
    httpPost.setEntity(new UrlEncodedFormEntity(pairs));

  6. Call execute method from HttpPost object/instance
    HttpResponse response= client.execute(httpPost);