First implement a simple layout:
Copy Code code 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 the STIRNG
Copy Code code as follows:
<string name= "Url_text" >http://luochuang.iteye.com/blog/1606231</string>
<string name= "Btn_text" > Browse </string>
<string name= "Ie_text" > Display browse Web content </string>
New class file:
First mainactivity the code:
Copy Code code as follows:
public class Mainactivity extends activity {
declaring controls
Public EditText Et_url;
Public TextView Tv_ie;
Network Operation class
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);
Get Et_url Object
Et_url = (edittext) Findviewbyid (R.id.et_url);
Tv_ie = (TextView) Findviewbyid (R.id.tv_ie);
Instantiation of
Networkutils = new Networkutils (this);
Instantiate this processor
Handler = new Handler () {
public void Handlemessage (msg) {
Super.handlemessage (msg);
Switch (msg.what) {
Case TEXT:
Tv_ie.settext (content);//Set Text to display
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 up a network
Boolean flag = Networkutils.setactivenetwork ();
if (flag) {
When the Run method finishes executing, the thread consumes the
Child threads
New Thread (New Runnable () {
@Override
public void Run () {
Send ();
Handler.sendemptymessage (TEXT);
}
). Start ();
}
}
Send Request action
@SuppressLint ("Newapi")
public void Send () {
Obtain the path path to the URL
String path = Et_url.gettext (). toString ();
if (Path.isempty ()) {
Toast.maketext (mainactivity.this, "Access URL address cannot be null",
Toast.length_long). Show ();
} else {
Content = Httputils.sendget (path);
}
/*//Set up network
Networkutils.setactivenetwork ();
Obtain the path path to the URL
String path = Et_url.gettext (). toString ();
if (Path.isempty ()) {
Toast.maketext (mainactivity.this, "Access URL address cannot be null",
Toast.length_long). Show ();
} else {
try {
Set the URL to access
URL url = new URL (path);
Open Request
HttpURLConnection httpurlconnection = (httpurlconnection) URL
. OpenConnection ();
Set the requested information
Httpurlconnection.setrequestmethod ("get");
Determine if the server is responding successfully
if (httpurlconnection.getresponsecode () = = 200) {
Gets the input stream object for the response
InputStream is = Httpurlconnection.getinputstream ();
BYTE output stream
Bytearrayoutputstream Bops = new Bytearrayoutputstream ();
Read cache of data
byte buffer[] = new byte[1024];
Read length record
int len = 0;
Looping read
while (len = is.read (buffer))!=-1) {
Bops.write (buffer, 0, Len);
}
Converts the read content into a byte array
byte data[] = Bops.tobytearray ();
Convert to 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 Code code as follows:
public class Httputils {
public static string Sendget (string path) {
String content = null;
try {
Set Access URL
URL url = new URL (path);
Open Request
HttpURLConnection httpurlconnection = (httpurlconnection) URL
. OpenConnection ();
Set the requested information
Httpurlconnection.setrequestmethod ("get");
Set whether the request is timed out
Httpurlconnection.setconnecttimeout (5000);
Determine if the server is responding successfully
if (httpurlconnection.getresponsecode () = = 200) {
Gets the input stream object for the response
InputStream is = Httpurlconnection.getinputstream ();
byte data[] = Streamtools.istodata (IS);
Convert to 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 Code code as follows:
public class Streamtools {
public static byte[] Istodata (InputStream is) throws ioexception{
BYTE output stream
Bytearrayoutputstream Bops = new Bytearrayoutputstream ();
Read cache of data
byte buffer[] = new byte[1024];
Read the length of the record
int len = 0;
Looping read
while (len = is.read (buffer))!=-1) {
Bops.write (buffer, 0, Len);
}
Converts the read content into a byte array
byte data[] = Bops.tobytearray ();
return data;
}
}
Copy Code code as follows:
public class Networkutils {
private context;
Network link Management Object
Public Connectivitymanager Connectivitymanager;
Public Networkutils {
This.context = context;
Get the object of a network link
Connectivitymanager = (Connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
}
public void Setactivenetwork () {
public Boolean setactivenetwork () {
Boolean flag = false;
Get the Available Network link objects
Networkinfo networkinfo = Connectivitymanager.getactivenetworkinfo ();
if (Networkinfo = = null) {
New Alertdialog.builder (context)
. Settitle ("Network Unavailable")
. Setmessage ("Can set up network?")
. Setpositivebutton ("Confirmation",
New Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog,
int which) {
Toast.maketext (Context, "Click OK",
Toast.length_long). Show ();
Declaration 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) {
}
Must be. Show ();
). Show ();
}
Determine if the network is available
if (networkinfo!=null) {
Flag=true;
}
return flag;
}
}
And then you add the right to access the network in Androidmanifest.xml.
Copy Code code as follows:
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_" State "/>
<uses-permission android:name=" Android.permission.INTERNET "/>