JAVA_http的post请求 实例

 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 } 

 

转载于:https://www.cnblogs.com/1989hero/archive/2013/01/09/2852559.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值