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:

package tnto.info.android.sitelauncher;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class SiteLauncher extends Activity {

Intent intent;
public SiteLauncher(){
String webAddress="http://ipb.ac.id";
intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(webAddress));
}
/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

startActivity(intent);

finish();
}
}

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: http://172.17.0.18/login.php

  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);