歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用開發之獲取網絡數據

Android應用開發之獲取網絡數據

日期:2017/3/1 10:44:30   编辑:Linux編程

J2SE實現網絡圖片的獲取

  1. public static void main(String[] args) throws Exception {
  2. String path = "http://img.linuxidc.com/img/Android.png";
  3. URL url = new URL(path);
  4. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  5. conn.setRequestMethod("GET");
  6. conn.setConnectTimeout(5*1000);
  7. InputStream inStream = conn.getInputStream();
  8. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  9. byte[] buffer = new byte[1024];
  10. int len=0;
  11. while((len=inStream.read(buffer))!=-1){
  12. outStream.write(buffer,0,len);
  13. }
  14. byte[] data = outStream.toByteArray();
  15. File f = new File("pic.jpg");
  16. FileOutputStream fos = new FileOutputStream(f);
  17. fos.write(data);
  18. fos.close();
  19. }

Androd中獲取網絡圖片

資源

  1. <string name="btn_text">顯示網絡圖片</string>
  2. <string name="error">下載圖片失敗!!</string>

布局

  1. <Button
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. android:text="@string/btn_text"
  5. android:id="@+id/showBtn"
  6. />
  7. <ImageView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:id="@+id/imageView"
  11. />

添加ImageService類

  1. package cn.class3g.service;
  2. public class ImageService {
  3. public static byte[] getImage(String path) throws Exception{
  4. URL url = new URL(path);
  5. //get //post
  6. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  7. conn.setRequestMethod("GET");
  8. conn.setConnectTimeout(5*1000);
  9. InputStream inStream = conn.getInputStream();
  10. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  11. byte[] buffer = new byte[1024];
  12. int len = 0;
  13. while( (len = inStream.read(buffer)) !=-1 ){
  14. outStream.write(buffer, 0, len);
  15. }
  16. byte[] data = outStream.toByteArray();//圖片的二進制數據
  17. outStream.close();
  18. inStream.close();
  19. return data;
  20. }
  21. }

Activity

  1. public void onClick(View v) {
  2. String path = "http://img.linuxidc.com/img/Android.png";
  3. try {
  4. byte[] data = ImageService.getImage(path);
  5. Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
  6. imgView.setImageBitmap(bitmap);
  7. } catch (Exception e) {
  8. // e.printStackTrace();
  9. Log.e("TAG", e.toString());
  10. Toast.makeText(this, R.string.error, 1).show();
  11. }
  12. }

Androd中獲取網頁代碼

布局

  1. <Button
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. android:text="@string/btn_text"
  5. android:id="@+id/showBtn"
  6. />
  7. <ImageView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:id="@+id/imageView"
  11. />

代碼:在前面的ImageService.getImage()方法基礎上修改即可

HtmlService

  1. public class HtmlService {
  2. /**
  3. * 獲取給定路徑的html代碼
  4. * @param path 網頁路徑
  5. * @return
  6. * @throws Exception
  7. */
  8. public static String getHtml(String path) throws Exception{
  9. URL url = new URL(path);
  10. //get //post
  11. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  12. conn.setRequestMethod("GET");
  13. conn.setConnectTimeout(5*1000);
  14. InputStream inStream = conn.getInputStream();
  15. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  16. byte[] buffer = new byte[1024];
  17. int len = 0;
  18. while( (len = inStream.read(buffer)) !=-1 ){
  19. outStream.write(buffer, 0, len);
  20. }
  21. byte[] data = outStream.toByteArray();//網頁的二進制數據
  22. outStream.close();
  23. inStream.close();
  24. return new String(data, "gb2312");
  25. }
  26. }

ShowHtmlActivity

  1. public void onClick(View v) {
  2. String path = pathText.getText().toString();
  3. try {
  4. String htmlcode = HtmlService.getHtml(path);
  5. resultView.setText(htmlcode);
  6. } catch (Exception e) {
  7. Log.e(TAG, e.toString());
  8. Toast.makeText(ShowHtmlActivity.this, R.string.error, 1).show();
  9. }
  10. }
Copyright © Linux教程網 All Rights Reserved