先建立一个class ,我命名为myapp,进行连接,传递参数获取数据
public class MyApp extends Application
{/*全局变量,所有界面共用*/
protected static String requestURL = "http://192.168.10.108:8080/OnuMonitor/FileUploadServlet";
private static String serverAddress = "http://192.168.10.108:8080/OnuMonitor/DataCommunicate";
private static String cStatus;//收藏状态
private static String oStatus;//开门权限状态
public static String getcStatus() {
return cStatus;
}
public static void setcStatus(String cStatus) {
MyApp.cStatus = cStatus;
}
public static String getoStatus() {
return oStatus;
}
public static void setoStatus(String oStatus) {
MyApp.oStatus = oStatus;
}
public static String getHostMAC() {
return hostMAC;
}
public static void setHostMAC(String hostMAC) {
MyApp.hostMAC = hostMAC;
}
private static String phoneNum;
private static String hostMAC;
private static boolean occupyed;
/*返回occupyed*/
public static boolean isOccupyed()
{
return occupyed;
}
/*设置电话号*/
public static void setPhoneNum(String pnum)
{
if((pnum != null)&&(!pnum.equals("")))
{
phoneNum = pnum;
}
}
/*返回电话号*/
public static String getPhoneNum() {
return phoneNum;
}
/*发送post请求*/
public static String executePost(List<NameValuePair> params)
{
/*httpclient被占用*/
occupyed = true;
/*初始化服务器返回的数据字符串*/
String responseMsg = "";
/*新建httpclient*/
HttpClient httpclient = new DefaultHttpClient();
/*连接超时*/
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000*60);
/*读取超时*/
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000*60);
try
{
/*新建post请求*/
HttpPost post = new HttpPost(serverAddress);
/*设置请求参数及编码方式*/
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
/*发送请求并读取服务器返回数据*/
HttpResponse response = httpclient.execute(post);
/*读取服务器返回的数据,并组成字符串*/
try
{
HttpEntity entity = response.getEntity();
if(entity != null)
{
InputStream is = entity.getContent();
try
{
InputStreamReader ir = new InputStreamReader(is);
try
{
BufferedReader br = new BufferedReader(ir);
try
{
/*读取服务器返回的数据并组成字符串*/
String str = null;
while((str = br.readLine()) != null)
{
responseMsg = responseMsg.concat(str);
}
}catch(Exception e)
{
responseMsg = null;
e.printStackTrace();
}
/*关闭缓冲阅读器*/
finally
{
br.close();
}
}catch(Exception e)
{
responseMsg = null;
e.printStackTrace();
}
/*关闭输入流阅读器*/
finally
{
ir.close();
}
}catch(Exception e)
{
responseMsg = null;
e.printStackTrace();
}
/*关闭输入流*/
finally
{
is.close();
entity.consumeContent();
}
}
/*没有收到服务器返回的数据*/
else
{
responseMsg = null;
}
}catch(Exception e)
{
responseMsg = null;
e.printStackTrace();
}
}catch(Exception e)
{
responseMsg = null;
e.printStackTrace();
}
/*httpclient解除占用*/
occupyed = false;
/*返回字符串*/
return responseMsg;
}
}
调用时候的代码:
/* 向服务器请求基站信息 */
private void sendRequest() {
/* 显示进度指示器 */
String msg = "数据加载中...";
showProgressDialog(msg);
/* 新建线程向服务器请求基站数据 */
new Thread() {
public void run() {
try {
/* 设置请求参数 */
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("requestType", "93"));
params.add(new BasicNameValuePair("onuid", onuId));
/* 发送请求并读取服务器返回的数据 */
while (MyApp.isOccupyed()) {
;
}
String responseMsg = MyApp.executePost(params);
/* 数据处理 */
if (responseMsg == null) {
onuInfoMap = null;
} else {
/* 基站数据不为空 */
if (!(responseMsg.equals("{}"))) {
onuInfoMap = parseJSON2Map(responseMsg);
}
/* 基站数据为空 */
else {
onuInfoMap = null;
}
}
}
/* 请求数据失败 */
catch (Exception e) {
onuInfoMap = null;
/* 关闭进度指示器 */
dissmissProgressDialog();
/* 弹出信息提示框 */
Message message = new Message();
message.what = 0x006;
Bundle bundle = new Bundle();
bundle.putString("message", "网络不给力啊...");
message.setData(bundle);
toastHandler.sendMessage(message);
e.printStackTrace();
}
finally {
/* 初始化界面 */
Message message = new Message();
message.what = 0x006;
initHandler.sendMessage(message);
/* 关闭进度指示器 */
dissmissProgressDialog();
}
}
}.start();
}
/* 数据解析器 */
public Map<String, Object> parseJSON2Map(String jsonStr) {
Map<String, Object> guanBasicInfoMap = new HashMap<String, Object>();
JSONObject jsonObj = JSONObject.fromObject(jsonStr);
for (Object key : jsonObj.keySet()) {
Object value = jsonObj.get(key);
guanBasicInfoMap.put((String) key, value);
}
return guanBasicInfoMap;
}