android开发网络连接工具类(一)
网络连接工具类整理:
package com.gzcivil.utils; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils; import android.content.Context; /**
* 网络连接类
*
* @author LiJinlun
*
*/
public class NetUtils { /**
* 获取Json数据
*
* @param param
* @return
*/ public static String post(Map<String, String> paramMap, String URL, Context context) { String result = null;
HttpPost httpRequest = new HttpPost(URL);
List<NameValuePair> params = new ArrayList<NameValuePair>(); paramMap.put("machineCode", CommonUtil.GetMachineCode(context));
paramMap.put("cid", CommonUtil.GetCid(context).toString()); for (Map.Entry<String, String> param : paramMap.entrySet()) {
params.add(new BasicNameValuePair(param.getKey(), param.getValue()));
}
// 打印去包日志
LogUtils.d(SysUtils.LOG_TAG, "去包:" + URL + params.toString());
HttpEntity httpEntity;
HttpClient httpClient = null;
try {
httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
httpRequest.setEntity(httpEntity);
httpClient = getHttpClient();
HttpResponse httpResponse = httpClient.execute(httpRequest); int status = httpResponse.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK) {
result = EntityUtils.toString(httpResponse.getEntity());
// 打印回包日志
LogUtils.d(SysUtils.LOG_TAG, "回包:" + result.toString());
return result;
}
} catch (IOException e) {
e.printStackTrace();
return SysUtils.errorcode.ERROR_TIMEOUT + "";
} finally {
if (httpClient != null)
httpClient.getConnectionManager().shutdown();
}
return null;
} /**
* get获取数据
*
* @param url
* @return
*/
public static String get(String url) {
// 打印去包日志
LogUtils.d(SysUtils.LOG_TAG, "去包:" + url.toString());
String result = null;
HttpGet get = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);
int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity(), "UTF-8");
// 打印回包日志
LogUtils.d(SysUtils.LOG_TAG, "回包:" + result.toString());
return result;
}
} catch (IOException e) {
e.printStackTrace();
return SysUtils.errorcode.ERROR_TIMEOUT + "";
} finally {
client.getConnectionManager().shutdown();
}
return null;
} public static HttpClient getHttpClient() {
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 8 * 1000);
HttpConnectionParams.setSoTimeout(httpParams, 8 * 1000);
// HttpClientParams.setRedirecting(httpParams, true);
HttpClient client = new DefaultHttpClient(httpParams);
// client.getParams().setIntParameter( HttpConnectionParams.SO_TIMEOUT,
// 8*1000); // 超时设置
// client.getParams().setIntParameter(
// HttpConnectionParams.CONNECTION_TIMEOUT, 8*1000);// 连接超时
return client;
} }
android开发网络连接工具类(一)的更多相关文章
- Android开发常用工具类
来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...
- 最全Android开发常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括 HttpUtils.DownloadManagerPro.Safe.ijiami.ShellUtils.Pack ...
- Android开发中JavaBean类和序列化知识的理解
原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/6296121.html Android开发中,我们经常用到JavaBean类以及序列化的知识,但经 ...
- Android开发 静态static类与static方法持有Context是否导致内存泄露的疑问
简述 在Android开发的过程中,难免会使用单例模式或者静态方法工具类.我们会让它们持有一些外部的Context或者View一般有以下几种情况: 单例模式,类的全局变量持有Context 或 Vie ...
- 29个android开发常用的类、方法及接口
在安卓开发中,我们常常都需要借助各种各样的方法.类和接口来实现相关功能.提升开发效率,但对于初学者而言,什么时候该用什么类.方法和接口呢?下面小编整理了29个,日常开发中比较常用的类.方法.接口及其应 ...
- android网络连接工具类
该类的目的主要是判断设备的联网状态 检测设备是否连接了网络 package com.wotlab.home.moneyplantairs.utils; import android.content. ...
- 网络请求以及网络请求下载图片的工具类 android开发java工具类
package cc.jiusan.www.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; ...
- android 开发 常用工具类
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38965311,本文出自[张鸿洋的博客] 打开大家手上的项目,基本都会有一大批的辅 ...
- android开发Tost工具类管理(一)
Tost工具类管理: package com.gzcivil.utils; import android.content.Context; import android.widget.Toast; / ...
随机推荐
- 如何统一删除word中的超链接
[摘要] 我们从别处拷贝文字,或从网上复制的文字,里面有很多超级链接,如何可以批量删除这些链接呢?这里介绍两种批量删除链接的方法. [正文] 方法一:使用快捷键删除超链接 有个神奇的快捷键,可以帮我们 ...
- 64bit ubuntu14.04编译PlatinumKit出现的arm-linux-androideabi-g++: not found错误解决方法
编译命令:scons target=arm-android-linux build_config=Release 出现错误: scons: Reading SConscript files ...** ...
- Android设置背景
一.设置图片背景 首先你先将一个的背景图片存入工程中res/drawble(当然drawble-hdpi.drawble-mdpi.drawble-ldpi中一个或者几个文件夹都可)文件夹中.假如我存 ...
- nodejs实现接收Snmp的Trap消息
var assert = require('assert'); var ASN1 = { EOC: 0, Boolean: 1, Integer: 2, BitString: 3, OctetStri ...
- logfile提示stale错误解决方法
产生该错误的原因解释如下: Explanation: ============ A stale redo log file is one that Oracle believes might be i ...
- 配置nginx静态资源路径
1.修改nginx.conf文件 server节点下location节点的 root目录修改 location / { root html; index index.html index.htm; } ...
- shopnc 商家中心添加打印商品二维码功能
需求中提到需要增加每一件商品可以打印,用于线下体验店实体商品的二维码标签,客人可以根据手机扫二维码功能进行购买 任务描述: 1.如附件实现”批量打印标签“和单个商品”打印“标签功能. 2.标签有两种” ...
- [汇编语言]-第九章 jcxz,loop指令,转移位移的意义
1- jcxz指令 指令为有条件转移指令, 所有的有条件转移指令都是短转移, 在对应的机器码中包含转移的位移, 而不是目的地址, 对IP的修改范围为: -128 ~ 127 指令格式: jcxz 标号 ...
- Fire Net--hdu1045
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- first blood暴力搜索,剪枝是关键
First Blood 题目描述 盖伦是个小学一年级的学生,在一次数学课的时候,老师给他们出了一个难题: 老师给了一个正整数 n,需要在不大于n的范围内选择三个正整数(可以是相同的),使它们三个的最小 ...