Saturday, November 5, 2011

Android Tutorial Communication with Server using Http

There are people who say use android without an internet connection like drinking tea without sugar. You must have felt how good it diandroid browsing, playing games diandroid. Download the application dimarket. To find a restaurant open aja deh android then find the closest restaurant to our location. In addition we can also facebook or twitter without opening a browser, but through the application.

Why could really mean that android application server to connect to facebook and twitter dong.

There are some interesting questions from several readers, who seem to want to build android applications that use data from the server.

Anything else is asking. For the data should be stored didatabase local or server?

If the application is only used to record and view personal data (which is filled by only those users are) better data are stored dilocal database. An example application that I created AHFinancial Record.

But if the data is used by many people, we input, other people can see the data must be stored in server. Application Examples facebook earlier. If we update the status. Well our friends can see.

Back kemateri principal, how Android communicate with the server?

To communicate with the server there are several ways we can go is through http connection or Native Socket Connection.

At this time we focus material using Http Connection to hook our android application with the server.

If we use the Http Connection means we have to have a web server. In this case generally Apache, for those who have been learning php and mysql should've known.

For those who have not installed apache on a computer or notebook, can install LAMP XAMPP for windows or for linux here will be directly installed Apache, PHP and MySQL.

For Windows root directory usually exist in the directory htdoc

For Linux usually in / var / www

If we search the internet for standard data formats between applications and servers android bias using XML, JSON

But we will not discuss it at this time. Our goal in this tutorial is that we can send data to the server and the server receives and processes the server then send the data to our application and we can accept applications

To let us make application to log into the server, here we must menginputkan user and password. For that let us create a file login.php

02 $user = $_GET['user'];//get nilai user from client
03 $password = $_GET['password'];//get nilai pass from client
04 $sret = '';
05
06 if ($user=='agus' && $password=='android'){
07 $sret = 'Login Success';
08 }else{
09 $sret = 'Login Fail, User dan Password Salah';
10 }
11 echo $sret;
12
13?>

From the PHP script above means that the data is sent from the client must use the GET method. here will be de check the value the user and password, if the user is agus and the password is android, the server then sends the answer "Login Sucess". If the value of the username and password are not agus and android then the server will amengirimkan answer "Login Fail, User and Password is wrong.

As we know in this Http mode there are two methods that can use the get and post

Now let's make projectnya

1. Create a New Project, the contents of the information required in accordance with the picture below




















At the time the project was first made there are 3 important files are directly made, namely

main.xml
string.xml
MainActivity.java

2. edit the file main.xml

xml version="1.0" encoding="utf-8"?>
02<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03 android:orientation="vertical"
04 android:layout_width="fill_parent"
05 android:layout_height="fill_parent"
06 >
07
08<TextView
09 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:text="Komunikasi Ke Server menggunakan Http"
12 />
13<TextView android:text="User :" android:id="@+id/TextView01" android:layout_width="wrap_content"android:layout_height="wrap_content">TextView>
14<EditText android:text="" android:id="@+id/txtUser" android:layout_width="fill_parent"android:layout_height="wrap_content" >EditText>
15<TextView android:text="Password :" android:id="@+id/TextView02" android:layout_width="wrap_content"android:layout_height="wrap_content">TextView>
16<EditText android:text="" android:id="@+id/txtPassword" android:layout_width="fill_parent"android:layout_height="wrap_content" >EditText>
17<Button android:text="Login" android:id="@+id/btnLogin" android:layout_width="fill_parent"android:layout_height="wrap_content">Button>
18<TextView android:text="Status:" android:id="@+id/TextView03" android:layout_width="wrap_content"android:layout_height="wrap_content" >TextView>
19<EditText android:text="" android:id="@+id/txtStatus" android:layout_width="fill_parent"android:layout_height="wrap_content" android:editable="false">EditText>
20LinearLayout>


In this main.xml we make

2 pieces of EditText for Input User and Password
1 piece Button "Login" as a trigger to send data
Showing 1 piece EditText to return results from the server
3. . Edit the file MainActivity.java

MainActivity.java is a major player, which will be executed when the application is run. In this class, we create to be able to read the data the user entered in this input user and password, kemudan send it to the server when you click the login button and the display data received by the server.

0 komentar:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Lady Gaga, Salman Khan