通過開原始碼檢視網頁原始碼
阿新 • • 發佈:2019-02-13
1、首先實現介面的佈局
效果如下
程式碼佈局程式碼如下
佈局無難度,用textView中的字元傳遞頁面路徑。<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>
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;
}
}
}