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
JSON 一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互。(现在公司大部分都用这个)。 json的表示方式:{"city":"Beijing","street":" Chaoyang Road ","postcode":100025} ,这就是个简单的json对象。实际上此处的 json对象相当于 Java 中的 Map<String, Object>。注意 Key 只能用 String 表示。其中 Value 也可以是另一个 Object 或者数组,因此,复杂的 Object 可以嵌套表示,例如,一个 Person 对象包含 name 和 address 对象,可以表示如下:
{"name":"Michael","address":
{"city":"Beijing","street":" Chaoyang Road ","postcode":100025}
}
</SPAN>
</SPAN>JavaScript 处理 JSON 数据
function handleJson() {
var j={"name":"Michael","address":
{"city":"Beijing","street":" Chaoyang Road ","postcode":100025}
}
document.write(j.name)
document.write(j.address.city)
}
现在一般在后台用gson将数据转换成json对象,前台用jquery的ajax与后台的数据进行交互。具体咋用网上有很多实例,如果实在不懂得话,加我的qq:1249119842.