Android:HTTP协议访问网络
来源:互联网 发布:linux 获取网卡ip 编辑:程序博客网 时间:2024/05/29 19:37
Http协议访问网络:
使用HTTP协议访问网络
get请求:
public static String getJsonByInternet(String path){ try { URL url = new URL("http://ww.baidu/com"); //打开连接 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); if(200 == urlConnection.getResponseCode()){ //得到输入流 InputStream is =urlConnection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while(-1 != (len = is.read(buffer))){ baos.write(buffer,0,len); baos.flush(); } return baos.toString("utf-8"); } } catch (IOException e) { e.printStackTrace(); } return null; }//获取其他页面的数据 /** * POST请求获取数据 */ public static String postDownloadJson(String path,String post){ URL url = null; try { url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式 // conn.setConnectTimeout(10000);//连接超时 单位毫秒 // conn.setReadTimeout(2000);//读取超时 单位毫秒 // 发送POST请求必须设置如下两行 httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); // 获取URLConnection对象对应的输出流 PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); // 发送请求参数 printWriter.write(post);//post的参数 xx=xx&yy=yy // flush输出流的缓冲 printWriter.flush(); //开始获取数据 BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len; byte[] arr = new byte[1024]; while((len=bis.read(arr))!= -1){ bos.write(arr,0,len); bos.flush(); } bos.close(); return bos.toString("utf-8"); } catch (Exception e) { e.printStackTrace(); } return null; }
使用 HttpClient访问网络
get请求:
public class void HttpResquestWithHttpClitent(){ HttpClient client=new DefaultHttpClient(); HttpGet httpGet=new HttpGet("http://www.baidu.com"); HttpResponse httpResponse=client.execute(httpGet); //判断请求和响应是否成功 if (httpResponse.getStatusLine().getStatusCode()==200) { HttpEntity entity=httpResponse.getEntity(); String response=EntityUtils.toString(entity); }}
Post请求:
HttpClient client=new DefaultHttpClient();HttpPost httpPost = new HttpPost("http://www.baidu.com");// NameValuePair集合来存放待提交的参数List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("username", "admin"));params.add(new BasicNameValuePair("password", "123456"));//NameValuePair集合传入到一个UrlEncodedFormEntity中UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "utf-8");httpPost.setEntity(entity);HttpResponse httpResponse=httpClient.execute(httpPost);//判断请求和响应是否成功if (httpResponse.getStatusLine().getStatusCode()==200) { HttpEntity entity=httpResponse.getEntity(); String response=EntityUtils.toString(entity); }
0 0
- Android Http协议访问网络
- Android - HTTP 协议访问网络
- Android:HTTP协议访问网络
- Android学习使用HTTP协议访问网络
- Android之使用HTTP协议访问网络
- Android使用HTTP协议访问网络
- Android使用Http协议访问网络
- Android使用Http协议访问网络
- Android入门-使用Http协议访问网络
- Android之使用HTTP协议访问网络
- Android—使用Http协议访问网络
- android中使用http协议访问网络
- Android使用http协议访问网络
- HTTP 协议访问网络
- Http协议访问网络
- android访问http协议
- 使用HTTP协议访问网络
- 使用HTTP协议访问网络
- Js+less实现自定义的日历
- Spring AOP自定义注解实现系统日志记录管理
- git 本地分支和远程分支联系 Threre is no tracking information for the current branch
- Android HTTP协议请求网络(三)之HttpURLConnection方式
- @PathVariable指定请求的URL路径里面的变量
- Android:HTTP协议访问网络
- SVG path 元素详解
- Unity3D 创建脚本自动添加模板注释
- kafka clients 2.10.0.0 不能import jar包
- hdu5747
- 把java 程序注册为windows 服务
- Python中flatten用法
- StringRedisTemplate常用操作
- Leetcode学习(24)—— Reverse Integer