Jamendo 使用java.net.URL类访问网络数据

我们通过URL类或者HttpClient类,都可以对网络访问,至于这两个类的区别,HttpClient类底层还是使用了Url类,对其封装了一下。

 RemoteImageView类中:

..........

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			mTaskUrl = params[0];
			InputStream stream = null;
			URL imageUrl;
			Bitmap bmp = null;
			
			try {
				imageUrl = new URL(mTaskUrl);
				try {
					stream = imageUrl.openStream();
					bmp = BitmapFactory.decodeStream(stream);
					
					try {
						if(bmp != null){
							JamendoApplication.getInstance().getImageCache().put(mTaskUrl, bmp);
							Log.d(JamendoApplication.TAG,"Image cached " + mTaskUrl);
						}else{
							Log.w(JamendoApplication.TAG, "failed to cache "+ mTaskUrl);
						}
					} catch (NullPointerException e) {
						// TODO Auto-generated catch block
						Log.w(JamendoApplication.TAG, "failed to cache "+ mTaskUrl);
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					Log.w(TAG,"Could not load bitmap from url: " + mTaskUrl);
				}finally{
					try{
						if(stream != null)
							stream.close();
					}catch(IOException e){
						
					}
				}
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return mTaskUrl;
		}


详细见:http://blog.csdn.net/a107494639/article/details/7331014

你可能感兴趣的:(Jamendo 使用java.net.URL类访问网络数据)