---------------------- android培训、java培训、期待与您交流! ----------------------
第四天
1、 从网上获取图片并显示在android上:
public class StreamTool {//读取二进制数据
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len); }
inStream.close();
return outStream.toByteArray(); }}
public class ImageService {
public static byte[] getImage(String path) throws Exception {
URL url = new URL("http://i3.itc.cn/20100707/76c_0969b700_d5b4_41cd_8243_9b486be92cc4_0.jpg");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();//通过输入流获取图片数据
return StreamTool.readInputStream(inStream);//得到图片的二进制数据
}}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pathText = (EditText) this.findViewById(R.id.urlpath);
imageView = (ImageView) this.findViewById(R.id.imageView);
Button button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String path = pathText.getText().toString();
try {
byte[] data = ImageService.getImage(path);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//生成位图
imageView.setImageBitmap(bitmap);//显示图片 } catch (Exception e) {
Toast.makeText(ImageShowActivity.this, R.string.error, 1).show(); Log.e(TAG, e.toString()); } } }); }
同理,可以用此种方法获取网页代码显示在android模拟器上。当显示网页代码时,由于代码太多,一页显示不完,可以增加滚动标签,方法如下:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
/>
</ScrollView>
应用网络是,需先声明权限:
<uses-permission android:name="android.permission.INTERNET"/>
1. 从网络返回XML数据并解析显示,解析方法前面已经讲过。此种方法性能较低,一般建议网站采用json数据格式方式返回数据,此种数据的解析性能远远高于xml方式。json格式为:[{id:78,title:”喜洋洋与灰太狼”,timelength:24},{id:23, title:”三国演义”, timelength:65}]。由于对象的个数不确定,因此,此处要用到ListView的知识用于显示对象及属性。Json是一个开源项目,android已经将jar包导入了android项目中。
得到Json数据并解析:
public static List<Video> getJSONLastVideos() throws Exception{
List<Video> videos = new ArrayList<Video>();
String path = "http://192.168.1.100:8080/videoweb/video/list.do?format=json";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(5*1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
byte[] data = StreamTool.readInputStream(inStream);
String json = new String(data);
JSONArray array = new JSONArray(json);
for(int i=0 ; i < array.length() ; i++){
JSONObject item = array.getJSONObject(i);
int id = item.getInt("id");
String title = item.getString("title");
int timelength = item.getInt("timelength");
videos.add(new Video(id, title, timelength)); }
return videos; }
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView)this.findViewById(R.id.listView); try {
List<Video> videos = VideoService.getJSONLastVideos();
List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
for(Video video : videos){
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("id", video.getId());
item.put("title", video.getTitle());
item.put("timelength", "时长:"+ video.getTime());
data.add(item); }
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
new String[]{"title", "timelength"}, new int[]{R.id.title, R.id.timelength});
listView.setAdapter(adapter);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "获取最新视频资讯失败", 1).show();
Log.e("MainActivity", e.toString()); } }
---------------------- android培训、java培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net/heima