当前位置: 移动技术网 > 移动技术>移动开发>Android > 详解Android提交数据到服务器的两种方式四种方法

详解Android提交数据到服务器的两种方式四种方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

android应用开发中,会经常要提交数据到服务器和从服务器得到数据,本文主要是给出了利用http协议采用httpclient方式向服务器提交数据的方法。

代码比较简单,这里不去过多的阐述,直接看代码。

/**
 * @author dylan
 * 本类封装了android中向web服务器提交数据的两种方式四种方法
 */
public class submitdatabyhttpclientandordinaryway {

 /**
 * 使用get请求以普通方式提交数据
 * @param map 传递进来的数据,以map的形式进行了封装
 * @param path 要求服务器servlet的地址
 * @return 返回的boolean类型的参数
 * @throws exception
 */
 public boolean submitdatabydoget(map<string, string> map, string path) throws exception {
 // 拼凑出请求地址
 stringbuilder sb = new stringbuilder(path);
 sb.append("?");
 for (map.entry<string, string> entry : map.entryset()) {
  sb.append(entry.getkey()).append("=").append(entry.getvalue());
  sb.append("&");
 }
 sb.deletecharat(sb.length() - 1);
 string str = sb.tostring();
 system.out.println(str);
 url url = new url(str);
 httpurlconnection httpconn = (httpurlconnection) url.openconnection();
 httpconn.setrequestmethod("get");
 httpconn.setreadtimeout(5000);
 // get方式的请求不用设置什么dooutput()之类的吗?
 if (httpconn.getresponsecode() == httpurlconnection.http_ok) {
  return true;
 }
 return false;
 }

 /**
 * 普通方式的dopost请求提交数据
 * @param map 传递进来的数据,以map的形式进行了封装
 * @param path 要求服务器servlet的地址
 * @return 返回的boolean类型的参数
 * @throws exception
 */
 public boolean submitdatabydopost(map<string, string> map, string path) throws exception {
 // 注意post地址中是不带参数的,所以newurl的时候要注意不能加上后面的参数
 url url = new url(path);
 // post方式提交的时候参数和url是分开提交的,参数形式是这样子的:name=y&age=6
 stringbuilder sb = new stringbuilder();
 // sb.append("?");
 for (map.entry<string, string> entry : map.entryset()) {
  sb.append(entry.getkey()).append("=").append(entry.getvalue());
  sb.append("&");
 }
 sb.deletecharat(sb.length() - 1);
 string str = sb.tostring();

 httpurlconnection httpconn = (httpurlconnection) url.openconnection();
 httpconn.setrequestmethod("post");
 httpconn.setreadtimeout(5000);
 httpconn.setdooutput(true);
 httpconn.setrequestproperty("content-type", "application/x-www-form-urlencoded");
 httpconn.setrequestproperty("content-length", string.valueof(str.getbytes().length));
 outputstream os = httpconn.getoutputstream();
 os.write(str.getbytes());
 if (httpconn.getresponsecode() == httpurlconnection.http_ok) {
  return true;
 }
 return false;
 }

 /**
 * 以httpclient的doget方式向服务器发送请数据
 * @param map 传递进来的数据,以map的形式进行了封装
 * @param path 要求服务器servlet的地址
 * @return 返回的boolean类型的参数
 * @throws exception
 */
 public boolean submitdatabyhttpclientdoget(map<string, string> map, string path) throws exception {
 httpclient hc = new defaulthttpclient();
 // 请求路径
 stringbuilder sb = new stringbuilder(path);
 sb.append("?");
 for (map.entry<string, string> entry : map.entryset()) {
  sb.append(entry.getkey()).append("=").append(entry.getvalue());
  sb.append("&");
 }
 sb.deletecharat(sb.length() - 1);
 string str = sb.tostring();
 system.out.println(str);
 httpget request = new httpget(sb.tostring());

 httpresponse response = hc.execute(request);
 if (response.getstatusline().getstatuscode() == httpurlconnection.http_ok) {
  return true;
 }
 return false;
 }
 
 /**
 * 以httpclient的dopost方式提交数据到服务器
 * @param map 传递进来的数据,以map的形式进行了封装
 * @param path 要求服务器servlet的地址
 * @return 返回的boolean类型的参数
 * @throws exception
 */
 public boolean submintdatabyhttpclientdopost(map<string, string> map, string path) throws exception {
 // 1. 获得一个相当于浏览器对象httpclient,使用这个接口的实现类来创建对象,defaulthttpclient
 httpclient hc = new defaulthttpclient();
 // dopost方式请求的时候设置请求,关键是路径
 httppost request = new httppost(path);
 // 2. 为请求设置请求参数,也即是将要上传到web服务器上的参数
 list<namevaluepair> parameters = new arraylist<namevaluepair>();
 for (map.entry<string, string> entry : map.entryset()) {
  namevaluepair namevaluepairs = new basicnamevaluepair(entry.getkey(), entry.getvalue());
  parameters.add(namevaluepairs);
 }
 // 请求实体httpentity也是一个接口,我们用它的实现类urlencodedformentity来创建对象,注意后面一个string类型的参数是用来指定编码的
 httpentity entity = new urlencodedformentity(parameters, "utf-8");
 request.setentity(entity);
 // 3. 执行请求
 httpresponse response = hc.execute(request);
 // 4. 通过返回码来判断请求成功与否
 if (response.getstatusline().getstatuscode() == httpurlconnection.http_ok) {
  return true;
 }
 return false;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网