Android程序联网

Android Http连接有时在ctnet(cmnet)下很正常,可是有时候在用户选择ctwap(cmwap)时就会没有反应,这是因为wap是,只能访问IP为10.*。*。*的IP地址,这时候需要使用代理。总的来说有以下三种方式:

(方式一):

 

一个是Java.net包中的:HttpURLConnection,它设置代理代码如下: Proxy proxy=new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress(“10.0.0.172″,80));
HttpURLConnection connection=(HttpURLConnection) url.openConnection(proxy); 还有一个是Apache的HttpClient: DefaultHttpClient httpclient = new DefaultHttpClient(); HttpHost proxy = new HttpHost(“10.0.0.172″, 80); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
(方式二):

URL url = new URL("http://10.0.0.172/img/baidu_logo.gif");  


HttpURLConnection conn = (HttpURLConnection) url.openConnection();  

conn.setRequestProperty("X-Online-Host", "www.baidu.com"); //J2ME设置CMWAP代理上网

conn.setDoInput(true);  
conn.connect();  

InputStream is = conn.getInputStream();  

bitmap = BitmapFactory.decodeStream(is);  

is.close(); 

conn.disconnect();

或者:

imageUrl = "http://www.baidu.com/img/baidu_logo.gif“;


Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress("10.0.0.200", 80));  

HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(proxy);

conn.connect();

InputStream is = conn.getInputStream();

bitmap = BitmapFactory.decodeStream(is);

is.close();

conn.disconnect();

(方式三):

   java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP,  
   new InetSocketAddress(android.net.Proxy.getDefaultHost(),  
   android.net.Proxy.getDefaultPort()));  
   conn = (HttpURLConnection) url.openConnection(p);
   Log.v("http", "httpdaili");

 

你可能感兴趣的:(apache,android,url,WAP)