In Android, you can enter a URL in the address bar to browse the source code of the address page and access the network.

Source: Internet
Author: User


First, implement a simple layout: Copy codeThe Code is as follows: <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>

In StirngCopy codeThe Code is as follows: <string name = "url_text"> http://luochuang.iteye.com/blog/1606231 </string>
<String name = "btn_text"> browse </string>
<String name = "ie_text"> display webpage content </string>

Create a class file:

First, the MainActivity code:Copy codeThe Code is as follows: public class MainActivity extends Activity {
// Declare controls
Public EditText et_url;
Public TextView TV _ie;
// Network Operations
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 );
// Obtain the et_url object
Et_url = (EditText) findViewById (R. id. et_url );
TV _ie = (TextView) findViewById (R. id. TV _ie );
// Instantiate
NetWorkUtils = new NetWorkUtils (this );
// Instantiate this handler
Handler = new Handler (){
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
Switch (msg. what ){
Case TEXT:
TV _ie.setText (content); // sets the displayed text.
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 ){
// Set the network
Boolean flag = netWorkUtils. setActiveNetWork ();
If (flag ){
// After the run method is executed, this thread consumes
// Subthread
New Thread (new Runnable (){
@ Override
Public void run (){
Send ();
Handler. sendEmptyMessage (TEXT );
}
}). Start ();
}
}
// Send request operation
@ SuppressLint ("NewApi ")
Public void send (){

// Obtain the path of the url
String path = et_url.getText (). toString ();
If (path. isEmpty ()){
Toast. makeText (MainActivity. this, "the accessed url cannot be blank ",
Toast. LENGTH_LONG). show ();
} Else {
Content = HttpUtils. sendGet (path );
}

/* // Set the network
NetWorkUtils. setActiveNetWork ();
// Obtain the path of the url
String path = et_url.getText (). toString ();
If (path. isEmpty ()){
Toast. makeText (MainActivity. this, "the accessed url cannot be blank ",
Toast. LENGTH_LONG). show ();
} Else {
Try {
// Set the access url
URL url = new URL (path );
// Open the request
HttpURLConnection httpURLConnection = (HttpURLConnection) url
. OpenConnection ();
// Set Request Information
HttpURLConnection. setRequestMethod ("GET ");
// Determine whether the server response is successful
If (httpURLConnection. getResponseCode () = 200 ){
// Obtain the response input stream object
InputStream is = httpURLConnection. getInputStream ();
// Byte output stream
ByteArrayOutputStream bops = new ByteArrayOutputStream ();
// Cache for Data Reading
Byte buffer [] = new byte [1024];
// Read the length record
Int len = 0;
// Read cyclically
While (len = is. read (buffer ))! =-1 ){
Bops. write (buffer, 0, len );
}
// Convert the read content to a byte array
Byte data [] = bops. toByteArray ();
// Convert to a string
Content = new String (data );
} Else {
Toast. makeText (MainActivity. this, "server response error ",
Toast. LENGTH_LONG). show ();
}
} Catch (MalformedURLException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}*/
}
}

Copy codeThe Code is as follows: public class HttpUtils {

Public static String sendGet (String path ){
String content = null;
Try {
// Set the access url
URL url = new URL (path );
// Open the request
HttpURLConnection httpURLConnection = (HttpURLConnection) url
. OpenConnection ();
// Set Request Information
HttpURLConnection. setRequestMethod ("GET ");
// Set whether the request has timed out.
HttpURLConnection. setConnectTimeout (5000 );
// Determine whether the server response is successful
If (httpURLConnection. getResponseCode () = 200 ){
// Obtain the response input stream object
InputStream is = httpURLConnection. getInputStream ();
Byte data [] = StreamTools. isToData (is );
// Convert to a string
Content = new String (data );
// Content Encoding Method
If (content. contains ("gb2312 ")){
Content = new String (data, "gb2312 ");
}
}
// Disconnect
HttpURLConnection. disconnect ();
} Catch (MalformedURLException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return content;
}
}

Copy codeThe Code is as follows: public class StreamTools {
Public static byte [] isToData (InputStream is) throws IOException {

// Byte output stream
ByteArrayOutputStream bops = new ByteArrayOutputStream ();
// Cache for Data Reading
Byte buffer [] = new byte [1024];
// Read the length record
Int len = 0;
// Read cyclically
While (len = is. read (buffer ))! =-1 ){
Bops. write (buffer, 0, len );
}
// Convert the read content to a byte array
Byte data [] = bops. toByteArray ();

Return data;

}
}

Copy codeThe Code is as follows: public class NetWorkUtils {
Private Context context;
// Network link management object
Public ConnectivityManager connectivityManager;
Public NetWorkUtils (Context context ){
This. context = context;
// Obtain the object of the network link
ConnectivityManager = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
}
// Public void setActiveNetWork (){
Public boolean setActiveNetWork (){
Boolean flag = false;
// Obtain available network link objects
NetworkInfo networkInfo = connectivityManager. getActiveNetworkInfo ();
If (networkInfo = null ){
New AlertDialog. Builder (context)
. SetTitle ("Network unavailable ")
. SetMessage ("can I set the network? ")
. SetPositiveButton ("OK ",
New DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog,
Int which ){
Toast. makeText (context, "click OK ",
Toast. LENGTH_LONG). show ();
// Declare intent
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 );
// Execution intent
Context. startActivity (intent );
}
})
. SetNegativeButton ("cancel ",
New DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog,
Int which ){
}
// Required. show ();
}). Show ();
}
// Determine whether the Network is available
If (networkInfo! = Null ){
Flag = true;
}
Return flag;
}
}

Then add the network access permission to AndroidManifest. xml.Copy codeThe Code is as follows: <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
<Uses-permission android: name = "android. permission. INTERNET"/>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.