public class HttpDownload { public String httpGet(String urlPath, String params) throws IOException { long ST = System.currentTimeMillis(); String resultStr = ""; HttpGet request = new HttpGet(urlPath + params); HttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, 10 * 1000); httpClient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10 * 1000); HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); resultStr = EntityUtils.toString(entity, HTTP.UTF_8); } long ET = System.currentTimeMillis(); return resultStr; } public String httpPostString(String urlPath, String params) throws IOException { long ST = System.currentTimeMillis(); String resultStr = ""; HttpPost request = new HttpPost(urlPath); HttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter("Content-Type", "application/json"); httpClient.getParams().setParameter("Charset", HTTP.UTF_8); httpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, 10 * 1000); // 超时设置 httpClient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10 * 1000);// 连接超时 HttpResponse response; if (params != null) { StringEntity reqEntity = new StringEntity(params); reqEntity.setContentType("application/json"); reqEntity.setContentEncoding("utf-8"); request.setEntity(reqEntity); } response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { resultStr = EntityUtils.toString(response.getEntity()); } long ET = System.currentTimeMillis(); return resultStr; } public String httpSendData(String urlPath, String params) throws IOException { long ST = System.currentTimeMillis(); StringBuffer sb = new StringBuffer(); BufferedReader br = null; try { URL url = new URL(urlPath); //打开连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置提交方式 conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); //post方式不能使用缓存 conn.setUseCaches(false); conn.setInstanceFollowRedirects(true); //设置连接超时时间 conn.setConnectTimeout(10 * 1000); //配置本次连接的Content-Type,配置为application/x-www-form-urlencoded conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //维持长连接 conn.setRequestProperty("Connection", "Keep-Alive"); //设置浏览器编码 conn.setRequestProperty("Charset", HTTP.UTF_8); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); //将请求参数数据向服务器端发送 dos.writeBytes(params); dos.flush(); dos.close(); if (conn.getResponseCode() == 200) { //获得服务器端输出流 br = new BufferedReader(new InputStreamReader(conn.getInputStream(), HTTP.UTF_8)); int c = 0; while ((c = br.read()) != -1) { sb.append((char) c); } } } catch (IOException e) { e.printStackTrace(); throw new IOException("系统错误"); } finally { try { if (br != null) br.close(); } catch (IOException e) { e.printStackTrace(); throw new IOException("系统错误"); } } long ET = System.currentTimeMillis(); return sb.toString(); } public String postFileParts(BaseActivity baseActivity, String param, List<String> sendPathList, String url) throws Exception { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setCharset(Charset.forName("utf-8")); for (int i = 0; i < sendPathList.size(); i++) { builder.addBinaryBody("fileData", new File(sendPathList.get(i))); } builder.addTextBody("", ""); HttpEntity entity = builder.build(); post.setEntity(entity); HttpResponse response = client.execute(post); if (response.getStatusLine().getStatusCode() == 200) { response = client.execute(post); } return ""; } }
Android Http传输数据
最新推荐文章于 2022-08-12 20:17:21 发布