通过开源代码查看网页源代码
2014-05-25 21:13
218 查看
1、首先实现界面的布局
效果如下
代码布局代码如下<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<EditText
android:id="@+id/et_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri"
android:text="@string/http_url" />
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_url"
android:text="@string/btn_send"
android:onClick="sendHttpUrl" />
<ScrollView
android:id="@+id/sc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/btn_send" >
<TextView
android:id="@+id/tv_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tv_tip" />
</ScrollView>
</RelativeLayout>
布局无难度,用textView中的字符传递页面路径。
2、添加jar文件android-async-http-1.4.4.jar
3、创建监听事件sendHttpUrl
4、获取地址参数
5、创建客户端对象
AsyncHttpClient client = new AsyncHttpClient();
6、返回内容
String result = new String(responseBody,default_charset);//default_charset网页编码方式
7、设置控件文本
tv_text.setText(result);
通过这几步基本实现了获取网页源代码的功能
8、加强
通过获取Content-Type的值来获取网页的编码方式
//获取Content-Type的值 text/html; charset=utf-8
for (Header header : headers) {
<span style="white-space:pre"> </span>if (header.equals(contentType_value)) {
<span style="white-space:pre"> </span>// header.
<span style="white-space:pre"> </span>contentType_value = header.getValue();
}
}
Content-Type获取的值为“
text/html; charset=utf-8" 通过判断字符串中字符的“=”的位置确定网页编码的字符串,即index+1到contentType_value.length()。数组获取长度为length,字符串获取长度为length()。
package www.csdn.net.webcode;
import java.io.UnsupportedEncodingException;
import org.apache.http.Header;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_url;
private TextView tv_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_url = (EditText) findViewById(R.id.et_url);
tv_text = (TextView) findViewById(R.id.tv_code);
}
public void sendHttpUrl(View v) {
int i = v.getId();
switch (i) {
case R.id.btn_send:
String url = et_url.getText().toString();
if (TextUtils.isEmpty(url)) {
Toast.makeText(this, "地址不能为空", Toast.LENGTH_LONG).show();
} else {
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
// TODO Auto-generated method stub
String contentType_value = null;
//获取Content-Type的值 text/html; charset=utf-8
for (Header header : headers) {
if (header.equals(contentType_value)) {
// header.
contentType_value = header.getValue();
}
}
String default_charset = "UTF-8";
//处理Content-Type的值,获取网页编码方式
if (contentType_value != null) {
if (contentType_value.contains("=")) {
int index = contentType_value.indexOf("=");
default_charset = contentType_value.substring(
index + 1, contentType_value.length());
} else {
String result = new String(responseBody);
default_charset = getCharset(result);
}
} else {
String result = new String(responseBody);
default_charset = getCharset(result);
}
Toast.makeText(MainActivity.this,
"编码是" + default_charset, 1).show();
if (statusCode == 200) {
try {
String result = new String(responseBody,
default_charset);
tv_text.setText(result);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
} else {
System.out.println("发生错误");
}
}
private String getCharset(String result) {
// TODO Auto-generated method stub
String defaultCharset = null;
if (result != null) {
//content=\"text/html; charset=GBK\ 中间没有空格
if (result
.contains("content=\"text/html; charset=GBK\"")) {
defaultCharset = "GBK";
} else if (result
.contains("content=\"text/html; charset=UTF-8\"")) {
defaultCharset = "UTF-8";
} else if (result
.contains("content=\"text/html; charset=gb2312\"")) {
defaultCharset = "gb2312";
} else if (result.contains("charset=\"UTF-8\"")) {
defaultCharset = "UTF-8";
} else if (result.contains("charset=\"GBK\"")) {
defaultCharset = "GBK";
}
}
return defaultCharset;
}
});
}
break;
}
}
}
效果如下
代码布局代码如下<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<EditText
android:id="@+id/et_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri"
android:text="@string/http_url" />
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_url"
android:text="@string/btn_send"
android:onClick="sendHttpUrl" />
<ScrollView
android:id="@+id/sc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/btn_send" >
<TextView
android:id="@+id/tv_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tv_tip" />
</ScrollView>
</RelativeLayout>
布局无难度,用textView中的字符传递页面路径。
2、添加jar文件android-async-http-1.4.4.jar
3、创建监听事件sendHttpUrl
4、获取地址参数
5、创建客户端对象
AsyncHttpClient client = new AsyncHttpClient();
6、返回内容
String result = new String(responseBody,default_charset);//default_charset网页编码方式
7、设置控件文本
tv_text.setText(result);
通过这几步基本实现了获取网页源代码的功能
8、加强
通过获取Content-Type的值来获取网页的编码方式
//获取Content-Type的值 text/html; charset=utf-8
for (Header header : headers) {
<span style="white-space:pre"> </span>if (header.equals(contentType_value)) {
<span style="white-space:pre"> </span>// header.
<span style="white-space:pre"> </span>contentType_value = header.getValue();
}
}
Content-Type获取的值为“
text/html; charset=utf-8" 通过判断字符串中字符的“=”的位置确定网页编码的字符串,即index+1到contentType_value.length()。数组获取长度为length,字符串获取长度为length()。
//处理Content-Type的值,获取网页编码方式 if (contentType_value != null) { if (contentType_value.contains("=")) { int index = contentType_value.indexOf("="); default_charset = contentType_value.substring( index + 1, contentType_value.length()); } else { String result = new String(responseBody); default_charset = getCharset(result); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} else { String result = new String(responseBody); <span style="white-space:pre"> </span>default_charset = getCharset(result); }判断不同的编码方式
private String getCharset(String result) { // TODO Auto-generated method stub String defaultCharset = null; if (result != null) { //content=\"text/html; charset=GBK\ 中间没有空格 if (result.contains("content=\"text/html; charset=GBK\"")) { defaultCharset = "GBK"; } else if (result.contains("content=\"text/html; charset=UTF-8\"")) { defaultCharset = "UTF-8"; } else if (result.contains("content=\"text/html; charset=gb2312\"")) { defaultCharset = "gb2312"; } else if (result.contains("charset=\"UTF-8\"")) { defaultCharset = "UTF-8"; } else if (result.contains("charset=\"GBK\"")) { <span style="white-space:pre"> </span>defaultCharset = "GBK"; } } return defaultCharset; }最后代码如下
package www.csdn.net.webcode;
import java.io.UnsupportedEncodingException;
import org.apache.http.Header;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_url;
private TextView tv_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_url = (EditText) findViewById(R.id.et_url);
tv_text = (TextView) findViewById(R.id.tv_code);
}
public void sendHttpUrl(View v) {
int i = v.getId();
switch (i) {
case R.id.btn_send:
String url = et_url.getText().toString();
if (TextUtils.isEmpty(url)) {
Toast.makeText(this, "地址不能为空", Toast.LENGTH_LONG).show();
} else {
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
// TODO Auto-generated method stub
String contentType_value = null;
//获取Content-Type的值 text/html; charset=utf-8
for (Header header : headers) {
if (header.equals(contentType_value)) {
// header.
contentType_value = header.getValue();
}
}
String default_charset = "UTF-8";
//处理Content-Type的值,获取网页编码方式
if (contentType_value != null) {
if (contentType_value.contains("=")) {
int index = contentType_value.indexOf("=");
default_charset = contentType_value.substring(
index + 1, contentType_value.length());
} else {
String result = new String(responseBody);
default_charset = getCharset(result);
}
} else {
String result = new String(responseBody);
default_charset = getCharset(result);
}
Toast.makeText(MainActivity.this,
"编码是" + default_charset, 1).show();
if (statusCode == 200) {
try {
String result = new String(responseBody,
default_charset);
tv_text.setText(result);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
} else {
System.out.println("发生错误");
}
}
private String getCharset(String result) {
// TODO Auto-generated method stub
String defaultCharset = null;
if (result != null) {
//content=\"text/html; charset=GBK\ 中间没有空格
if (result
.contains("content=\"text/html; charset=GBK\"")) {
defaultCharset = "GBK";
} else if (result
.contains("content=\"text/html; charset=UTF-8\"")) {
defaultCharset = "UTF-8";
} else if (result
.contains("content=\"text/html; charset=gb2312\"")) {
defaultCharset = "gb2312";
} else if (result.contains("charset=\"UTF-8\"")) {
defaultCharset = "UTF-8";
} else if (result.contains("charset=\"GBK\"")) {
defaultCharset = "GBK";
}
}
return defaultCharset;
}
});
}
break;
}
}
}
相关文章推荐
- 右键无法查看网页源代码修复方法
- 轻松查看网页源代码-BlazingTool Instant Source软件的使用
- 如何查看被禁止的网页源代码
- 如何禁止查看网页源代码(转)
- 禁止查看网页源代码全攻略
- 查看网页源代码终极方法
- 关于查看网页源文件不显示源代码(打开的是桌面文件夹)的问题
- JS实现禁止查看网页源代码方法总结
- 用view-source:查看网页源代码
- 修改网页查看源代码的编辑器
- tomcat ssl问题 弹出 “即将通过安全连接查看网页“然后就没有下文了
- 方法汇总 禁止查看网页源代码全攻略
- 如何实现通过右键查看源代码来查看jsp页面的名字
- 使用Firefox通过cmwap浏览www及wap网页查看,调试
- 用JS实现禁止查看网页源代码方法总结
- 查看网页源代码
- 查看网页源代码--VB
- 采用MASM32编写的查看网页源代码程序——HttpRead,文件大小仅为 5632 字节
- 运用JavaScript禁止别人查看网页的源代码
- 改IE,fire fox默认查看网页源代码程序