xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zzw.request.MainActivity" > <EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" /> <EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/username"
android:hint="输入密码" /> <Button
android:id="@+id/get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/password"
android:onClick="getClient"
android:text="get请求" /> <Button
android:id="@+id/post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/get"
android:onClick="postClient"
android:text="post请求" /> </RelativeLayout>
java代码:
1 package com.zzw.httpClient;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import org.apache.http.HttpEntity;
10 import org.apache.http.HttpResponse;
11 import org.apache.http.NameValuePair;
12 import org.apache.http.client.ClientProtocolException;
13 import org.apache.http.client.HttpClient;
14 import org.apache.http.client.entity.UrlEncodedFormEntity;
15 import org.apache.http.client.methods.HttpGet;
16 import org.apache.http.client.methods.HttpPost;
17 import org.apache.http.impl.client.DefaultHttpClient;
18 import org.apache.http.impl.conn.DefaultClientConnection;
19 import org.apache.http.message.BasicNameValuePair;
20
21 import android.app.Activity;
22 import android.os.Bundle;
23 import android.view.Menu;
24 import android.view.MenuItem;
25 import android.view.View;
26 import android.widget.EditText;
27 import android.widget.Toast;
28
29 public class MainActivity extends Activity {
30 EditText username_et = null;
31 EditText passWord_et = null;
32
33 @Override
34 protected void onCreate(Bundle savedInstanceState) {
35 super.onCreate(savedInstanceState);
36 setContentView(R.layout.activity_main);
37 username_et = (EditText) findViewById(R.id.username);
38 passWord_et = (EditText) findViewById(R.id.password);
39 }
40
41 public void getClient(View view) {
42 new Thread(new Runnable() {
43 @Override
44 public void run() {
45 try {
46 // 1、获得一个client,相当于浏览器
47 HttpClient client = new DefaultHttpClient();
48 // 2、定义一个get请求,并且封装参数
49 String data = "username=" + username_et.getText().toString() + "&password="
50 + passWord_et.getText().toString();
51 HttpGet get = new HttpGet("http://10.0.2.2:8080/baidu/LoginServelt?" + data);
52 // 3、用定义好的浏览器来请求一个地址
53 HttpResponse response = client.execute(get);
54 int status = response.getStatusLine().getStatusCode();
55 if (status == 200) {
56 final String content = getStringFromStream(response.getEntity().getContent());
57 runOnUiThread(new Runnable() {
58 @Override
59 public void run() {
60 Toast.makeText(MainActivity.this, "返回信息:" + content, 0).show();
61 }
62 });
63
64 }
65 } catch (ClientProtocolException e) {
66 // TODO Auto-generated catch block
67 e.printStackTrace();
68 } catch (IOException e) {
69 // TODO Auto-generated catch block
70 e.printStackTrace();
71 }
72
73 }
74 }).start();
75 }
76
77 public void postClient(View view) {
78 new Thread(new Runnable() {
79 @Override
80 public void run() {
81 try {
82 // 1、获得client
83 HttpClient client = new DefaultHttpClient();
84 // 2、获得POST请求及设置参数
85 HttpPost post = new HttpPost("http://10.0.2.2:8080/baidu/LoginServelt");
86 List<NameValuePair> parameters = new ArrayList<NameValuePair>();
87 NameValuePair nameuser = new BasicNameValuePair("username", username_et.getText().toString());
88 NameValuePair passWord = new BasicNameValuePair("passWord", passWord_et.getText().toString());
89 parameters.add(nameuser);
90 parameters.add(passWord);
91
92 HttpEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8");
93 post.setEntity(entity);
94 // 3、执行post请求
95 HttpResponse response = client.execute(post);
96 int status = response.getStatusLine().getStatusCode();
97 if (status == 200) {
98 final String content = getStringFromStream(response.getEntity().getContent());
99 runOnUiThread(new Runnable() {
100
101 @Override
102 public void run() {
103 Toast.makeText(MainActivity.this,"POST:"+content,0).show();
104 }
105 });
106
107
108 }
109 } catch (ClientProtocolException e) {
110 // TODO Auto-generated catch block
111 e.printStackTrace();
112 } catch (IOException e) {
113 // TODO Auto-generated catch block
114 e.printStackTrace();
115 }
116 }
117 }).start();
118 }
119
120 public String getStringFromStream(InputStream in) throws IOException {
121 byte[] buffer = new byte[1024];
122 ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
123 int leng = 0;
124 while ((leng = in.read(buffer, 0, 1024)) != -1) {
125 byteArray.write(buffer);
126 }
127 String content = byteArray.toString();
128 byteArray.close();
129 return content;
130 }
131 }