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; / ...
随机推荐
- Android应用程序组件Content Provider应用实例
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6950440 文简要介绍了Android应用程序 ...
- Ubuntu下嵌入式Qt开发环境配置全攻略
http://qpcwth.blog.163.com/blog/static/20993024620139151424822/ 在安装的过称中,出现一些问题,注意试想: 1.本次开发环境的配置,是基于 ...
- java中拼接两个数组
int a[]={1,2,3,2}; int b[]={4,2,90,8,98}; int[] d3 = new int[a.length + b.length]; System.arraycopy( ...
- Spring 入门 Ioc-Annotation
通过对XML版本进行修改:http://www.cnblogs.com/likailan/p/3446821.html 一.导入 Spring 所需要的包 spring-framework-2.5.6 ...
- 安装Cocoapoads遇到的问题的解决办法
错误一.首先来个错误截图
- 运行加速多线程和GPU
http://blog.csdn.net/augusdi/article/details/11883003
- mysql用root用户启动后其他用户无法启动不问题
问题描述:用root账户启动mysql后,在用mysql用户或其他非root账户启动不了mysql问题解决:通过看mysql的err日志,发现 Failed to open log (robert-b ...
- Android TextView 字符串展示不同大小文字
用Spannable字符串实现: String s= "Hello Everyone"; SpannableString ss1= new SpannableString(s); ...
- CS0016: 未能写入输出文件*****目录名称无效
一大早,杀毒软件弹出删除隐私记录.清理空间一堆堆的提醒,一般我都是无视它,今天顺便点了下清理,然后出问题. 昨晚下班提交的代码,程序运行好好地,今早清理完系统,竟然就出问题了, 具体如下 CS0016 ...
- 计算两点间的距离-hdu2001
Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离. Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2 ...