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");
这网络部分要考虑超时、重新发起连接、数据是否是想要的数据、数据量的大小问题。很多情况....
- 9月份总结II
- 9月份总结
- 9月份总结
- 9月份英语总结
- 9月份英语总结
- 9月份月报总结
- 8月份~9月份总结(下)
- 9月份英语总结,persistent。
- 17年9月份总结
- 9月份英语学习计划执行情况总结
- 9月份营销学学习总结
- 9月份英语学习总结—坚持
- 9月份实习总结-落花归于平静
- 对9月份信息学训练的总结
- 2017年9月份知识点记录总结
- 10月份总结
- 3月份总结
- 2-3月份总结
- SVCUTIL工具注解
- 国内外设计师之间的差距
- 大链表数据的排序
- Django +MySQL Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
- Digester解析XML的一个列子
- 9月份总结II
- 关于闪光灯的一点理解
- ATL Internals 笔记
- 在MyEclipse 8.6上搭建Android开发环境(图文模式)
- 软件开发的七种武器
- 英语常用短语
- 修改MyBatis 源代码,实现数据库分页及Object[]数据结果
- 项目日志
- sth about struts2