public static void main(String[] args) throws Exception { String path = "http://res.img.ifeng.com/2011/1219/xes_bb10a1495fa8e7f76415023d177585d0.jpg";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5*1000); InputStream inStream = conn.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024];
int len=0; while((len=inStream.read(buffer))!=-1){ outStream.write(buffer,0,len); }
byte[] data = outStream.toByteArray();
File f = new File("pic.jpg"); FileOutputStream fos = new FileOutputStream(f); fos.write(data); fos.close(); } |
资源
|
布局
|
添加ImageService类
package cn.class3g.service; … public class ImageService { public static byte[] getImage(String path) throws Exception{ URL url = new URL(path); //get //post HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5*1000); InputStream inStream = conn.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len = inStream.read(buffer)) !=-1 ){ outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray();//图片的二进制数据 outStream.close(); inStream.close(); return data; } } |
Activity
public void onClick(View v) { String path = "http://res.img.ifeng.com/2011/1221/xes_5a3a1d15ffb4b856a02237cdd79d4e41.jpg";
try { byte[] data = ImageService.getImage(path);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); imgView.setImageBitmap(bitmap);
} catch (Exception e) { // e.printStackTrace(); Log.e("TAG", e.toString()); Toast.makeText(this, R.string.error, 1).show(); } } |
布局
|
代码:在前面的ImageService.getImage()方法基础上修改即可
HtmlService
public class HtmlService { /** * 获取给定路径的html代码 * @param path 网页路径 * @return * @throws Exception */ public static String getHtml(String path) throws Exception{ URL url = new URL(path); //get //post HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5*1000); InputStream inStream = conn.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len = inStream.read(buffer)) !=-1 ){ outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray();//网页的二进制数据 outStream.close(); inStream.close(); return new String(data, "gb2312"); } } |
ShowHtmlActivity
public void onClick(View v) { String path = pathText.getText().toString(); try { String htmlcode = HtmlService.getHtml(path); resultView.setText(htmlcode); } catch (Exception e) { Log.e(TAG, e.toString()); Toast.makeText(ShowHtmlActivity.this, R.string.error, 1).show(); } } |
获取web服务器xml数据
资源
|
布局
main.xml
"1.0" encoding="utf-8"?> android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> |
item.xml
"1.0" encoding="utf-8"?> android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
android:id="@+id/title" android:layout_width="250dip" android:layout_height="wrap_content" /> android:id="@+id/timelength" android:layout_width="fill_parent" android:layout_height="wrap_content" /> |
添加业务bean类Video
package cn.class3g.domain;
public class Video { private Integer id; private String title; private Integer timelength;
public Video(Integer id, String title, Integer timelength) { super(); this.id = id; this.title = title; this.timelength = timelength; } public Video() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Integer getTimelength() { return timelength; } public void setTimelength(Integer timelength) { this.timelength = timelength; } public String toString() { return "Video [id=" + id + ", title=" + title + ", timelength=" + timelength + "]"; } } |
添加业务类VideoService类
package cn.class3g.service; ... public class VideoService {
/**
* * @return * @throws Throwable */ public static List String path = "http://192.168.1.102:8080/videoweb/video/list.do"; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5 * 1000); conn.setRequestMethod("GET"); InputStream inStream = conn.getInputStream(); return parseXML(inStream); }
private static List Video video = null; List XmlPullParser pullParser = Xml.newPullParser(); pullParser.setInput(inStream, "UTF-8");
int event = pullParser.getEventType();// 触发第一个事件
while (event != XmlPullParser.END_DOCUMENT) { switch (event) { case XmlPullParser.START_DOCUMENT: videos = new ArrayList break; case XmlPullParser.START_TAG: if ("video".equals(pullParser.getName())) { int id = new Integer(pullParser.getAttributeValue(0)); video = new Video(); video.setId(id); } if (video != null) { if ("title".equals(pullParser.getName())) { video.setTitle(pullParser.nextText()); } if ("timelength".equals(pullParser.getName())) { video.setTimelength(new Integer(pullParser.nextText())); } } break;
case XmlPullParser.END_TAG: if ("video".equals(pullParser.getName())) { videos.add(video); video = null; } break; } event = pullParser.next(); } return videos; } } |
Activity类MyVideoNewsActivity
public class MyVideoNewsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
try { List ListView listView = (ListView)this.findViewById(R.id.listView); List for(Video video : videos){ HashMap item.put("title", video.getTitle()); item.put("timelength", "时长:"+video.getTimelength()); item.put("id", video.getId()); 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 (Throwable e) { Log.e("TAG", e.toString()); Toast.makeText(this, R.string.error, 1).show(); } } } |
布局
|
代码:在前面的ImageService.getImage()方法基础上修改即可
HtmlService
public class HtmlService { /** * 获取给定路径的html代码 * @param path 网页路径 * @return * @throws Exception */ public static String getHtml(String path) throws Exception{ URL url = new URL(path); //get //post HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5*1000); InputStream inStream = conn.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len = inStream.read(buffer)) !=-1 ){ outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray();//网页的二进制数据 outStream.close(); inStream.close(); return new String(data, "gb2312"); } } |
ShowHtmlActivity
public void onClick(View v) { String path = pathText.getText().toString(); try { String htmlcode = HtmlService.getHtml(path); resultView.setText(htmlcode); } catch (Exception e) { Log.e(TAG, e.toString()); Toast.makeText(ShowHtmlActivity.this, R.string.error, 1).show(); } } |