可以去一下地址下载:
http://pan.baidu.com/s/1hqrJF7m
/**
* 实用工具类来获取服务器资源
*
* get方法传送数据
*
* 1、通过path设定传送方式
* 2、创建客户端
* 3、得到输入流
* 4、读取流准备工作
* 5、读取并写入
* @throws IOException
* @throws ClientProtocolException
*
*/
1 public static String getHttpResult(String path) throws ClientProtocolException, IOException{ 2 /*1、通过path设定传送方式*/ 3 4 HttpGet get=new HttpGet(path); 5 /*2、创建客户端*/ 6 HttpClient client=new DefaultHttpClient(); 7 //通过get方式发送数据给服务器 8 HttpResponse response=client.execute(get); 9 /*3、得到输入流*/ 10 if(response.getStatusLine().getStatusCode()==200){ 11 InputStream in=response.getEntity().getContent(); 12 13 /*4、读取流准备工作*/ 14 ByteArrayOutputStream bos=new ByteArrayOutputStream(); 15 byte[]arr=new byte [1024]; 16 int len=0; 17 18 /*5、读取并写入*/ 19 while((len=in.read(arr))!=-1){ 20 bos.write(arr, 0, len); 21 } 22 byte[]b=bos.toByteArray(); 23 return new String(b,0,b.length); 24 } 25 26 27 28 return null; 29 }
/**
* 实用工具类来获取服务器资源
*
* Post方法传送数据
*
* 1、通过path设定传送方式
* 2、创建客户端
* 3、得到输入流
* 4、读取流准备工作
* 5、读取并写入
* @throws IOException
* @throws ClientProtocolException
*
*/
1 public static String getHttpResult(String path) throws ClientProtocolException, IOException{ 2 /*0、初始化要发送的数据用list存储*/ 3 List<NameValuePair> list=new ArrayList<NameValuePair>(); 4 list.add(new BasicNameValuePair("name", "zhangsan")); 5 list.add(new BasicNameValuePair("name", "lisi")); 6 list.add(new BasicNameValuePair("name", "wangwu")); 7 /*1、通过path设定传送方式*/ 8 9 HttpPost post=new HttpPost(path); 10 /*2、创建客户端*/ 11 HttpClient client=new DefaultHttpClient(); 12 //通过post表单方式发送数据给服务器 13 14 //建立表单 15 UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list,"utf-8"); 16 //装载到post中 17 post.setEntity(entity); 18 19 HttpResponse response=client.execute(post); 20 /*3、得到输入流*/ 21 if(response.getStatusLine().getStatusCode()==200){ 22 InputStream in=response.getEntity().getContent(); 23 24 /*4、读取流准备工作*/ 25 ByteArrayOutputStream bos=new ByteArrayOutputStream(); 26 byte[]arr=new byte [1024]; 27 int len=0; 28 29 /*5、读取并写入*/ 30 while((len=in.read(arr))!=-1){ 31 bos.write(arr, 0, len); 32 } 33 byte[]b=bos.toByteArray(); 34 return new String(b,0,b.length); 35 } 36 37 38 39 return null; 40 } 41
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。