基于apache的httpclient发送请求。

由于毕业设计要涉及到互联网的的通讯,所以很快就接触到于远程服务器交互。起初想是和服务器的数据库直接连接,后来找了半天,都没有相关的文档,而且也不安全。故转为通过网页来和数据库交互。

        服务端: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 ?>



       客户端:android

xml布局代码:

 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发送"
8 android:id="@+id/post"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 />
12 <Button
13 android:text="GET发送"
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>



界面代码:

  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.execute(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()));//以流的方式读取可以避免中文乱码。
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 }

最后还要给配置文件加上访问互联网的权限:

 <uses-permission android:name="android.permission.INTERNET"/>

 

效果图:

  get时候:

        

post的时候:

  

posted @ 2012-01-14 13:42  狼迹天涯  阅读(1250)  评论(0编辑  收藏  举报