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的值,获取网页编码方式
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;
}
}
}