解析JS (Gson)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解析JS (Gson)相关的知识,希望对你有一定的参考价值。
参考技术A //创建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
以上是关于解析JS (Gson)的主要内容,如果未能解决你的问题,请参考以下文章
在用Gson解析对象的时候,json数据中没有的字段,会赋初值。