天天看點

通信--------android 用戶端 與 web 伺服器通信

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 

 public JSONObject getJsonData(String httpurl)

  JSONObject jsonData=null;

   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(JSONException e2)

   LogUtil.log("HttpConManager getJsonData JSONE", 4, e2.getMessage());

  catch(Exception e3)

   LogUtil.log("HttpConManager getJsonData E", 4, e3.getMessage());

  return jsonData;

  * 用戶端從服務端接收String類型資料

 public String getStringData(String httpurl)

  String value="";

    value=new String(output.toByteArray());

   LogUtil.log("HttpConManager getStringData IOE", 4, e1.getMessage());

   LogUtil.log("HttpConManager getStringData E", 4, e2.getMessage());

  return value;

  * 用戶端發送資料給服務端

  * @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 strValue 字元串

 public void sendPostData(String httpurl,String strkey,String strValue)

   nameValuePairs.add(new BasicNameValuePair(strkey, strValue)); 

 /***

  * post通信方式并且傳回JSON格式的資料

  * @param httpUrl 通路的URL位址

  * @param strKey 相當于&key=中的key

  * @param strValue StringBuffer類型的參數

 public JSONObject sendPostDataAndResult(String httpUrl,String strKey,StringBuffer strValue)

  JSONObject jsonObj=null;

  HttpPost httpPost=new HttpPost(httpUrl);

   nameValuePairs.add(new BasicNameValuePair(strKey, strValue.toString())); 

   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());

   } 

  return jsonObj;

  * @param strValue String類型的參數

 public JSONObject sendPostDataAndResult(String httpUrl,String strKey,String strValue)

   nameValuePairs.add(new BasicNameValuePair(strKey, strValue)); 

  * 檢查網絡是否可用

 public boolean checkNetwork()

  HttpURLConnection con=getHttpconnect(StoreConstUtils.Store_Url_Domain);

    return true;

   else

    return false;

  catch (IOException e) 

   LogUtil.log("checkNetwork", 4, e.getMessage());

  return false;

}