文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > 计算机软件及应用 > Android之用HTTP的get,post,HttpClient三种方式向service提交文本数据
侵权投诉

Android之用HTTP的get,post,HttpClient三种方式向service提交文本数据

Android之用HTTP的get,post,HttpClient三种方式向service提交文本数据

客户端代码示例:

/** * HTTP请求 * @author kesenhoo * */ public class HttpRequest { public static boolean sendXML(String path, String xml)throws Exception {

byte[] data = xml.getBytes();

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST");

conn.setConnectTimeout(5 * 1000);

//如果通过post提交数据,必须设置允许对外输出数据

conn.setDoOutput(true);

conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");

conn.setRequestProperty("Content-Length",

String.valueOf(data.length));

OutputStream outStream = conn.getOutputStream();

outStream.write(data);

outStream.flush();

outStream.close();

if(conn.getResponseCode()==200)

{

return true;

}

return false;

}

/**

* 通过get方式提交参数给服务器

* @param path

* @param params

* @param enc

* @return

* @throws Exception

*/

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

{

//构造如下形式的字符串,这里的字符串依情况不同

// ?method=save&title=435435435&timelength=89&

//使用StringBuilder对象

第1页

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email