Android手机联网方式有以下几种,要适应所有的方式。

Wifi

cmnet

cmwap

wifi和cmnet连接上就可以用了, cmwap是通过移动代理上网的, 有代理服务。但是移动联通电信的代理IP和都不一样。


先检查网络是否可用及网络类型:

public static boolean isNetworkAvailable(Context ctx) {
boolean isConnection=false;
try {
ConnectivityManager cm = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if(info != null ){
String typeName = info.getTypeName().toLowerCase(); // WIFI/MOBILE
if(!typeName.equals("wifi")){
if(null != info.getExtraInfo() ){
String wap = info.getExtraInfo().toLowerCase();
//3gnet/3gwap/uninet/uniwap/cmnet/cmwap/ctnet/ctwap
if(wap.endsWith("wap"))
isWap = true;
}
}
proxy=getproxy(ctx);
if(proxy != null && !typeName.equals("wifi"))
isWap =true;
isConnection= info.isAvailable();
}
} catch (Exception e) {
e.printStackTrace();
}
return isConnection;
}


设置HttpURLConnection连接参数

private static HttpURLConnection connectionNetWap(String nameUrl,String endUrl) throws IOException {
HttpURLConnection connection = null;
if(isWap){
URL url = new URL("http://"+proxy+":80/"+endUrl); //移动网关
connection = (HttpURLConnection )url.openConnection();
connection.setRequestProperty("X-Online-Host",nameUrl);
connection.setRequestProperty("Accept","*/*");
}else {
URL url = new URL(nameUrl+endUrl);
connection = (HttpURLConnection )url.openConnection();
}
return connection;
}


使用

public static String sendPostRequest(HashMap<String ,Object> params,String strUrl)
{
String result = null;
BufferedReader br = null;
HttpURLConnection connection = null;
DataOutputStream out = null;
InputStream instream = null;
try
{
// 参数
StringBuilder paraUrl = new StringBuilder();
int index=0;
if(params != null && !params.isEmpty() ){
for(Iterator it=params.entrySet().iterator();it.hasNext();){
Map.Entry e=(Map.Entry) it.next();
if(index >0 )
paraUrl.append("&");
paraUrl.append(e.getKey().toString());
paraUrl.append("=");
paraUrl.append(URLEncoder.encode(e.getValue().toString(), "utf-8"));
index++;
}
}

//非WAP方式, 这样就可以了:
// URL url = new URL(strUrl);
// connection = (HttpURLConnection)url.openConnection();
//适应所有方式
connection=connectionNetWap(serviceUrl,strUrl);//support wap and net


// 设置是否向connection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true
connection.setDoOutput(true);
// Read from the connection. Default is true.
connection.setDoInput(true);
// Set the post method. Default is GET
connection.setRequestMethod("POST");
// Post 请求不能使用缓存
connection.setUseCaches(false);
// URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
connection.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
// 意思是正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode进行编码
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
connection.connect();
out = new DataOutputStream(connection
.getOutputStream());
// 正文,正文内容其实跟get的URL中'?'后的参数字符串一致
out.writeBytes(paraUrl.toString());
out.flush();
StringBuilder sBuilder = new StringBuilder();
//取得输入流,并使用Reader读取
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
instream=connection.getInputStream();
InputStreamReader isr = new InputStreamReader(instream, "utf-8");
int ichar;

while ((ichar = isr.read()) != 0) {
sBuilder.append((char) ichar);
}

result = sBuilder.toString();
//isr.close();
}
}
catch(Exception e)
{
Log.d("HTTP",e.getMessage());
LogMgr.getLogger(HttpUtil.class).warning(e.getMessage());
}finally{
if(null != connection)
connection.disconnect();
try{

if(null != out)
out.close(); // flush and close
}catch(IOException ioe){
Log.d("HTTP",ioe.getMessage());
}
}
return result;
}

更多相关文章

  1. [Android]四种方式解析字符串----JSON、SAX、DOM、XML
  2. Android应用获取系统属性
  3. 深入浅出学习 Android之Android布局管理:LinerLayout线性布局
  4. android中的4种launchmode详解
  5. android retrofit2.0框架的使用介绍
  6. Android(安卓)自动化测试(6)
  7. Android关于RecycleView不走onBindViewHolder和onCreateViewHold
  8. android使用HttpURLConnection上传文件同时提交参数
  9. Android中应用的快捷方式的创建

随机推荐

  1. Android获取手机短信和通话记录及通讯录
  2. android PopupWindow简单例子
  3. Android(安卓)三种菜单(Menu)的实现
  4. [ ]在Android系统上使用busybox——最简
  5. Android NDK Camera2小结
  6. Android(安卓)Audio System 之二: AudioFl
  7. android画图-----shape的使用
  8. TextView 行间距以及字间距
  9. android 全透明式状态栏
  10. CoordinatorLayout 的使用