1.啟動模擬器(android 2.2 emulator ),然後進入 settings->Wireless & network ->Mobile networks->Access Point Names
然後開啟出現在列表中的 Telkila.
2.然後下面這樣設定:
- Proxy : your proxy address
- Port : your proxy port
經過以上2步,系統的瀏覽器已經可以訪問internet了。但是要讓程式中的webview或httpconnection等訪問internet還不行。怎麼辦呢?
首先,要讓程式訪問internet,需要加permission, 第二個permission為添加代理而增加:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
1。 對於webview
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings()
.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mWebView.enablePlatformNotifications();
搞定!
2. 對於URLConnection:
URL url = new URL(urlStr);
String host = android.net.Proxy.getDefaultHost();
int port = android.net.Proxy.getDefaultPort();
SocketAddress sa = new InetSocketAddress(host, port);
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,
sa);
URLConnection conn = url.openConnection(proxy);
InputStream is = conn.getInputStream();
搞定!
3. 對於HttpClient:
DefaultHttpClient httpClient=new DefaultHttpClient();
String host=Proxy.getDefaultHost();//此處Proxy源自android.net
int port = Proxy.getPort(context);//同上
HttpHost httpHost = new HttpHost(host, port);
//設定代理
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,httpHost);
HttpGet httpGet=new HttpPost("<a href="http://www.baidu.com">www.baidu.com</a>");
HttpResponse response=httpClient.execute(httpGet);
4. 以下為轉載的相關內容:
判斷手機是否連網
boolean isConnect(){
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm!=null){
return true;
}
return false;
}
判斷當前網路是否為WIFI
boolean isWifi(){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm!=null){
NetworkInfo ni = cm.getActiveNetworkInfo();
if(!ni.getTypeName().equals("WIFI")){
/*
* ni.getTypeNmae()可能取值如下
* WIFI,表示WIFI連網
* MOBILE,表示GPRS、EGPRS
* 3G網路沒有測試過
* WIFI和(E)GPRS不能共存,如果兩個都開啟,系統僅支援WIFI
*/
return true;
}
}
return false;
}
綜合判斷網路類型,我們便可以確定是否需要設定代理,實現正確的連網。
樣本一
HttpURLConnection con =null;
URL postUrl = new URL("www.baidu.com");
boolean isProxy=false;
//網路檢測
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isProxy=false;
if(cm!=null){
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni!=null){
if(! ni.getTypeName().equals("WIFI")){
isProxy=true;
}
}
}
if(isProxy){
Proxy proxy=new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress(android.net.Proxy.getDefaultHost(),android.net.Proxy.getDefaultPort()));
con = (HttpURLConnection) postUrl.openConnection(proxy);
}else{
con = (HttpURLConnection) postUrl.openConnection();
}
樣本二
DefaultHttpClient httpClient=new DefaultHttpClient();
//網路檢測
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm!=null){
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni!=null){
if(!ni.getTypeName().equals("WIFI")){
//設定代理
String host=Proxy.getDefaultHost();
int port = Proxy.getPort(context);
HttpHost httpHost = new HttpHost(host, port);
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,httpHost);
}
}
}
原文連結:http://blog.csdn.net/fhy_2008/article/details/7027892