class Load extends AsyncTask<string, string= "" >
{
public String url = http: //open.iciba.com/dsapi/;
ProgressDialog pdlg;
String jsonstr = ;
JSONObject json = null ;
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httppost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
BufferedReader reader = new BufferedReader( new InputStreamReader(is, UTF- 8 ));
StringBuilder sb = new StringBuilder();
String line = null ;
while ((line = reader.readLine()) != null )
{
sb.append(line +
);
}
is.close();
jsonstr = sb.toString();
json = new JSONObject(jsonstr.toString());
engstr = json.getString(content);
chistr = json.getString(note);
imagurl = json.getString(picture);
timestr = json.getString(dateline);
fromstr = json.getString(translation);
JSONArray array = json.getJSONArray(tags);
for ( int i= 0 ;i<p>使用了一个图片处理的工具类,ImageLoader,主要用来通过url解析图片,处理图片的大小,以文件的形式缓存图片。</p><pre class = "brush:java;" > public class ImageLoader {
MemoryCache memoryCache= new MemoryCache();
FileCache fileCache;
private Map<imageview, string= "" > imageViews=Collections.synchronizedMap( new WeakHashMap<imageview, string= "" >());
ExecutorService executorService;
public ImageLoader(Context context){
fileCache= new FileCache(context);
executorService=Executors.newFixedThreadPool( 5 );
}
final int stub_id = R.drawable.drug_trans;
public void DisplayImage(String url, ImageView imageView)
{
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);
if (bitmap!= null )
imageView.setImageBitmap(bitmap);
else
{
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView)
{
PhotoToLoad p= new PhotoToLoad(url, imageView);
executorService.submit( new PhotosLoader(p));
}
private Bitmap getBitmap(String url)
{
File f=fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if (b!= null )
return b;
//from web
try {
Bitmap bitmap= null ;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout( 30000 );
conn.setReadTimeout( 30000 );
conn.setInstanceFollowRedirects( true );
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex){
ex.printStackTrace();
return null ;
}
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true ;
BitmapFactory.decodeStream( new FileInputStream(f), null ,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE= 70 ;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale= 1 ;
while ( true ){
if (width_tmp/ 1.5 <required_size a= "(Activity)photoToLoad.imageView.getContext();" activity= "" bd= "new" bitmap= "b;photoToLoad=p;}" bitmapdisplayer= "" bitmapfactory.options= "" bmp= "getBitmap(photoToLoad.url);" boolean = "" catch = "" decode= "" display= "" else = "" filenotfoundexception= "" for = "" height_tmp= "" imageview= "" implements = "" in= "" insamplesize= "" new = "" o2= "new" o2.insamplesize= "scale;" override= "" photosloader= "" phototoload= "" pre= "" private = "" public = "" queue= "" return = "" runnable= "" string= "" tag= "=null" task= "" the= "" this .phototoload= "photoToLoad;" thread= "" to= "" ui= "" url= "u;" used= "" void = "" width_tmp= "" with= "" ><p> </p><p> </p></required_size></imageview,></imageview,></pre>
</array.length();i&# 43 ;&# 43 ;)></string,>
推推族,免费得门票,游景区:www.tuituizu.com
结伴旅游,一个免费的交友网站:www.jieberu.com
|