9月份总结II

来源:互联网 发布:淘宝首页焦点图 编辑:程序博客网 时间:2024/05/01 08:35

对URL中的参数进行UTF-8编码

public static String UTFCode(String value) {
  String resultValue = null;

  try {
   resultValue = URLEncoder.encode(value, "UTF-8");
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  
  return resultValue;
 }

 

网络连接:wifi,cmwap,cmnet都适用,用Java的标准接口,get方法

 

//获取手机的网络类型wifi、mobile
 private int getNetTyle() {
  int Type = NONET;
  
  ConnectivityManager cm = (ConnectivityManager) context
    .getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo info = cm.getActiveNetworkInfo();
  if (info != null) {
   String typeName = info.getTypeName(); // wifi,mobile
   if (!typeName.equalsIgnoreCase("wifi")) {
    Type = getCurrentApnInUse();
   } else {
    Type = WIFIAndCMNET;
    
   }
  }
  
  return Type;
 }

//获取Mobile网络下的cmwap、cmnet
 public int getCurrentApnInUse() {
  int type = NONET;
  
  Cursor cursor = context.getContentResolver().query(PREFERRED_APN_URI,
    new String[] { "_id", "apn", "type" }, null, null, null);
  cursor.moveToFirst();
  if (!cursor.isAfterLast()) {
   String apn = cursor.getString(1);
   if (apn.equalsIgnoreCase("cmnet")) {
    type = WIFIAndCMNET;
   } else if (apn.equalsIgnoreCase("cmwap")) {
    type = CMWAP;
   }
  }
  
  return type;
 }


//获取Mobile网络下的cmwap、cmnet
 public int getCurrentApnInUse() {
  int type = NONET;
  
  Cursor cursor = context.getContentResolver().query(PREFERRED_APN_URI,
    new String[] { "_id", "apn", "type" }, null, null, null);
  cursor.moveToFirst();
  if (!cursor.isAfterLast()) {
   String apn = cursor.getString(1);
   if (apn.equalsIgnoreCase("cmnet")) {
    type = WIFIAndCMNET;
   } else if (apn.equalsIgnoreCase("cmwap")) {
    type = CMWAP;
   }
  }
  
  return type;
 }


// 获取代理地址
 private String getAgentURL(String iURL) {
  int start = "http://".length() + 1;
  int end = iURL.indexOf("/", start) + 1;
  String agentURL = "http://10.0.0.172:80/" + iURL.substring(end);

  return agentURL;
 }

 // 获取域名
 private String getDomain(String iURL) {
  int start = "http://".length();
  int end = iURL.indexOf("/", start);
  String domain = iURL.substring(start, end);

  return domain;
 }


//根据网络类型获取数据放入到httpconnection。cmwap代理:其实就是将域名换成10.0...的IP就行
 private boolean get() {
  boolean getFlag = false;
  // this.isConnected = false;
  URL getUrl = null;
  try {
   if (netType == 1) {
    try {
     getUrl = new URL(this.netURL);
    } catch (MalformedURLException e) {
     e.printStackTrace();
    }
    httpconnection = (HttpURLConnection) getUrl.openConnection();
   } else {
    try {
     getUrl = new URL(getAgentURL(this.netURL));
    } catch (MalformedURLException e) {
     e.printStackTrace();
    }
    httpconnection = (HttpURLConnection) getUrl.openConnection();
    httpconnection.setRequestProperty("X-Online-Host",getDomain(this.netURL));
   }
   httpconnection.setUseCaches(false);
   httpconnection.setRequestMethod("GET");
   httpconnection.connect();
   getFlag = true;
  } catch (IOException e) {
   e.printStackTrace();
  } catch (IllegalArgumentException exp) {
   exp.printStackTrace();
  } catch (SecurityException exp) {
   exp.printStackTrace();
  } catch (Exception exp) {
   exp.printStackTrace();
  }
  return getFlag;
 }

 

剩下来就是获取数据,若是文本文件,注意编码问题。
若是乱码,将获取的字符串重新编码一次。
content = new String(conbuffer.toString().getBytes("ISO8859-1"), "UTF-8");

 

这网络部分要考虑超时、重新发起连接、数据是否是想要的数据、数据量的大小问题。很多情况....

 

原创粉丝点击