解析JS (Gson)

JavaScript08

解析JS (Gson),第1张

//创建AsyncTask对象

AsyncTaskasyncTask = new AsyncTask() {            @Override

protected void onPreExecute() {

super.onPreExecute()

}

//子线程运行的方法

@Override

protected String doInBackground(Void... params) {

//用httpclient请求数据

DefaultHttpClient defaultHttpClient = new DefaultHttpClient()

HttpGet httpGet = new HttpGet(path)

try {

HttpResponse execute = defaultHttpClient.execute(httpGet)

StatusLine statusLine = execute.getStatusLine()

int statusCode = statusLine.getStatusCode()

if (statusCode == 200) {

HttpEntity entity = execute.getEntity()

InputStream content = entity.getContent()

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()                        int len = 0

byte[] buffer = new byte[1024]

while ((len = content.read(buffer)) != -1) {

byteArrayOutputStream.write(buffer, 0, len)

}

//给主线程返回请求的数据

return byteArrayOutputStream.toString()

}

} catch (IOException e) {

e.printStackTrace()

}                return null

}

//主线程运行的方法

@Override

protected void onPostExecute(String s) {

super.onPostExecute(s)

//创建list集合

list = new ArrayList()

//gson解析

Gson gson = new Gson()

final myBean myBean = gson.fromJson(s, myBean.class)

// 把解析出来的内容存入list集合

list = myBean.data

//找控件

ListView listview = (ListView) findViewById(R.id.listview)

//创建自定义适配器

myBaseAdapter adpter = new myBaseAdapter(list, MainActivity.this)

//设置适配器

listview.setAdapter(adpter)

//设置item的条目点击事件

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView parent, View view, int position, long id) {

//跳转到第二个页面

Intent intent = new Intent(MainActivity.this, SecondActivity.class)

//intent传值

intent.putExtra("IMAGEURL", list.get(position).IMAGEURL)

intent.putExtra("TITLE", list.get(position).TITLE)

intent.putExtra("FROMNAME", list.get(position).FROMNAME)

intent.putExtra("SHOWTIME", list.get(position).SHOWTIME)

intent.putExtra("SUBTITLE",list.get(position).SUBTITLE)

startActivity(intent)

}

})

}

}

asyncTask.execute()

}

}

//////////////////////////////////////////////youhua

if (convertView == null) {

holder = new viewHolder()

convertView = convertView.inflate(context, R.layout.item, null)

holder.imageView = (ImageView) convertView.findViewById(R.id.imageview)

holder.title = (TextView) convertView.findViewById(R.id.TITLE)

holder.FROMNAME = (TextView) convertView.findViewById(R.id.FROMNAME)

holder.SHOWTIME = (TextView) convertView.findViewById(R.id.SHOWTIME)

convertView.setTag(holder)

} else {

holder = (viewHolder) convertView.getTag()

}

//这步骤是用imageloder加载图片的

http://www.jianshu.com/p/a8cbb228fb83

这个网址里有

DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()

.cacheInMemory(true)

.cacheOnDisk(true)  .build()

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)

.defaultDisplayImageOptions(displayImageOptions)

.build()

ImageLoader.getInstance().init(config)

ImageLoader.getInstance().displayImage(list.get(position).IMAGEURL, holder.imageView)

holder.title.setText(list.get(position).TITLE)

holder.FROMNAME.setText(list.get(position).FROMNAME)

holder.SHOWTIME.setText(list.get(position).SHOWTIME)

return convertView

}

解析惊悚 ViewLoader

html5uploader.js 兼容ie8方法如下:

拥有两个插件,一个是flash版本的uploadify,免费的。另一个是自己写的html5版本的,名叫html5uploader(好俗的名字。。),再加一个适配器uploadadapter,用来决定在什么时候调用哪个插件。页面中只调用uploadadapter。关键的难题就在于,页面中的代码是只写一次的,不管是flash的还是html5的都得能识别出页面上的参数,这也就是我的山寨版本的插件做的事情,原flash版本的配置参数通通得识别并有效。幸好,已经实现了。

上demo

    很多东西,一上demo就都清楚了。。。

[html] view plain copy

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>无标题文档</title>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript" src="jquery.uploadadapter.js"></script>

<script type="text/javascript" src="jquery.loadscript.js"></script>

<style type="text/css">

</style>

<script type="text/javascript">

$(function(){

  $('#upload').uploadadapter({

      fileTypeExts:'*.jpg*.png',

      auto:false,

      showUploaded:true,

      baseurl:'.',//当前目录

      multi:true,

      removeTimeout:9999999,

      uploadurl:'upload.php'

      })

  })

</script>

</head>

<body>

<input type="file" name="file" id="upload"  />

</body>

</html>

    ie8下调用flash

   firefox、chrome下调用html5

    略有差异,ie8下圆角没了。。。如果不喜欢这个样式呢,就请打开css文件自己修改吧。

    很简单的调用,可以看到在页面中使用的是uploadadapter,由它来决定调用哪个插件。俩个插件所需要的js文件和css文件都是异步引入的,此处用到一个小插件loadScript。参数没有写全,可以自己看源代码的注释~

    uploadadapter中的调用情况是我需要的配置,你也可以随意修改。

四、相关文件注释

    在此把文件夹中的文件做一个简要介绍:

    /html5uploader   html5上传插件,你也可以拿来单独使用

    /uploadify3.2        flash上传插件,也可以拿来单独使用

    /uploads    存放上传的文件

    /jquery.loadScript.js   用于异步引入脚本的小插件

    /jquery.uploadadapter.js   适配器,用来判断客户端类型,动态调用上传插件

    /upload.php    后台处理程序,最基本的

五、上源码,注释很全哦

http://download.csdn.net/detail/never_say_goodbye/5090639

六、一个bug!!

很重的哦,我之前给疏漏了,在这里说一下,文件就不重新上传了

在jquery.html5uploader.js的158和164行,将$('.uploadify-progress')改为$('#'+file.index).fnd('.uploadify-progress'),否则上传多个文件会混淆。