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 }
posted @ 2012-05-29 09:51  hnrainll  阅读(505)  评论(0编辑  收藏  举报