`
1028826685
  • 浏览: 922239 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类

android向服务器提交数据

阅读更多

在我们的实际开发中我们很多时候都需要向服务器提交数据来获取服务器响应数据...

下面我就来总结一下通常使用的提交参数的方法有GET和POST

首先来看一下GET方式提交数据 注意:GET方式提交数据一般在2k左右(因服务器而异)

GET方式:

看下面的方法

public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws Exception{

//path 是我们提交参数的路径

//params 是提交的参数,通过一个Map来封装这些提交参数

//enc 是编码方式(一般在服务器是:ISO-8859-1 android:UTF-8 还要常见的 gbk、gb2312)

//下面是组拼提交路径
StringBuilder sb = new StringBuilder(path);
sb.append('?');
//提交参数----> ?method=save&title=435435435&timelength=89&
//我们通过先得到这个Map的实体集合,让后通过迭代去得到实体的Key、Value

for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}

//去掉最后多月的&
sb.deleteCharAt(sb.length()-1);
//下面的得到url对象打开连接建立通道
URL url = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置请求方式 (这里因为是采用Http协议)

conn.setRequestMethod("GET");
//设置请求超时时间

conn.setConnectTimeout(5 * 1000);
if(conn.getResponseCode()==200){
return true;
}
return false;
}

POST方式:

看下面方法:

 

<1、我们直接面向Http协议去提交数据

 

//需要HttpWahcer查看去查看请求头协议(在android其它软件)
public static boolean sendPostRequest(String path, Map<String, String> params, String enc) throws Exception{
//path :提交参数路径

//params:是提交数据 通过Map来封装这些数据

//enc:编码方式 -->为了不出现乱码

// --->提交数据:title=dsfdsf&timelength=23&method=save

StringBuilder sb = new StringBuilder();
//组拼提交数据

 

if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);
}

 

//通过HttpWatch可以知道 在我们直接面向Http协议通过POST提交数据时我们可以只需

//提交的请求头有Content-Type:和Content-Length: 和我们提交数据即可

//注意:在这里提交的Content-Length:是以二进制来计量长度的


byte[] entitydata = sb.toString().getBytes();//得到实体的二进制数据

//得到连接通道

URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置请求方式和连接超时

conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);

//注意:这里设置允许输出必须设置true因为我们通过Http协议下服务器提交数据所有必须设置输出

conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据

//Content-Type: application/x-www-form-urlencoded
//Content-Length: 38
//设置请求头的属性

conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
//通过通道得到输出流

OutputStream outStream = conn.getOutputStream();
//写出数据

outStream.write(entitydata);
//让缓存中数据及时输出

outStream.flush();
outStream.close();
if(conn.getResponseCode()==200){
return true;
}
return false;
}

<2、

我们通过开源的HttpClient去向服务器提交数据,这里android已经集成了这个开源项目所有就不用加入jar包了

在这个开源的项目中它已经帮助我们封装了上面我们通过直接面向Http协议提交数据中的请求协议部分

方法如下:

//SSL HTTPS Cookie
//注意:如果我们在其他项目中如果用到 SSL 、HTTPS、Cookie时使用这个开源项目会使得编程更简单

 

public static boolean sendRequestFromHttpClient(String path, Map<String, String> params, String enc) throws Exception{
//参数如上

 

//下面通过名字对和List集合来封装我们的数据

// NameValuePair就相当于我们的Map

List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}

//通过UrlEncodedFormEntity对我们发送的数据进行编码(enc),让后获得实体数据部分

UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);//得到经过编码过后的实体数据

//通过HttpPost类来提交我们的数据
HttpPost post = new HttpPost(path); //form
//设置请求提交的数据实体

post.setEntity(entitydata);

//相当于浏览器来提交数据

DefaultHttpClient client = new DefaultHttpClient(); //浏览器
HttpResponse response = client.execute(post);//执行请求
if(response.getStatusLine().getStatusCode()==200){
return true;
}
return false;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics