As the graduation project involves Internet communication, it will soon be exposed to remote server interaction. At first, I wanted to directly connect to the database on the server. After searching for the database for half a day, I did not have any relevant documents, and it was not safe. Therefore, the web page is used to interact with the database.
Server: PHP + Apache
User. php
1 <?php
2
3 $name=$_POST['name'];
4
5 $passwd=$_POST['pwd'];
6
7 if($name=="yecao" && $passwd=="yecao")
8
9 {
10
11 echo "ok";
12
13 }else{
14 $getname=$_GET["getname"];
15 $getpwd=$_GET["getpwd"];
16 echo "your name:".$getname."and your pwd:".$getpwd;
17 }
18 ?>
Client: Android
XML layout code:
1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
3 Android: layout_width = "fill_parent"
4 Android: layout_height = "fill_parent"
5 Android: Orientation = "vertical">
6 <button
7 Android: text = "post sending"
8 Android: Id = "@ + ID/Post"
9 Android: layout_width = "fill_parent"
10 Android: layout_height = "wrap_content"
11/>
12 <button
13 Android: text = "get send"
14 Android: Id = "@ + ID/get"
15 Android: layout_width = "fill_parent"
16 Android: layout_height = "wrap_content"
17/>
18
19 <textview
20 Android: layout_width = "fill_parent"
21 Android: layout_height = "wrap_content"
22 Android: text = "@ string/hello"
23 Android: Id = "@ + ID/showtext"/>
24
25 </linearlayout>
Interface code:
1 package com. loin;
2
3 Import java. Io. bufferedreader;
4 Import java. Io. ioexception;
5 import java. Io. inputstream;
6 Import java. Io. inputstreamreader;
7 Import java. Io. outputstream;
8 Import java. util. arraylist;
9 Import java. util. List;
10
11 import org. Apache. http. header;
12 Import org. Apache. http. httpentity;
13 Import org. Apache. http. httpresponse;
14 Import org. Apache. http. namevaluepair;
15 Import org. Apache. http. Client. clientprotocolexception;
16 Import org. Apache. http. Client. httpclient;
17 import org. Apache. http. Client. entity. urlencodedformentity;
18 Import org. Apache. http. Client. Methods. httpget;
19 Import org. Apache. http. Client. Methods. httppost;
20 Import org. Apache. http. impl. Client. defaulthttpclient;
21 import org. Apache. http. Message. basicnamevaluepair;
22
23 Import Android. App. activity;
24 import Android. OS. Bundle;
25 import Android. View. view;
26 Import Android. View. window;
27 import Android. View. View. onclicklistener;
28 Import Android. widget. Button;
29 Import Android. widget. textview;
30
31 public class loginactivity extends activity {
32/** called when the activity is first created .*/
33 button postbt, getbt;
34 textview TV;
35 Public String URL;
36 @ override
37 public void oncreate (bundle savedinstancestate ){
38 Super. oncreate (savedinstancestate );
39 requestwindowfeature (window. feature_no_title );
40 setcontentview (R. layout. Main );
41 postbt = (button) findviewbyid (R. Id. post );
42 getbt = (button) findviewbyid (R. Id. Get );
43 url = "http: // 192.168.1.6/usr. php ";
44 TV = (textview) findviewbyid (R. Id. showtext );
45 postbt. setonclicklistener (postclick );
46 getbt. setonclicklistener (getclick );
47
48
49}
50 // post
51 private onclicklistener postclick = new onclicklistener (){
52
53 @ override
54 public void onclick (view v ){
55 // todo auto-generated method stub
56
57 postdata (URL, "yecao", "yecao ");
58}
59 };
60 // get
61 private onclicklistener getclick = new onclicklistener (){
62
63 @ override
64 public void onclick (view v ){
65 // todo auto-generated method stub
66 getdata (URL, "yecao", "yecao ");
67}
68 };
69
70 // postmethod
71 private void postdata (string URL, string nametext, string pwdtext)
72 {
73 try {
74 httpclient HTTP = new defaulthttpclient ();
75 httppost post = new httppost (URL );
76 list <namevaluepair> pair = new arraylist <namevaluepair> ();
77 pair. Add (New basicnamevaluepair ("name", nametext ));
78 pair. Add (New basicnamevaluepair ("PWD", pwdtext ));
79 post. setentity (New urlencodedformentity (pair, "UTF-8 "));
80 httpresponse response=http.exe cute (post );
81 httpentity ht = response. getentity ();
82 string result = "";
83 string line = "";
84 If (response. getstatusline (). getstatuscode () = 200)
85 {
86 bufferedreader reader = new bufferedreader (New inputstreamreader (HT. getcontent (); // read data in a stream to avoid Chinese garbled characters.
87 while (line = reader. Readline ())! = NULL)
88 {
89 result = Result + line;
90}
91 TV. settext (result );
92}
93} catch (exception e) {e. printstacktrace ();}
94}
//getMethod
95 private void getdata(String url,String nametext,String pwdtext)
96 {
97 HttpClient httpclient=new DefaultHttpClient();
98 url=url+"?getname="+nametext+"&getpwd="+pwdtext;
99 HttpGet get=new HttpGet(url);
100 try {
101 HttpResponse response=httpclient.execute(get);
102 HttpEntity entity=response.getEntity();
103 String result="";
104 String line="";
105 if(response.getStatusLine().getStatusCode()==200)
106 {
107 BufferedReader reader=new BufferedReader(new InputStreamReader(entity.getContent()));
108 while((line=reader.readLine())!=null)
109 {
110 result=result+line;
111 }
112 tv.setText(result);
113 }
114 } catch (Exception e) {
115 // TODO Auto-generated catch block
116 e.printStackTrace();
117 }
118 }
121
122 }
Add the Internet access permission to the configuration file:
<uses-permission android:name="android.permission.INTERNET"/>
:
Get:
Post: