Saturday, November 5, 2011

Android Tutorial Communication with Server using Http step 2

package com.agus.android.http;
02
03/**
04* Class MainActivity
05* @version 1.0 Apr 04, 2011
06* @author Agus Haryanto (agus.superwriter@gmail.com)
08*/
09
10import java.io.BufferedReader;
11import java.io.InputStream;
12import java.io.InputStreamReader;
13
14import org.apache.http.HttpResponse;
15import org.apache.http.client.HttpClient;
16import org.apache.http.client.methods.HttpGet;
17import org.apache.http.impl.client.DefaultHttpClient;
18
19import android.app.Activity;
20import android.os.Bundle;
21import android.util.Log;
22import android.view.View;
23import android.widget.Button;
24import android.widget.EditText;
25
26public class MainActivity extends Activity {
27private EditText txtUser;
28private EditText txtPassword;
29private EditText txtStatus;
30private Button btnLogin;
32/**
33* Method yang dipanggil pada saat aplikaasi dijalankan
34* */
35@Override
36public void onCreate(Bundle savedInstanceState) {
37
38super.onCreate(savedInstanceState);
39setContentView(R.layout.main);
40txtUser = (EditText) findViewById(R.id.txtUser);
41txtPassword = (EditText) findViewById(R.id.txtPassword);
42txtStatus = (EditText) findViewById(R.id.txtStatus);
43btnLogin = (Button) findViewById(R.id.btnLogin);
44//daftarkan even onClick pada btnLogin
45btnLogin.setOnClickListener(new Button.OnClickListener(){
46public void onClick(View v){
47url +="?user="+txtUser.getText().toString()+"&password="+txtPassword.getText().toString();
48getRequest(txtStatus,url);
49}
50});
51
52}
53
54/**
55* Method untuk Mengirimkan data kes erver
56* event by button login diklik
57*
58* @param view
59*/
60public void getRequest(EditText txtResult, String Url){
61Log.d("getRequest",Url);
62HttpClient client = new DefaultHttpClient();
63HttpGet request = new HttpGet(url);
64try{
65HttpResponse response = client.execute(request);
66txtResult.setText(request(response));




Step 1, Step 2, Step 3, step 4

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