Android网络编程之通过Post传递参数
2011-12-22 17:06
302 查看
前面一篇文章《Android网络编程之通过Get方法实现》我们讲解了Android中通过Get方法实现网络编程的基本例子,我们知道,在网络编程中还有另一个方法,就是通过Psot来实现与服务器的数据交换,因此在本文中ATAAW.COM将对Android中Post方法做简要介绍。 同样的,我们用一个例子来做分析,以熟悉各个方法的使用。 A、使用Map来存储参数 Map<String, String> map = new HashMap<String, String>(); map.put("name", "ataaw"); map.put("password", "ataaw.com"); B、使用DefaultHttpClient创建HttpClient实例 DefaultHttpClient httpClient = new DefaultHttpClient(); C、构建HttpPost HttpPost post = new HttpPost(“http://www.ataaw.com/..”); D、将由Map存储的参数转化为键值NameValue List<BasicNameValuePair> postData = new ArrayList<BasicNameValuePair>(); for (Map.Entry<String, String> entry : map.entrySet()) { postData.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } E、使用编码构建Post实体 UrlEncodedFormEntity entity = new UrlEncodedFormEntity( postData, HTTP.UTF_8); F、设置Post实体 post.setEntity(entity); G、执行Post方法 HttpResponse response = httpClient.execute(post); H、获取返回实体 HttpEntity httpEntity = response.getEntity(); I、将H中返回实体转化为输入流 InputStream is = httpEntity.getContent(); J、读取输入流 StringBuffer sb = new StringBuffer(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = ""; while((line=br.readLine())!=null){ sb.append(line); } 以上就是Android中通过Post来实现客户端与服务端的通讯过程,完成参数封装到发送到服务器,最后接收服务端返回的数据,从而完成一个完整的HTTP数据传递过程。 |
相关文章推荐
- Android网络编程之通过Post传递参数
- Android网络编程之通过Post传递参数
- Android网络编程之通过Post传递参数
- Android: 网络编程之Post传递参数
- Android 网络通信之通过GET和POST方式提交参数给web应用
- Android:网络:通过GET和POST两种方式上传参数
- android网络编程之post提交数据
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- android笔记之--向网络发送GET/POST请求参数
- android笔记之--向网络发送GET/POST请求参数
- Android通过POST方法传递数据给服务器
- Android 通过WebService进行网络编程,使用工具类轻松实现
- Android http get/post传递参数
- [转]android 网络编程 HttpGet类和HttpPost类使用详解
- android 网络编程 HttpGet类和HttpPost类使用详解
- android 网络编程 HttpGet类和HttpPost类使用详解
- jsp页面之间通过post方法传递参数
- Android post请求网络数据(json作为参数)
- Android向服务器传递参数方式:Post&Get
- 在Js页面通过POST传递参数跳转到新页面详解