android中查看网页源码
2014-04-25 15:53
363 查看
工具类
package cs.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamUtil {
public static byte [] read(InputStream inStream) throws IOException{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
int len = 0;
while((len = inStream.read(buffer))!=-1){
outStream.write(buffer);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
}
业务类
package cs.service;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import cs.util.StreamUtil;
public class HtmlService {
public static String gethtml(String path) throws IOException{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
while(conn.getResponseCode() == 200){
InputStream inStrem = conn.getInputStream();
byte [] html = StreamUtil.read(inStrem);
String data = new String (html,"UTF-8");
return data;
}
return null;
}
}
Activity
package com.example.htmlview;
import java.io.IOException;
import cs.service.HtmlService;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText htmlpath;
Button ok;
TextView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
htmlpath = (EditText) this.findViewById(R.id.htmlpath);
ok = (Button) this.findViewById(R.id.ok);
show = (TextView) this.findViewById(R.id.show);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String path = htmlpath.getText().toString().trim();
try {
String data = HtmlService.gethtml(path);
show.setText(data);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "读取失败", 1).show();
e.printStackTrace();
}
}
});
}
}
package cs.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamUtil {
public static byte [] read(InputStream inStream) throws IOException{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
int len = 0;
while((len = inStream.read(buffer))!=-1){
outStream.write(buffer);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
}
业务类
package cs.service;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import cs.util.StreamUtil;
public class HtmlService {
public static String gethtml(String path) throws IOException{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
while(conn.getResponseCode() == 200){
InputStream inStrem = conn.getInputStream();
byte [] html = StreamUtil.read(inStrem);
String data = new String (html,"UTF-8");
return data;
}
return null;
}
}
Activity
package com.example.htmlview;
import java.io.IOException;
import cs.service.HtmlService;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText htmlpath;
Button ok;
TextView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
htmlpath = (EditText) this.findViewById(R.id.htmlpath);
ok = (Button) this.findViewById(R.id.ok);
show = (TextView) this.findViewById(R.id.show);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String path = htmlpath.getText().toString().trim();
try {
String data = HtmlService.gethtml(path);
show.setText(data);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "读取失败", 1).show();
e.printStackTrace();
}
}
});
}
}
相关文章推荐
- android将给定图片维持宽高比缩放后,截取正中间的正方形部分
- [Android] SurfaceView使用实例
- Android不能调用java.awt的原因及解决办法和思考
- Android音乐播放器
- android studio中使用第三方工程
- android中获取json数据
- Android 获取SIM卡运营商 .
- android ,点击title的ImageView弹出PopupWindow
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- android 图片横向滚动效果
- AndroidStudio 导入Maven项目作为jar文件使用
- Android中Webview使用自定义的javascript进行回调
- android Buton使用
- android onTouch()与onTouchEvent()的区别
- Android和JavaScript互相调用
- Android布局实现圆角边框
- android 输入法界面显示的开关
- Android开发学习之TabView选项卡详解 -- 基于Android4.4
- Android开发相关文章--
- Android 极光推送基本步骤