协议
getdatafromInternet
org.wp.net.activity
DataActivity
Java代码
- package org.wp.net.activity;
-
-
import org.wp.net.utils.NetTool;
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.graphics.Bitmap;
-
import android.graphics.BitmapFactory;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.View;
-
import android.widget.Button;
-
import android.widget.EditText;
-
import android.widget.ImageView;
-
import android.widget.Toast;
-
-
public class DataActivity extends Activity {
-
private static final String TAG = "DataActivity";
-
private EditText imagePathText;
-
private ImageView imageView;
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
imagePathText = (EditText) this.findViewById(R.id.imagepath);
-
imageView = (ImageView) this.findViewById(R.id.imageview);
-
-
Button button = (Button) this.findViewById(R.id.button);
-
button.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View arg0) {
-
String path = imagePathText.getText().toString();
-
try {
-
byte[] data = NetTool.getImage(path);// 获取图片
-
/*
-
* 使用代理获取
-
* byte[] data = NetTool.getImageByProxy(path);
-
*/
-
Bitmap bm = BitmapFactory.decodeByteArray(data, 0,
-
data.length);
-
imageView.setImageBitmap(bm);
-
} catch (Exception e) {
-
Log.e(TAG, e.toString());
-
Toast.makeText(DataActivity.this, "获取图片失败", 1).show();
-
}
-
}
-
});
-
-
Button sohuButton = (Button) this.findViewById(R.id.sohuButton);
-
sohuButton.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View arg0) {
-
Intent intent = new Intent();
-
intent.setClass(DataActivity.this, SohuActivity.class);
-
startActivity(intent);
-
}
-
});
-
}
-
}
SohuActivity
Java代码
- package org.wp.net.activity;
-
-
import org.wp.net.utils.NetTool;
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.widget.TextView;
-
import android.widget.Toast;
-
-
public class SohuActivity extends Activity {
-
private static final String TAG = "SohuActivity";
-
private TextView textView;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.sohu);
-
-
textView = (TextView) this.findViewById(R.id.sohu);
-
try {
-
String html = NetTool.getHtml("", "GBK");
-
textView.setText(html);
-
} catch (Exception e) {
-
Log.e(TAG, e.toString());
-
Toast.makeText(SohuActivity.this, "获取网页数据失败", Toast.LENGTH_SHORT)
-
.show();
-
}
-
}
-
}
org.wp.net.utils
NetTool
Java代码
- package org.wp.net.utils;
-
-
import java.io.ByteArrayOutputStream;
-
import java.io.InputStream;
-
import java.net.HttpURLConnection;
-
import java.net.InetSocketAddress;
-
import java.net.Proxy;
-
import java.net.URL;
-
-
public class NetTool {
-
-
/**
-
* 获取给定Url路径的数据
-
*
-
* @param urlpath Url路径
-
* @return
-
* @throws Exception
-
*/
-
public static byte[] getImage(String urlpath) throws Exception {
-
URL url = new URL(urlpath);
-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
conn.setRequestMethod("GET");
-
conn.setConnectTimeout(6 * 1000);
-
if (conn.getResponseCode() == 200) {
-
InputStream inputStream = conn.getInputStream();
-
return readStream(inputStream);
-
}
-
return null;
-
}
-
-
/**
-
* 使用代理
-
*
-
* @param urlpath
-
* @return
-
* @throws Exception
-
*/
-
public static byte[] getImageByProxy(String urlpath) throws Exception {
-
URL url = new URL(urlpath);
-
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
-
"124.207.99.60", 8080));
-
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
-
conn.setRequestMethod("GET");
-
conn.setConnectTimeout(6 * 1000);
-
if (conn.getResponseCode() == 200) {
-
InputStream inputStream = conn.getInputStream();
-
return readStream(inputStream);
-
}
-
return null;
-
}
-
-
/**
-
* 获取url路径指定的网页代码
-
*
-
* @param urlpath url路径
-
* @return
-
* @throws Exception
-
*/
-
public static String getHtml(String urlpath, String encoding)
-
throws Exception {
-
URL url = new URL(urlpath);
-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
conn.setConnectTimeout(6 * 1000);
-
conn.setRequestMethod("GET");
-
if (conn.getResponseCode() == 200) {
-
InputStream inputStream = conn.getInputStream();
-
byte[] data = readStream(inputStream);
-
return new String(data, encoding);
-
} else {
-
throw new RuntimeException("请求url失败");
-
}
-
}
-
-
/**
-
* 读取数据
-
*
-
* @param inputStream 输入流
-
* @return
-
* @throws Exception
-
*/
-
public static byte[] readStream(InputStream inputStream) throws Exception {
-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-
byte[] buffer = new byte[1024];
-
int len = -1;
-
while ((len = inputStream.read(buffer)) != -1) {
-
outputStream.write(buffer, 0, len);
-
}
-
outputStream.close();
-
inputStream.close();
-
return outputStream.toByteArray();
-
}
-
}
main.xml
Xml代码
- <?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android=""
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/path"
-
/>
-
<EditText
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text=""
-
android:id="@+id/imagepath"
-
/>
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="@string/button"
-
android:id="@+id/button"
-
/>
-
<ImageView
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:id="@+id/imageview"
-
/>
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="获取sohu网页内容"
-
android:id="@+id/sohuButton"
-
/>
-
</LinearLayout>
sohu.xml
Xml代码
- <?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android=""
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<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/sohu"
-
/>
-
</ScrollView>
-
</LinearLayout>
strings.xml
Xml代码
- <?xml version="1.0" encoding="utf-8"?>
-
<resources>
-
<string name="hello">Hello World, DataActivity!</string>
-
<string name="app_name">从Internet获取数据</string>
-
<string name="path">图片路径</string>
-
<string name="button">获取图片</string>
-
</resources>
AndroidManifest.xml
Xml代码
- <?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android=""
-
package="org.wp.net.activity" android:versionCode="1"
-
android:versionName="1.0">
-
<application android:icon="@drawable/icon" android:label="@string/app_name">
-
<activity android:name=".DataActivity" android:label="@string/app_name">
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</activity>
-
<activity android:name=".SohuActivity" android:label="sohu网页" />
-
</application>
-
<uses-sdk android:minSdkVersion="7" />
-
<!-- 访问Internet权限 -->
-
<uses-permission android:name="android.permission.INTERNET" />
-
</manifest>
阅读(962) | 评论(0) | 转发(0) |