本文和取网络图片实理方式差不多,只不过一个是把byte[]转成Bitmap,一个是转String,再就是显示源码的时候需要滚动条,即要用到ScrollView,在Android里面,基本上控件都要设置android:layout_width和android:layout_height否则很可能会无法安装app到手机或者虚拟机,废话少说,直接上代码。
步骤一:配置清单文件Manifest.xml,主要是允许访问网络
在application节点前面加上<uses-permission android:name="android.permission.INTERNET"/>
步骤二:配置要用到的文字,即配置string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">获取网页源码</string>
<string name="hello_world">获取网页源码</string>
<string name="action_settings">Settings</string>
<string name="btntxt">获取源码</string>
<string name="titletips">请输入网址</string>
<string name="errtips">获取源码失败</string>
<string name="defurl">http://www.163.com</string>
</resources>
步骤三:配置Activity.xml
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.getnetpagecode.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/titletips"
/>
<EditText
android:id="@+id/txturlpath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/defurl"
/>
<Button
android:id="@+id/btnget"
android:text="@string/btntxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/codeview"
/>
</ScrollView>
</LinearLayout>
步骤四:编写网络访问方法
package com.example.getnetpagecode.Services;
import java.net.*;
import java.io.*;
import android.os.StrictMode;
public class NetService {
/**
* 获取一个网页的源码
* @param url:要获取源码的网址
* @return
* @throws Exception
*/
public static String GetCodeFromWebpage(String url) throws Exception
{
String code="";
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
URL u=new URL(url);
HttpURLConnection conn=(HttpURLConnection)u.openConnection();
conn.setReadTimeout(8000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200)
{
InputStream instream=conn.getInputStream();
byte[] barr=GetByteFromSteam(instream);
code=new String(barr,"utf-8");
}
conn.disconnect();
return code;
}
/**
* 将流转成byte[]
* @param stream
* @return
* @throws Exception
*/
public static byte[] GetByteFromSteam(InputStream stream) throws Exception{
ByteArrayOutputStream bas=new ByteArrayOutputStream();
int len=0;
byte[] barr=new byte[1024];
while((len=stream.read(barr))>-1){
bas.write(barr,0,len);
}
stream.close();
return bas.toByteArray();
}
}
步骤五:在activity后台调用
package com.example.getnetpagecode;
import android.app.Activity;
import android.widget.*;
import android.os.Bundle;
import android.view.*;
import com.example.getnetpagecode.Services.*;
public class MainActivity extends Activity {
private Button btnget=null;
private EditText txtpath=null;
private TextView codeview=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnget=(Button)this.findViewById(R.id.btnget);
txtpath=(EditText)this.findViewById(R.id.txturlpath);
codeview=(TextView)this.findViewById(R.id.codeview);
if(btnget!=null)
btnget.setOnClickListener(new ButtonOnClick());
}
private final class ButtonOnClick implements View.OnClickListener{
@Override
public void onClick(View v) {
String path=txtpath.getText().toString();
if(path==""){
Toast.makeText(getApplicationContext(), "请输入网址", Toast.LENGTH_LONG).show();
return;
}
try{
String code=NetService.GetCodeFromWebpage(path);
codeview.setText(code);
}catch(Exception ex){
Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}