网络连接工具类整理:

 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开发网络连接工具类(一)的更多相关文章

  1. Android开发常用工具类

    来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...

  2. 最全Android开发常用工具类

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括  HttpUtils.DownloadManagerPro.Safe.ijiami.ShellUtils.Pack ...

  3. Android开发中JavaBean类和序列化知识的理解

    原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/6296121.html Android开发中,我们经常用到JavaBean类以及序列化的知识,但经 ...

  4. Android开发 静态static类与static方法持有Context是否导致内存泄露的疑问

    简述 在Android开发的过程中,难免会使用单例模式或者静态方法工具类.我们会让它们持有一些外部的Context或者View一般有以下几种情况: 单例模式,类的全局变量持有Context 或 Vie ...

  5. 29个android开发常用的类、方法及接口

    在安卓开发中,我们常常都需要借助各种各样的方法.类和接口来实现相关功能.提升开发效率,但对于初学者而言,什么时候该用什么类.方法和接口呢?下面小编整理了29个,日常开发中比较常用的类.方法.接口及其应 ...

  6. android网络连接工具类

    该类的目的主要是判断设备的联网状态  检测设备是否连接了网络 package com.wotlab.home.moneyplantairs.utils; import android.content. ...

  7. 网络请求以及网络请求下载图片的工具类 android开发java工具类

    package cc.jiusan.www.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; ...

  8. android 开发 常用工具类

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38965311,本文出自[张鸿洋的博客] 打开大家手上的项目,基本都会有一大批的辅 ...

  9. android开发Tost工具类管理(一)

    Tost工具类管理: package com.gzcivil.utils; import android.content.Context; import android.widget.Toast; / ...

随机推荐

  1. c# 高斯模糊

    using System; using System.Collections.Generic; using System.Text; using System.Drawing; using Syste ...

  2. C++中如何建立一个顺序表

    准备数据 #define MAXLEN 100 //定义顺序表的最大长度 struct DATA { char key[10]; //结点的关键字 char name[20]; int age; }; ...

  3. Linux SSH 远程操作与传送文件

    操作系统:centos 6.5 x64 一.远程连接:在进行linux 的 ssh远程操作前,一定要确认linux 是否安装了 openssh-clients,为了方便起见,一般用yum安装即可:# ...

  4. 【二分图最大匹配】【HDU2063】过山车

    [科普]什么是BestCoder?如何参加? 过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  5. 多媒体封装格式----mkv

    Matroska 开源多媒体容器标准.MKV属于其中的一部分.Matroska常见的有.MKV视频格式.MKA音频格式..MKS字幕格式..MK3D files (stereoscopic/3D vi ...

  6. <audio>使用2

    1.属性测试 <!--显示控件--> <audio src="../images/wind.mp3" id="audioOne" contro ...

  7. django学习笔记一

    django作为一个python的开源项目发布,其web框架采用了mtv设计模式 在目前一些较为成熟的大型网站中有不少网站的应用基于django开发,django作为一个重量型的web框架提供了以下的 ...

  8. 【初级坑跳跳跳】[NULLException] findViewById() id 引用错误,导致空指针

    在学习Intent页面切换,几个页面切换,导致view id 写错,写成另一个xml里的id去了,导致空指针异常 setContentView(R.layout.activity_second); B ...

  9. 疯狂Android第一章:Android环境配置以及基本概念

    第一章 无关痒痛:Android Studio安装,配置,基本功能介绍! 重点内容:Android应用基本结构分析. 基础概念部分(只需知道作用,原理后见代码): Activity:安卓系统中负责与用 ...

  10. ref 关键字out关键字

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...