Chinaunix首页 | 论坛 | 博客
  • 博客访问: 900132
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: Java

2011-01-07 09:31:03

协议

getdatafromInternet

org.wp.net.activity

DataActivity

Java代码

  1. package org.wp.net.activity;
  2.  
  3. import org.wp.net.utils.NetTool;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.ImageView;
  14. import android.widget.Toast;
  15.  
  16. public class DataActivity extends Activity {
  17.     private static final String TAG = "DataActivity";
  18.     private EditText imagePathText;
  19.     private ImageView imageView;
  20.  
  21.     @Override
  22.     public void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.main);
  25.  
  26.         imagePathText = (EditText) this.findViewById(R.id.imagepath);
  27.         imageView = (ImageView) this.findViewById(R.id.imageview);
  28.  
  29.         Button button = (Button) this.findViewById(R.id.button);
  30.         button.setOnClickListener(new View.OnClickListener() {
  31.             @Override
  32.             public void onClick(View arg0) {
  33.                 String path = imagePathText.getText().toString();
  34.                 try {
  35.                     byte[] data = NetTool.getImage(path);// 获取图片
  36.                     /*
  37.                      * 使用代理获取
  38.                      * byte[] data = NetTool.getImageByProxy(path);
  39.                      */
  40.                     Bitmap bm = BitmapFactory.decodeByteArray(data, 0,
  41.                             data.length);
  42.                     imageView.setImageBitmap(bm);
  43.                 } catch (Exception e) {
  44.                     Log.e(TAG, e.toString());
  45.                     Toast.makeText(DataActivity.this, "获取图片失败", 1).show();
  46.                 }
  47.             }
  48.         });
  49.  
  50.         Button sohuButton = (Button) this.findViewById(R.id.sohuButton);
  51.         sohuButton.setOnClickListener(new View.OnClickListener() {
  52.             @Override
  53.             public void onClick(View arg0) {
  54.                 Intent intent = new Intent();
  55.                 intent.setClass(DataActivity.this, SohuActivity.class);
  56.                 startActivity(intent);
  57.             }
  58.         });
  59.     }
  60. }

SohuActivity

Java代码

  1. package org.wp.net.activity;
  2.  
  3. import org.wp.net.utils.NetTool;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9.  
  10. public class SohuActivity extends Activity {
  11.     private static final String TAG = "SohuActivity";
  12.     private TextView textView;
  13.  
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.sohu);
  18.  
  19.         textView = (TextView) this.findViewById(R.id.sohu);
  20.         try {
  21.             String html = NetTool.getHtml("", "GBK");
  22.             textView.setText(html);
  23.         } catch (Exception e) {
  24.             Log.e(TAG, e.toString());
  25.             Toast.makeText(SohuActivity.this, "获取网页数据失败", Toast.LENGTH_SHORT)
  26.                     .show();
  27.         }
  28.     }
  29. }

org.wp.net.utils

NetTool

Java代码

  1. package org.wp.net.utils;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.InputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.InetSocketAddress;
  7. import java.net.Proxy;
  8. import java.net.URL;
  9.  
  10. public class NetTool {
  11.  
  12.     /**
  13.      * 获取给定Url路径的数据
  14.      *
  15.      * @param urlpath Url路径
  16.      * @return
  17.      * @throws Exception
  18.      */
  19.     public static byte[] getImage(String urlpath) throws Exception {
  20.         URL url = new URL(urlpath);
  21.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  22.         conn.setRequestMethod("GET");
  23.         conn.setConnectTimeout(6 * 1000);
  24.         if (conn.getResponseCode() == 200) {
  25.             InputStream inputStream = conn.getInputStream();
  26.             return readStream(inputStream);
  27.         }
  28.         return null;
  29.     }
  30.  
  31.     /**
  32.      * 使用代理
  33.      *
  34.      * @param urlpath
  35.      * @return
  36.      * @throws Exception
  37.      */
  38.     public static byte[] getImageByProxy(String urlpath) throws Exception {
  39.         URL url = new URL(urlpath);
  40.         Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
  41.                 "124.207.99.60", 8080));
  42.         HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
  43.         conn.setRequestMethod("GET");
  44.         conn.setConnectTimeout(6 * 1000);
  45.         if (conn.getResponseCode() == 200) {
  46.             InputStream inputStream = conn.getInputStream();
  47.             return readStream(inputStream);
  48.         }
  49.         return null;
  50.     }
  51.  
  52.     /**
  53.      * 获取url路径指定的网页代码
  54.      *
  55.      * @param urlpath url路径
  56.      * @return
  57.      * @throws Exception
  58.      */
  59.     public static String getHtml(String urlpath, String encoding)
  60.             throws Exception {
  61.         URL url = new URL(urlpath);
  62.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  63.         conn.setConnectTimeout(6 * 1000);
  64.         conn.setRequestMethod("GET");
  65.         if (conn.getResponseCode() == 200) {
  66.             InputStream inputStream = conn.getInputStream();
  67.             byte[] data = readStream(inputStream);
  68.             return new String(data, encoding);
  69.         } else {
  70.             throw new RuntimeException("请求url失败");
  71.         }
  72.     }
  73.  
  74.     /**
  75.      * 读取数据
  76.      *
  77.      * @param inputStream 输入流
  78.      * @return
  79.      * @throws Exception
  80.      */
  81.     public static byte[] readStream(InputStream inputStream) throws Exception {
  82.         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  83.         byte[] buffer = new byte[1024];
  84.         int len = -1;
  85.         while ((len = inputStream.read(buffer)) != -1) {
  86.             outputStream.write(buffer, 0, len);
  87.         }
  88.         outputStream.close();
  89.         inputStream.close();
  90.         return outputStream.toByteArray();
  91.     }
  92. }

main.xml

Xml代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <TextView
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content"
  10.         android:text="@string/path"
  11.         />
  12.     <EditText
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="wrap_content"
  15.         android:text=""
  16.         android:id="@+id/imagepath"
  17.         />
  18.     <Button
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"
  21.         android:text="@string/button"
  22.         android:id="@+id/button"
  23.         />
  24.     <ImageView
  25.         android:layout_width="wrap_content"
  26.         android:layout_height="wrap_content"
  27.         android:id="@+id/imageview"
  28.         />
  29.     <Button
  30.         android:layout_width="wrap_content"
  31.         android:layout_height="wrap_content"
  32.         android:text="获取sohu网页内容"
  33.         android:id="@+id/sohuButton"
  34.         />
  35. </LinearLayout>

sohu.xml

Xml代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <ScrollView
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="fill_parent"
  10.         >
  11.     <TextView
  12.         android:layout_width="fill_parent"
  13.         android:layout_height="wrap_content"
  14.         android:id="@+id/sohu"
  15.         />
  16.     </ScrollView>
  17. </LinearLayout>

strings.xml

Xml代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <string name="hello">Hello World, DataActivity!</string>
  4.     <string name="app_name">从Internet获取数据</string>
  5.     <string name="path">图片路径</string>
  6.     <string name="button">获取图片</string>
  7. </resources>

AndroidManifest.xml

Xml代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="org.wp.net.activity" android:versionCode="1"
  4.     android:versionName="1.0">
  5.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  6.         <activity android:name=".DataActivity" android:label="@string/app_name">
  7.             <intent-filter>
  8.                 <action android:name="android.intent.action.MAIN" />
  9.                 <category android:name="android.intent.category.LAUNCHER" />
  10.             </intent-filter>
  11.         </activity>
  12.         <activity android:name=".SohuActivity" android:label="sohu网页" />
  13.     </application>
  14.     <uses-sdk android:minSdkVersion="7" />
  15.     <!-- 访问Internet权限 -->
  16.     <uses-permission android:name="android.permission.INTERNET" />
  17. </manifest>

阅读(971) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~