实现ListView图文混排 —— 获取Json数据
2015-12-30 22:33
791 查看
先新建一个布局文件和NewsBean类
在activity_main.xml中,引用前面新建的布局
在MainActivity中写相应的代码。
这是如何通过InputStream读取网络信息的呢?
传进一个InputStream字节流,通过InputStreamReader将字节流转化为字符流,再通过BufferedReader将字节流以Buffer形式读取出来,最终拼接到result中。
最后,要记得在清单文件中添加权限。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/iv_image" android:layout_width="64dp" android:layout_height="64dp" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="64dp" android:gravity="center" android:orientation="vertical" android:paddingLeft="10dp" > <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:maxLines="1" android:text="Title" android:textSize="20sp" /> <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="2" android:text="Content" android:textSize="15sp" /> </LinearLayout> </LinearLayout>
public class NewsBean { public String newsIconUrl; public String newsTitle; public String newsContent; }
在activity_main.xml中,引用前面新建的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ListView android:id="@+id/lv_main" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
在MainActivity中写相应的代码。
public class MainActivity extends Activity { private ListView listView; public static String URL = "http://www.imooc.com/api/teacher?type=4&num=30"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.lv_main); new NewsAsyncTask().execute(URL); } private List<NewsBean> getJsonData(String url) { List<NewsBean> newsBeansList = new ArrayList<NewsBean>(); try { String jsonString = readStream(new URL(url).openStream()); // 此句和url.openConnection().getInputStream()相同,可根据URL直接联网获取网络数据 // 返回值的类型为InputStream Log.d("abc", jsonString); } catch (IOException e) { e.printStackTrace(); } return newsBeansList; } private String readStream(InputStream is) { String result = ""; InputStreamReader isr; try { String line = ""; isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); while ((line = br.readLine()) != null) { result += line; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } class NewsAsyncTask extends AsyncTask<String, Void, List<NewsBean>> { @Override protected List<NewsBean> doInBackground(String... params) { // TODO Auto-generated method stub return getJsonData(params[0]); } } }
这是如何通过InputStream读取网络信息的呢?
传进一个InputStream字节流,通过InputStreamReader将字节流转化为字符流,再通过BufferedReader将字节流以Buffer形式读取出来,最终拼接到result中。
最后,要记得在清单文件中添加权限。
<uses-permission android:name="android.permission.INTERNET" />
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories