1 实例一:
2
3 String uriAPI = "http://192.168.1.100:8080/test/test.jsp"; //这是我测试的本地,大家可以随意改
4 /*建立HTTPost对象*/
5 HttpPost httpRequest = new HttpPost(uriAPI);
6 /*
7 * NameValuePair实现请求参数的封装
8 */
9 List <NameValuePair> params = new ArrayList <NameValuePair>();
10 params.add(new BasicNameValuePair("u", "沈大海"));
11 params.add(new BasicNameValuePair("p", "123"));
12 try
13 {
14 /* 添加请求参数到请求对象*/
15 httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
16 /*发送请求并等待响应*/
17 HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
18 /*若状态码为200 ok*/
19 if(httpResponse.getStatusLine().getStatusCode() == 200)
20 {
21 /*读返回数据*/
22 String strResult = EntityUtils.toString(httpResponse.getEntity());
23 mTextView1.setText(strResult);
24 }
25 else
26 {
27 mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
28 }
29 }
30 catch (ClientProtocolException e)
31 {
32 mTextView1.setText(e.getMessage().toString());
33 e.printStackTrace();
34 }
35 catch (IOException e)
36 {
37 mTextView1.setText(e.getMessage().toString());
38 e.printStackTrace();
39 }
40 catch (Exception e)
41 {
42 mTextView1.setText(e.getMessage().toString());
43 e.printStackTrace();
44 }
45 ////大家能根据这个代码实现个android用户登陆吗?
46
47
48
49
50
51 实例二:
52 public void MyFunction{
53
54 HttpClient httpclient = new DefaultHttpClient();
55
56 //你的URL
57
58 HttpPost httppost = new HttpPost("http://www.winu.cn/post_datas.php");
59
60
61
62 try {
63
64 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
65
66 //Your DATA
67
68 nameValuePairs.add(new BasicNameValuePair("id", "12345"));
69
70 nameValuePairs.add(new BasicNameValuePair("stringdata", "eoeAndroid.com is Cool!"));
71
72
73
74 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
75
76
77
78 HttpResponse response;
79
80 response=httpclient.execute(httppost);
81
82 } catch (ClientProtocolException e) {
83
84 // TODO Auto-generated catch block
85
86 e.printStackTrace();
87
88 } catch (IOException e) {
89
90 // TODO Auto-generated catch block
91
92 e.printStackTrace();
93
94 }
95
96 }