1. import java.io.ByteArrayOutputStream;
  2. import java.io.InputStream;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;
  5. import java.util.List;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.NameValuePair;
  9. import org.apache.http.client.HttpClient;
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;
  11. import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.entity.mime.MultipartEntity;
  14. import org.apache.http.impl.client.DefaultHttpClient;
  15. import org.apache.http.protocol.HTTP;
  16. import org.apache.http.util.EntityUtils;
  17.  
  18. /**
  19. * Connect to the internet
  20. *
  21. * @author crane
  22. *
  23. */
  24. public class HttpTools {
  25. /**
  26. * Post data to server
  27. *
  28. * @param sUrl
  29. * @param entity
  30. * @return
  31. */
  32. public static String postData(String sUrl, MultipartEntity entity) {
  33. String destUrl = "";
  34. destUrl = sUrl;
  35. String sResult = "";
  36. // instantiate HttpPost object from the url address
  37. HttpEntityEnclosingRequestBase httpRequest = new HttpPost(destUrl);
  38. try {
  39. httpRequest.setEntity(entity);
  40. // execute the post and get the response from servers
  41. HttpResponse httpResponse = new DefaultHttpClient()
  42. .execute(httpRequest);
  43. int code = httpResponse.getStatusLine().getStatusCode();
  44. if (httpResponse.getStatusLine().getStatusCode() == 200) {
  45. // get the result
  46. String strResult = EntityUtils.toString(httpResponse
  47. .getEntity());
  48. sResult = strResult;
  49. System.out.println(strResult);
  50. } else {
  51. System.out.println("Error Response"
  52. + httpResponse.getStatusLine().toString());
  53. }
  54. } catch (Exception e) {
  55. System.out.println("error occurs");
  56. }
  57. return sResult;
  58. }
  59.  
  60. /**
  61. * Get data from server
  62. *
  63. * @param urlPath
  64. * @return
  65. */
  66. public static String getData(String sUrl) {
  67. String urlPath = sUrl;
  68. ByteArrayOutputStream outStream = null;
  69. String sResult = "";
  70. try {
  71. outStream = new ByteArrayOutputStream();
  72. byte[] data = new byte[1024];
  73. int len = 0;
  74. URL url = new URL(urlPath);
  75. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  76. InputStream inStream = conn.getInputStream();
  77. while ((len = inStream.read(data)) != -1) {
  78. outStream.write(data, 0, len);
  79. }
  80. inStream.close();
  81. sResult = new String(outStream.toByteArray()).trim();
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85.  
  86. return sResult;
  87. }
  88.  
  89. public static String post(String sUrl, List<NameValuePair> parameters) {
  90. HttpClient httpClient = new DefaultHttpClient();
  91. HttpPost httpPost = new HttpPost(sUrl);
  92. String sResult = "";
  93.  
  94. try {
  95. HttpEntity entity = new UrlEncodedFormEntity(parameters, HTTP.UTF_8); // 设置编码,防止中文乱码
  96. httpPost.setEntity(entity);
  97. // httpClient执行httpPost表单提交
  98. HttpResponse response = httpClient.execute(httpPost);
  99. // 得到服务器响应实体对象
  100. HttpEntity responseEntity = response.getEntity();
  101. if (responseEntity != null) {
  102. sResult = EntityUtils.toString(responseEntity, "utf-8");
  103. System.out.println("表单上传成功!");
  104. } else {
  105. System.out.println("服务器无响应!");
  106. }
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. } finally {
  110. // 释放资源
  111. httpClient.getConnectionManager().shutdown();
  112. }
  113. return sResult.replace(",", ",");
  114. }
  115.  
  116. }
  117.  
  118. 所需jar包:httpmime-4.1.3.jar

Android—网络请求的更多相关文章

  1. Android网络请求框架AsyncHttpClient实例详解(配合JSON解析调用接口)

    最近做项目要求使用到网络,想来想去选择了AsyncHttpClient框架开进行APP开发.在这里把我工作期间遇到的问题以及对AsyncHttpClient的使用经验做出相应总结,希望能对您的学习有所 ...

  2. xamarin android网络请求总结

    xamarin android中网络请求的框架非常多,在项目中使用的是第三方的一个网络请求框架restsharp,应该是github上.net网络请求最多star的框架,没有之一.这里就简单汇总了其他 ...

  3. Android 网络请求框架Retrofit

    Retrofit是Square公司开发的一款针对Android网络请求的框架,Retrofit2底层基于OkHttp实现的,OkHttp现在已经得到Google官方认可,大量的app都采用OkHttp ...

  4. Android 网络请求及数据处理

    Android 网络请求: 1.Volley   http://blog.csdn.net/t12x3456/article/details/9221611 2.Android-Async-Http  ...

  5. Android 网络请求Retrofit + RxJava

    一.背景 经常看到项目用Retrofit+RxJava+RxAndroid的框架,为了看懂项目的结构.现在来了解一下,Retrofit: Retrofit是Square 公司开发的一款正对Androi ...

  6. android 网络请求Ⅰ

    本章讲述在android开发中,常用的网络请求操作.网络请求利用android基本的HttpURLConnection连接URL和开源网络请求包AsyncHttpClient.本次网络请求以调取天气接 ...

  7. Android网络请求通信之Volley

    一.Volley简介 Volley网络框架是Google公司在2013年发布的一款Android平台上的网络请求通信库.以下是对Volley的简单归纳. Volley的优点: 使网络通信更快.更简单. ...

  8. android 网络请求库的比较

    源码请戳 一. 现有库和选择的库 HttpURLConnection:是Java中的标准类,是对Java中socket的封装. Httpclient:是Apache的开源框架,是对HttpURLCon ...

  9. 浅论Android网络请求库——android-async-http

    在iOS开发中有大名鼎鼎的ASIHttpRequest库,用来处理网络请求操作,今天要介绍的是一个在Android上同样强大的网络请求库android-async-http,目前非常火的应用Insta ...

  10. Android网络请求框架

    本篇主要介绍一下Android中经常用到的网络请求框架: 客户端网络请求,就是客户端发起网络请求,经过网络框架的特殊处理,让后将请求发送的服务器,服务器根据 请求的参数,返回客户端需要的数据,经过网络 ...

随机推荐

  1. 在Eclipse IDE进行Struts开发时提示错误:java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.FilterDispatcher的解决办法

    If you have... included all necessary jars Configured build path correctly added them all in deploym ...

  2. cv2.warpAffine 参数详解

    本文链接:https://blog.csdn.net/qq878594585/article/details/81838260本文为作者原创文章,未经同意严禁转载! opencv中的仿射变换在pyth ...

  3. Java 读取clob字段的几种方法

    Java 读取clob字段的几种方法 一.第一种 Clob clob = rs.getClob("remark");//Java.sql.Clob String detailinf ...

  4. [转]IE、FireFox、Chrome浏览器中关于URL传参中文乱码,解决兼容性问题!

    原文地址:https://cloud.tencent.com/developer/article/1334736 前台用url传值中文,后台用request.getParameter接收参数.在Fir ...

  5. centos下安装ffmpeg加上fdk-aac的支持

    本文参考自:https://blog.csdn.net/jklinux/article/details/72367829 安装包可以从这里下载https://download.csdn.net/dow ...

  6. shell编程系列1--shell脚本中的变量替换

    shell编程系列1--shell脚本中的变量替换 变量替换总结: .${变量#匹配规则} # 从头开始匹配,最短删除 .${变量##匹配规则} # 从头开始匹配,最长删除(贪婪模式) .${变量%匹 ...

  7. 008-SpringBoot发布WAR启动报错:Error assembling WAR: webxml attribute is required

    一.Spring Boot发布war包流程: 1.修改web model的pom.xml <packaging>war</packaging> SpringBoot默认发布的都 ...

  8. SeetaFaceDetection识别人脸

    SeetaFaceDetection识别人脸 #pragma warning(disable: 4819) #include <seeta/FaceEngine.h> #include & ...

  9. NSGA,NSGA-II,Epsilon-MOEA,DE C语言Deb教授原版代码

    NSGA,NSGA-II,Epsilon-MOEA,Basic Differential Evolution (DE) C语言Deb教授原版代码地址 觉得有用的话,欢迎一起讨论相互学习~[Follow ...

  10. Sq常用操作

    sql创建表实例: CREATE TABLE mytable( id varchar(40) NOT NULL default '', userId varchar(40) NOT NULL defa ...