Android 网络显示图片 通过代理访问

主要的代码参考:http://www.cnblogs.com/hnrainll/archive/2012/05/28/2522558.html

 

重点看如何在Android代码中设置代理。

代码如下:

 1 public class MainDemo extends Activity {
 2     /** Called when the activity is first created. */
 3 
 4     private ImageView imageView = null;
 5 
 6     private String urlString = "http://s7.sinaimg.cn/middle/9b82a8c54c10ecacbb686&960";
 7     
 8     @Override
 9     public void onCreate(Bundle savedInstanceState) {
10         super.onCreate(savedInstanceState);
11         setContentView(R.layout.main);
12 
13         imageView = (ImageView) findViewById(R.id.imageview01);
14         imageView.setImageBitmap(returnBitmap(urlString));
15     }
16 
17     public Bitmap returnBitmap(String url) {
18         Bitmap bm = null;
19 
20         try {
21             HttpURLConnection conn = getUrlConnection(url);
22             conn.setDoInput(true);
23             conn.connect();
24             InputStream is = conn.getInputStream();
25             bm = BitmapFactory.decodeStream(is);
26             is.close();
27         } catch (IOException e) {
28             // TODO Auto-generated catch block
29             e.printStackTrace();
30         }
31 
32         return bm;
33     }
34 
35     private HttpURLConnection getUrlConnection(String url) {
36         java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP,
37                         new InetSocketAddress("172.20.220.2",
38                                 8080));
39         try {
40             return (HttpURLConnection) new URL(url).openConnection(p);
41         } catch (MalformedURLException e) {
42             // TODO Auto-generated catch block
43             e.printStackTrace();
44         } catch (IOException e) {
45             // TODO Auto-generated catch block
46             e.printStackTrace();
47         }
48         return null;
49     }
50 }

转载于:https://www.cnblogs.com/hnrainll/archive/2012/05/29/2523503.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值