Android—网络请求
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.List;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.mime.MultipartEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.protocol.HTTP;
- import org.apache.http.util.EntityUtils;
- /**
- * Connect to the internet
- *
- * @author crane
- *
- */
- public class HttpTools {
- /**
- * Post data to server
- *
- * @param sUrl
- * @param entity
- * @return
- */
- public static String postData(String sUrl, MultipartEntity entity) {
- String destUrl = "";
- destUrl = sUrl;
- String sResult = "";
- // instantiate HttpPost object from the url address
- HttpEntityEnclosingRequestBase httpRequest = new HttpPost(destUrl);
- try {
- httpRequest.setEntity(entity);
- // execute the post and get the response from servers
- HttpResponse httpResponse = new DefaultHttpClient()
- .execute(httpRequest);
- int code = httpResponse.getStatusLine().getStatusCode();
- if (httpResponse.getStatusLine().getStatusCode() == 200) {
- // get the result
- String strResult = EntityUtils.toString(httpResponse
- .getEntity());
- sResult = strResult;
- System.out.println(strResult);
- } else {
- System.out.println("Error Response"
- + httpResponse.getStatusLine().toString());
- }
- } catch (Exception e) {
- System.out.println("error occurs");
- }
- return sResult;
- }
- /**
- * Get data from server
- *
- * @param urlPath
- * @return
- */
- public static String getData(String sUrl) {
- String urlPath = sUrl;
- ByteArrayOutputStream outStream = null;
- String sResult = "";
- try {
- outStream = new ByteArrayOutputStream();
- byte[] data = new byte[1024];
- int len = 0;
- URL url = new URL(urlPath);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- InputStream inStream = conn.getInputStream();
- while ((len = inStream.read(data)) != -1) {
- outStream.write(data, 0, len);
- }
- inStream.close();
- sResult = new String(outStream.toByteArray()).trim();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return sResult;
- }
- public static String post(String sUrl, List<NameValuePair> parameters) {
- HttpClient httpClient = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost(sUrl);
- String sResult = "";
- try {
- HttpEntity entity = new UrlEncodedFormEntity(parameters, HTTP.UTF_8); // 设置编码,防止中文乱码
- httpPost.setEntity(entity);
- // httpClient执行httpPost表单提交
- HttpResponse response = httpClient.execute(httpPost);
- // 得到服务器响应实体对象
- HttpEntity responseEntity = response.getEntity();
- if (responseEntity != null) {
- sResult = EntityUtils.toString(responseEntity, "utf-8");
- System.out.println("表单上传成功!");
- } else {
- System.out.println("服务器无响应!");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- // 释放资源
- httpClient.getConnectionManager().shutdown();
- }
- return sResult.replace(",", ",");
- }
- }
- 所需jar包:httpmime-4.1.3.jar
Android—网络请求的更多相关文章
- Android网络请求框架AsyncHttpClient实例详解(配合JSON解析调用接口)
最近做项目要求使用到网络,想来想去选择了AsyncHttpClient框架开进行APP开发.在这里把我工作期间遇到的问题以及对AsyncHttpClient的使用经验做出相应总结,希望能对您的学习有所 ...
- xamarin android网络请求总结
xamarin android中网络请求的框架非常多,在项目中使用的是第三方的一个网络请求框架restsharp,应该是github上.net网络请求最多star的框架,没有之一.这里就简单汇总了其他 ...
- Android 网络请求框架Retrofit
Retrofit是Square公司开发的一款针对Android网络请求的框架,Retrofit2底层基于OkHttp实现的,OkHttp现在已经得到Google官方认可,大量的app都采用OkHttp ...
- Android 网络请求及数据处理
Android 网络请求: 1.Volley http://blog.csdn.net/t12x3456/article/details/9221611 2.Android-Async-Http ...
- Android 网络请求Retrofit + RxJava
一.背景 经常看到项目用Retrofit+RxJava+RxAndroid的框架,为了看懂项目的结构.现在来了解一下,Retrofit: Retrofit是Square 公司开发的一款正对Androi ...
- android 网络请求Ⅰ
本章讲述在android开发中,常用的网络请求操作.网络请求利用android基本的HttpURLConnection连接URL和开源网络请求包AsyncHttpClient.本次网络请求以调取天气接 ...
- Android网络请求通信之Volley
一.Volley简介 Volley网络框架是Google公司在2013年发布的一款Android平台上的网络请求通信库.以下是对Volley的简单归纳. Volley的优点: 使网络通信更快.更简单. ...
- android 网络请求库的比较
源码请戳 一. 现有库和选择的库 HttpURLConnection:是Java中的标准类,是对Java中socket的封装. Httpclient:是Apache的开源框架,是对HttpURLCon ...
- 浅论Android网络请求库——android-async-http
在iOS开发中有大名鼎鼎的ASIHttpRequest库,用来处理网络请求操作,今天要介绍的是一个在Android上同样强大的网络请求库android-async-http,目前非常火的应用Insta ...
- Android网络请求框架
本篇主要介绍一下Android中经常用到的网络请求框架: 客户端网络请求,就是客户端发起网络请求,经过网络框架的特殊处理,让后将请求发送的服务器,服务器根据 请求的参数,返回客户端需要的数据,经过网络 ...
随机推荐
- 在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 ...
- cv2.warpAffine 参数详解
本文链接:https://blog.csdn.net/qq878594585/article/details/81838260本文为作者原创文章,未经同意严禁转载! opencv中的仿射变换在pyth ...
- Java 读取clob字段的几种方法
Java 读取clob字段的几种方法 一.第一种 Clob clob = rs.getClob("remark");//Java.sql.Clob String detailinf ...
- [转]IE、FireFox、Chrome浏览器中关于URL传参中文乱码,解决兼容性问题!
原文地址:https://cloud.tencent.com/developer/article/1334736 前台用url传值中文,后台用request.getParameter接收参数.在Fir ...
- centos下安装ffmpeg加上fdk-aac的支持
本文参考自:https://blog.csdn.net/jklinux/article/details/72367829 安装包可以从这里下载https://download.csdn.net/dow ...
- shell编程系列1--shell脚本中的变量替换
shell编程系列1--shell脚本中的变量替换 变量替换总结: .${变量#匹配规则} # 从头开始匹配,最短删除 .${变量##匹配规则} # 从头开始匹配,最长删除(贪婪模式) .${变量%匹 ...
- 008-SpringBoot发布WAR启动报错:Error assembling WAR: webxml attribute is required
一.Spring Boot发布war包流程: 1.修改web model的pom.xml <packaging>war</packaging> SpringBoot默认发布的都 ...
- SeetaFaceDetection识别人脸
SeetaFaceDetection识别人脸 #pragma warning(disable: 4819) #include <seeta/FaceEngine.h> #include & ...
- NSGA,NSGA-II,Epsilon-MOEA,DE C语言Deb教授原版代码
NSGA,NSGA-II,Epsilon-MOEA,Basic Differential Evolution (DE) C语言Deb教授原版代码地址 觉得有用的话,欢迎一起讨论相互学习~[Follow ...
- Sq常用操作
sql创建表实例: CREATE TABLE mytable( id varchar(40) NOT NULL default '', userId varchar(40) NOT NULL defa ...