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;
}
}
分享到:
相关推荐
一个Android消息推送系统,包括Android客户端和服务端的实现。客户端支持长连接和短连接方式,长连接采用MQTT协议,短连接采用HTTP协议。服务端整合到web项中目中,有一个简单的界面进行操作处理。 软件开发设计:...
掌握开发Android客户端界面; 掌握使用HttpClient发送请求、使用HttpClient获取服务器响应; 掌握将服务器响应装换为JSON对象或数组、通过Android客户端加载服务器响应; 开发java web project作为服务端; 开发...
该项目主要用户注册功能、用户登录功能、查找添加好友功能、聊天功能,后台运行,退出登录功能,后台管理。 开发技术:Android 集成开发工具:Eclipse 运行平台:Android
此源码包含AppClient与AppServer,是由安卓客户端与服务端交互源代码(包括安卓端和web端),即下载即使用,在eclipse中打开即可运行使用。
本例演示最简单的android客户端和服务器端通信,客户端使用android4.0以上版本,服务器端为dynamic web project工程,使用时将client代码中的ip地址改为自己电脑的ipv4地址,同时保证手机和电脑处于同一网络下(内网...
服务器和客户端数据通信格式: XML格式(用于传输查询的记录集)和json格式(用于传输单个的对象信息) 系统主要有管理员管理、用户管理两个功能模块。管理员管理主要包括用户管理,车辆类型管理管理管理、车辆管理管理...
从上文可以看出,传统 Web 模式在处理高并发及实时性需求的时候,会遇到难以逾越的瓶颈,我们需要一种高效节能的双向通信机制来保证数据的实时传输。在此背景下,基于 HTML5 规范的、有 Web TCP 之称的 WebSocket ...
发现网上的很多例子都不能正常运行,...服务端用Cxf建立WebService,android客户端用ksoap2-android与服务端的webservice取得通信。本项目所有的jar包都已经打包,大家不用下载,仅仅要改的是IP,然后就可以直接运行了
此资源实现安卓APP注册登录功能,通过Android Studio编写界面和相应功能,用Eclipse开启web服务器进行客户端与服务端之间的通信,并且将用户的账户密码经过MD5的加密后存入Mysql数据库中。
1.2.4 Android移动Web项目开发的三种解决方案:Native, Web和Hybrid优缺陷分析 4 1.2.5国内外应用现状 6 1.2.6 研究现状总结 7 1.3研究目标与内容 7 1.3.1多窗口浏览器模式的实现机制 7 1.3.2跨域交互即缓存处理方法...
2.3.2 Web服务端技术论证 6 2.3.3 数据库论证 7 3 系统总体设计 8 3.1架构设计 8 3.2 系统功能设计 8 3.2.1手机客户端功能设计: 10 3.2.2 Web服务端功能设计: 11 3.3 数据库设计 13 3.3.1 系统E-R图 13 3.3.2 ...
Android客户端 Android客户端项目,仅支持一对一音视频 Android专业版,支持分组音视频 Vue Web客户端基于vue的web端项目基于vue的web端项目 Vue移动端基于vue的移动端项目 Electron-Vue客户端基于electron的pc端...
不过作为Web开发,实时同时,客户端和服务端双工通信这也是非常重要的需求。无论是传统Web网站还是移动平台,这个框架应该都会对我们的开发工作带来很大便利。SignalR Hub AuthorizationSecuring SignalR to your ...
Webkit 引擎的集成,可以实现本地界面与Web内容的无缝集成, 但是真正使得 Qt 在自由软件界的众多 Widgets (如 Lesstif,Gtk,EZWGL,Xforms,fltk 等等)中脱颖而出的还是基于 Qt 的重量级软件 KDE。 信号和槽机制...
Webkit 引擎的集成,可以实现本地界面与Web内容的无缝集成, 但是真正使得 Qt 在自由软件界的众多 Widgets (如 Lesstif,Gtk,EZWGL,Xforms,fltk 等等)中脱颖而出的还是基于 Qt 的重量级软件 KDE。 信号和槽机制...
服务器代码可在web项目中使用,客户端代码可以在Android开发时使用,本例子中没有将登录信息保存到IOSession中,在实际开发中,可以将登录信息保存到服务器端的iossion对象中,当前,也可以将其他会话有关的信息保存...
Webkit 引擎的集成,可以实现本地界面与Web内容的无缝集成, 但是真正使得 Qt 在自由软件界的众多 Widgets (如 Lesstif,Gtk,EZWGL,Xforms,fltk 等等)中脱颖而出的还是基于 Qt 的重量级软件 KDE。 信号和槽机制...
Webkit 引擎的集成,可以实现本地界面与Web内容的无缝集成, 但是真正使得 Qt 在自由软件界的众多 Widgets (如 Lesstif,Gtk,EZWGL,Xforms,fltk 等等)中脱颖而出的还是基于 Qt 的重量级软件 KDE。 信号和槽机制...
## 服务器和客户端数据通信格式: XML格式(用于传输查询的记录集)和json格式(用于传输单个的对象信息) 系统主要有管理员管理、用户管理两个功能模块。管理员管理主要包括用户管理,车辆类型管理管理管理、车辆管理...