Android 에 서 는 주소 표시 줄 에 웹 주 소 를 입력 하면 이 주소 웹 페이지 의 원본 코드 를 탐색 하고 네트워크 에 접근 할 수 있 습 니 다.

 먼저 간단 한 레이아웃 구현:
 
<EditText
android:id="@+id/et_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:text="@string/url_text" >
<requestFocus android:layout_width="match_parent" />
</EditText>
<Button
android:id="@+id/btn_ie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/et_url"
android:onClick="sendHttp"
android:text="@string/btn_text" />
<ScrollView
android:id="@+id/sv_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btn_ie" >
<TextView
android:id="@+id/tv_ie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ie_text" />
</ScrollView>
Stirng 에서
 
<string name="url_text">http://luochuang.iteye.com/blog/1606231</string>
<string name="btn_text"> </string>
<string name="ie_text"> </string>
새 클래스 파일: 우선 MainActivity 에서 코드:
 
public class MainActivity extends Activity {
//
public EditText et_url;
public TextView tv_ie;
//
public NetWorkUtils netWorkUtils;
private Handler handler;
public String content;
public static final int TEXT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// et_url
et_url = (EditText) findViewById(R.id.et_url);
tv_ie = (TextView) findViewById(R.id.tv_ie);
//
netWorkUtils = new NetWorkUtils(this);
//
handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case TEXT:
tv_ie.setText(content);//
break;
default:
break;
}
}
};
}
@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;
}
public void sendHttp(View v) {
//
boolean flag = netWorkUtils.setActiveNetWork();
if (flag) {
// run
//
new Thread(new Runnable() {
@Override
public void run() {
send();
handler.sendEmptyMessage(TEXT);
}
}).start();
}
}
//
@SuppressLint("NewApi")
public void send() {

// url path
String path = et_url.getText().toString();
if (path.isEmpty()) {
Toast.makeText(MainActivity.this, " url ",
Toast.LENGTH_LONG).show();
} else {
content = HttpUtils.sendGet(path);
}



/*//
netWorkUtils.setActiveNetWork();
// url path
String path = et_url.getText().toString();
if (path.isEmpty()) {
Toast.makeText(MainActivity.this, " url ",
Toast.LENGTH_LONG).show();
} else {
try {
// url
URL url = new URL(path);
//
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
//
httpURLConnection.setRequestMethod("GET");
//
if (httpURLConnection.getResponseCode() == 200) {
//
InputStream is = httpURLConnection.getInputStream();
//
ByteArrayOutputStream bops = new ByteArrayOutputStream();
//
byte buffer[] = new byte[1024];
//
int len = 0;
//
while ((len = is.read(buffer)) != -1) {
bops.write(buffer, 0, len);
}
// byte
byte data[] = bops.toByteArray();
//
content = new String(data);
} else {
Toast.makeText(MainActivity.this, " ",
Toast.LENGTH_LONG).show();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}*/
}
}
 
public class HttpUtils {


public static String sendGet(String path) {
String content = null;
try {
// url
URL url = new URL(path);
//
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
//
httpURLConnection.setRequestMethod("GET");
//
httpURLConnection.setConnectTimeout(5000);
//
if (httpURLConnection.getResponseCode() == 200) {
//
InputStream is = httpURLConnection.getInputStream();
byte data[] = StreamTools.isToData(is);
//
content = new String(data);
//
if (content.contains("gb2312")) {
content = new String(data, "gb2312");
}
}
//
httpURLConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
}
 
public class StreamTools {
public static byte[] isToData(InputStream is) throws IOException{

//
ByteArrayOutputStream bops = new ByteArrayOutputStream();
//
byte buffer[] = new byte[1024];
//
int len = 0;
//
while((len = is.read(buffer)) != -1){
bops.write(buffer, 0, len);
}
// byte
byte data[] = bops.toByteArray();

return data;

}
}
 
public class NetWorkUtils {
private Context context;
//
public ConnectivityManager connectivityManager;
public NetWorkUtils(Context context) {
this.context = context;
//
connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
}
//public void setActiveNetWork() {
public boolean setActiveNetWork() {
boolean flag = false;
//
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) {
new AlertDialog.Builder(context)
.setTitle(" ")
.setMessage(" ?")
.setPositiveButton(" ",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(context, " ",
Toast.LENGTH_LONG).show();
//
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory("android.intent.category.LAUNCHER");
intent.setComponent(new ComponentName(
"com.android.settings",
"com.android.settings.Settings"));
intent.setFlags(0x10200000);
//
context.startActivity(intent);
}
})
.setNegativeButton(" ",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
// .show();
}).show();
}
//
if(networkInfo!=null){
flag=true;
}
return flag;
}
}
그리고 AndroidManifest.xml 에 네트워크 에 접근 할 수 있 는 권한 을 추가 해 야 합 니 다

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

좋은 웹페이지 즐겨찾기