博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 通过Get/Post提交请求数据到服务器
阅读量:7120 次
发布时间:2019-06-28

本文共 3162 字,大约阅读时间需要 10 分钟。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package
com.lifengfeng.submitdata.utils;
 
import
java.io.ByteArrayOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.net.HttpURLConnection;
import
java.net.MalformedURLException;
import
java.net.URL;
import
java.net.URLConnection;
import
java.net.URLEncoder;
 
import
android.util.Log;
 
public
class
NetUtils {
 
    
private
static
final
String TAG =
"NetUtils"
;
     
    
/**
     
* 使用post的方式登录
     
* @param userName
     
* @param password
     
* @return
     
*/
    
public
static
String loginOfPost(String userName, String password) {
        
HttpURLConnection conn =
null
;
        
try
{
            
URL url =
new
URL(
""
);
             
            
conn = (HttpURLConnection) url.openConnection();
             
            
conn.setRequestMethod(
"POST"
);
            
conn.setConnectTimeout(
10000
);
// 连接的超时时间
            
conn.setReadTimeout(
5000
);
// 读数据的超时时间
            
conn.setDoOutput(
true
);
// 必须设置此方法, 允许输出
//          conn.setRequestProperty("Content-Length", 234);     // 设置请求头消息, 可以设置多个
             
            
// post请求的参数
            
String data =
"username="
+ userName +
"&password="
+ password;
             
            
// 获得一个输出流, 用于向服务器写数据, 默认情况下, 系统不允许向服务器输出内容
            
OutputStream out = conn.getOutputStream(); 
            
out.write(data.getBytes());
            
out.flush();
            
out.close();
             
            
int
responseCode = conn.getResponseCode();
            
if
(responseCode ==
200
) {
                
InputStream is = conn.getInputStream();
                
String state = getStringFromInputStream(is);
                
return
state;
            
}
else
{
                
Log.i(TAG,
"访问失败: "
+ responseCode);
            
}
        
}
catch
(Exception e) {
            
e.printStackTrace();
        
}
finally
{
            
if
(conn !=
null
) {
                
conn.disconnect();
            
}
        
}
        
return
null
;
    
}
 
    
/**
     
* 使用get的方式登录
     
* @param userName
     
* @param password
     
* @return 登录的状态
     
*/
    
public
static
String loginOfGet(String userName, String password) {
        
HttpURLConnection conn =
null
;
        
try
{
            
String data =
"username="
+ URLEncoder.encode(userName) +
"&password="
+ URLEncoder.encode(password);
            
URL url =
new
URL(
""
+ data);
            
conn = (HttpURLConnection) url.openConnection();
             
            
conn.setRequestMethod(
"GET"
);      
// get或者post必须得全大写
            
conn.setConnectTimeout(
10000
);
// 连接的超时时间
            
conn.setReadTimeout(
5000
);
// 读数据的超时时间
             
            
int
responseCode = conn.getResponseCode();
            
if
(responseCode ==
200
) {
                
InputStream is = conn.getInputStream();
                
String state = getStringFromInputStream(is);
                
return
state;
            
}
else
{
                
Log.i(TAG,
"访问失败: "
+ responseCode);
            
}
        
}
catch
(Exception e) {
            
e.printStackTrace();
        
}
finally
{
            
if
(conn !=
null
) {
                
conn.disconnect();     
// 关闭连接
            
}
        
}
        
return
null
;
    
}
     
    
/**
     
* 根据流返回一个字符串信息
     
* @param is
     
* @return
     
* @throws IOException
     
*/
    
private
static
String getStringFromInputStream(InputStream is)
throws
IOException {
        
ByteArrayOutputStream baos =
new
ByteArrayOutputStream();
        
byte
[] buffer =
new
byte
[
1024
];
        
int
len = -
1
;
         
        
while
((len = is.read(buffer)) != -
1
) {
            
baos.write(buffer,
0
, len);
        
}
        
is.close();
         
        
String html = baos.toString(); 
// 把流中的数据转换成字符串, 采用的编码是: utf-8
         
//      String html = new String(baos.toByteArray(), "GBK");
         
        
baos.close();
        
return
html;
    
}
}

转载地址:http://zhsel.baihongyu.com/

你可能感兴趣的文章
Arcgis Engine 添加一个Symbol符号样式步骤
查看>>
kafka 控制台命令
查看>>
alpha冲刺10
查看>>
睡觉了~~
查看>>
【LeetCode】28 - Implement strStr()
查看>>
Node.js与Sails~Model数据模型
查看>>
基础才是重中之重~如何整理BLL与DAL层的文件
查看>>
[转]没有找到 MFC42D.DLL,因此这个应用程序未能启动。重新安装应用程序可能会修复此问题。解决方法!...
查看>>
我再也不-或许永远不-用zend studio-受够了!
查看>>
软件工程(2019)第三次作业
查看>>
Java性能调优
查看>>
第 6 章 存储 - 039 - Data Volume 之 bind mount
查看>>
异步IO
查看>>
MySQL
查看>>
【转】Linux内核结构详解
查看>>
DevExpress学习03——label控件的背景色问题
查看>>
Cass环境下光标无显示
查看>>
linux系统监控命令汇总
查看>>
谷歌:全球10大爬升最快搜索关键字排行榜 Google Zeitgeist 2011
查看>>
firewalld 极速上手指南
查看>>