android網絡連接Wifi和cmnet及cmwap的問題

困擾了我很久的,android ,http client無法直接使用cmwap,使用某些wifi會出錯的問題,這2天被解決了,也是在網上無意看到別人說的就這麼做了。

 

第一個。某些Wifi連接,有時候出現100-continue的錯誤,apache http client說什麼協議錯誤,解決辦法:

//關閉Expect:100-Continue握手
//100-Continue握手需謹慎使用,因爲遇到不支持HTTP/1.1協議的服務器或者代理時會引起問題
httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

  

第二個:使用cmwap上網,cmwap本質是使用代理上網,之前未明白,折騰了好久,用httpclient,很簡單,如下:

HttpHost proxy = new HttpHost("10.0.0.172", 80);//設置cmwap代理
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

 如果檢測呢,一下代碼可以檢測當前移動網絡的apn代理情況。

ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (connectivity != null) { 
				// 獲取網絡連接管理的對象
				NetworkInfo info = connectivity.getActiveNetworkInfo();

				if (info != null && info.isConnected()) {
					// 判斷當前網絡是否已經連接
					if (info.getState() == NetworkInfo.State.CONNECTED) {
 						if(info.getTypeName().equals("WIFI")){
							 
						else{ 
							Uri uri = Uri.parse("content://telephony/carriers/preferapn");
				            Cursor cr = context.getContentResolver().query(uri, null,null, null, null);
				            while (cr != null && cr.moveToNext()) {
			                      // APN id
			                      @SuppressWarnings("unused")
			                      String id = cr.getString(cr.getColumnIndex("_id"));
			                      // APN name
			                      @SuppressWarnings("unused")
			                      String apn = cr.getString(cr.getColumnIndex("apn"));
			                      // do other things...
			                      String strProxy = cr.getString(cr.getColumnIndex("proxy"));
			                      String strPort = cr.getString(cr.getColumnIndex("port"));
			                      if(strProxy != null && !"".equals(strProxy)){
				                      Config.host = strProxy;
				                      Config.port = Integer.valueOf(strPort);
			                      }
			                      
				             }
						} 
					}
				}

 

現在很完美了,程序可以在cmnet,cmwap,以及wifi下完美運行。

發佈了21 篇原創文章 · 獲贊 0 · 訪問量 3526
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章