网站前端用什么做,个人网站免备案,宁波seo站外优化推广,跨境电商资讯网Android系统中主要提供了两种方式来进行HTTP通信#xff0c;HttpURLConnection和HttpClient#xff0c;几乎在任何项目的代码中我们都能看到这两个类的身影#xff0c;使用率非常高。不过HttpURLConnection和HttpClient的用法还是稍微有些复杂的#xff0c;如果不进行适当封…Android系统中主要提供了两种方式来进行HTTP通信HttpURLConnection和HttpClient几乎在任何项目的代码中我们都能看到这两个类的身影使用率非常高。不过HttpURLConnection和HttpClient的用法还是稍微有些复杂的如果不进行适当封装的话很容易就会写出不少重复代码。于是乎一些Android网络通信框架也就应运而生比如说AsyncHttpClient它把HTTP所有的通信细节全部封装在了内部我们只需要简单调用几行代码就可以完成通信操作了。再比如Universal-Image-Loader它使得在界面上显示网络图片的操作变得极度简单开发者不用关心如何从网络上获取图片也不用关心开启线程、回收图片资源等细节Universal-Image-Loader已经把一切都做好了。这里简单介绍下HttpURLConnection和HttpClient的使用。至于框架后面会研究后再介绍HttpClient的get使用HttpClient mClient; //http客户端public static HttpEntity getEntity(String uri,ArrayList params,int method) throws ClientProtocolException, IOException{mClientnew DefaultHttpClient();HttpUriRequest requestnull;switch (method) {case HTTP_GET://get请求StringBuilder sbnew StringBuilder(uri);//判断设置参数为不为空if(null!params!params.isEmpty()){sb.append(?);//设置配置参数for (BasicNameValuePair param : params) {sb.append(param.getName()).append().append(URLEncoder.encode(param.getValue(), utf-8)).append();}sb.deleteCharAt(sb.length()-1); //删除多余的 }HttpGet getnew HttpGet(sb.toString()); //发送get请求requestget;break;}//cookie缓存HttpClientParams.setCookiePolicy(mClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);//连接时间mClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);//设置请求超时时间mClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);//执行请求 获得响应HttpResponse response mClient.execute(request);if(response.getStatusLine().getStatusCode()200){ //如果成功 返回HttpEntity 对象return response.getEntity();}return null;}HttpURLConnection的post使用 post是 表单方式请求的URL url new URL(actionUrl);HttpURLConnection con(HttpURLConnection)url.openConnection();con.setReadTimeout(10 * 1000); //读数请求时间con.setConnectTimeout(10 * 1000); //连接超时/* 允许Input、Output不使用Cache */con.setDoInput(true); //以后就可以使用conn.getInputStream().read();con.setDoOutput(true); //以后就可以使用conn.getOutputStream().write() get用不到这个con.setUseCaches(false); //不使用缓存/* 设置传送的methodPOST */con.setRequestMethod(POST);/* setRequestProperty */con.setRequestProperty(Connection, Keep-Alive); //保持tcp连接con.setRequestProperty(Charset, UTF-8); //传输字符格式 UTF-8con.setRequestProperty(Content-Type,multipart/form-data;boundaryboundary);/* 设置DataOutputStream */DataOutputStream ds new DataOutputStream(con.getOutputStream());ds.writeBytes(twoHyphens boundary end);ds.writeBytes(Content-Disposition: form-data; name\file1\;filename\newName \ end);ds.writeBytes(end);/* 取得文件的FileInputStream */FileInputStream fStream new FileInputStream(uploadFile);/* 设置每次写入1024bytes */int bufferSize 1024;byte[] buffer new byte[bufferSize];int length -1;/* 从文件读取数据至缓冲区 */while((length fStream.read(buffer)) !-1){/* 将资料写入DataOutputStream中 */ds.write(buffer, 0, length);}ds.writeBytes(end);ds.writeBytes(twoHyphens boundary twoHyphens end);/* close streams */fStream.close();ds.flush();/* 取得Response内容 */InputStream is con.getInputStream();int ch;StringBuffer b new StringBuffer();while( ( ch is.read() ) !-1 ){b.append( (char)ch );}/* 将Response显示于Dialog */showDialog(上传成功b.toString().trim());/* 关闭DataOutputStream */ds.close();