引用:http://guaicaifeiben.iteye.com/blog/825426

package baiduchuan.malata.store.net;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import baiduchuan.malata.store.utils.LogUtil;
import baiduchuan.malata.store.utils.StoreConstUtils;

public class HttpConManager
{
private static HttpConManager intance=null;
public static HttpConManager GetIntance()
{
if(intance==null)
intance=new HttpConManager();
return intance;
}

/**
* http数据通道
* @param httpurl
* @return
*/
private HttpURLConnection getHttpconnect(String httpurl)
{
HttpURLConnection con=null;
try
{
URL url =new URL(httpurl);
con=(HttpURLConnection)url.openConnection();
con.setDoOutput(true);//使用 URL 连接进行输出
con.setDoInput(true);//使用 URL 连接进行输入
con.setUseCaches(false);//忽略缓存
con.setRequestMethod("POST");//设置URL请求方法
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-type", "application/json");
con.connect();
con.setConnectTimeout(1);
con.setReadTimeout(1);
}
catch(IOException e1)
{
LogUtil.log("HttpConManager getJsonData IOE", 4, e1.getMessage());
}
catch(Exception e2)
{
LogUtil.log("HttpConManager getJsonData E", 4, e2.getMessage());
}
return con;
}

/**
* 客户端从服务端接收json格式数据
* @param httpurl
* @return
*/
public JSONObject getJsonData(String httpurl)
{
JSONObject jsonData=null;
try
{
HttpURLConnection con=getHttpconnect(httpurl);
int rescode=con.getResponseCode();
if(HttpURLConnection.HTTP_OK==rescode)
{
InputStream input=con.getInputStream();
int inputLength=input.available();
byte[] buffer=new byte[inputLength];
int offset=0;
int length=0;
ByteArrayOutputStream output=new ByteArrayOutputStream();
while((offset=input.read(buffer,0,inputLength))!=-1)
length +=offset;
output.write(buffer,0,length);
String str=new String(output.toByteArray());
jsonData=new JSONObject(str);
output.close();
input.close();
buffer=null;
con.disconnect();
}
}
catch(IOException e1)
{
LogUtil.log("HttpConManager getJsonData IOE", 4, e1.getMessage());
}
catch(JSONException e2)
{
LogUtil.log("HttpConManager getJsonData JSONE", 4, e2.getMessage());
}
catch(Exception e3)
{
LogUtil.log("HttpConManager getJsonData E", 4, e3.getMessage());
}
return jsonData;
}

/**
* 客户端从服务端接收String类型数据
* @param httpurl
* @return
*/
public String getStringData(String httpurl)
{
String value="";
try
{
HttpURLConnection con=getHttpconnect(httpurl);
int rescode=con.getResponseCode();
if(HttpURLConnection.HTTP_OK==rescode)
{
InputStream input=con.getInputStream();
int inputLength=input.available();
byte[] buffer=new byte[inputLength];
int offset=0;
int length=0;
ByteArrayOutputStream output=new ByteArrayOutputStream();
while((offset=input.read(buffer,0,inputLength))!=-1)
length +=offset;
output.write(buffer,0,length);
value=new String(output.toByteArray());
output.close();
input.close();
buffer=null;
con.disconnect();
}
}
catch(IOException e1)
{
LogUtil.log("HttpConManager getStringData IOE", 4, e1.getMessage());
}
catch(Exception e2)
{
LogUtil.log("HttpConManager getStringData E", 4, e2.getMessage());
}
return value;
}

/**
* 客户端发送数据给服务端
* @param httpurl
* @param strkey
* @param strValue StringBuffer类型
*/
public void sendPostData(String httpurl,String strkey,StringBuffer strValue)
{
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(httpurl);
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(strkey, strValue.toString()));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpClient.execute(httpPost);
}
catch (ClientProtocolException e)
{
e.printStackTrace();

} catch (IOException e)
{
e.printStackTrace();
}
}

/**
* 客户端发送数据给服务端
* @param httpurl
* @param strkey
* @param strValue 字符串
*/
public void sendPostData(String httpurl,String strkey,String strValue)
{
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(httpurl);
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(strkey, strValue));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpClient.execute(httpPost);
}
catch (ClientProtocolException e)
{
e.printStackTrace();

} catch (IOException e)
{
e.printStackTrace();
}
}

/***
* post通信方式并且返回JSON格式的数据
* @param httpUrl 访问的URL地址
* @param strKey 相当于&key=中的key
* @param strValue StringBuffer类型的参数
* @return
*/
public JSONObject sendPostDataAndResult(String httpUrl,String strKey,StringBuffer strValue)
{
JSONObject jsonObj=null;
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(httpUrl);
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(strKey, strValue.toString()));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse=httpClient.execute(httpPost);
HttpEntity httpEntity=httpResponse.getEntity();
String str=EntityUtils.toString(httpEntity);
try {
jsonObj=new JSONObject(str);
} catch (JSONException e) {
LogUtil.log("sendPostDataAndResult", 4, e.getMessage());
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();

} catch (IOException e)
{
e.printStackTrace();
}
return jsonObj;
}

/***
* post通信方式并且返回JSON格式的数据
* @param httpUrl 访问的URL地址
* @param strKey 相当于&key=中的key
* @param strValue String类型的参数
* @return
*/
public JSONObject sendPostDataAndResult(String httpUrl,String strKey,String strValue)
{
JSONObject jsonObj=null;
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(httpUrl);
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(strKey, strValue));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse=httpClient.execute(httpPost);
HttpEntity httpEntity=httpResponse.getEntity();
String str=EntityUtils.toString(httpEntity);
try {
jsonObj=new JSONObject(str);
} catch (JSONException e) {
LogUtil.log("sendPostDataAndResult", 4, e.getMessage());
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();

} catch (IOException e)
{
e.printStackTrace();
}
return jsonObj;
}


/**
* 检查网络是否可用
* @return
*/
public boolean checkNetwork()
{
HttpURLConnection con=getHttpconnect(StoreConstUtils.Store_Url_Domain);
try
{
int rescode=con.getResponseCode();
if(HttpURLConnection.HTTP_OK==rescode)
return true;
else
return false;
}
catch (IOException e)
{
LogUtil.log("checkNetwork", 4, e.getMessage());
}
return false;
}

}

更多相关文章

  1. Android实验七之SQLite数据库存储
  2. Android ContentProviders数据共享
  3. android inputreader 部分对event数据的处理
  4. Android sqlite3 数据库批量操作
  5. Android File 数据存储
  6. 游戏开发4_01 数据存储--io

随机推荐

  1. android笔试题
  2. Android核心模块内容概述
  3. Android(安卓)ContentProvider的使用和理
  4. Android横竖屏切换小结
  5. android布局layout中的一些属性
  6. Android开机log和常见异常的分析
  7. Android(安卓)Sensor详解(5)搭建adsp firmw
  8. ListView使用技巧(二):相关设置
  9. android:configChanges 横竖屏切换的生命
  10. Android事件处理第一节(View对Touch事件的