This article describes the Android Internet access picture and how it is displayed on the client side. Share to everyone for your reference, specific as follows:
1. Layout interface
<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 "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= ". Mainactivity "> <edittext android:id=" @+id/url_text "android:layout_width=" Wrap_content "Android:layout_heigh" t= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparentright= "true" Android:layout_
Alignparenttop= "true" android:ems= "ten" android:inputtype= "Textpostaladdress" android:text= "@string/url_text" >
<requestfocus/> </EditText> <button android:id= "@+id/btn_text" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignleft= "@+id/url_text "android:layout_below=" @+id/url_text "android:layout_margintop=" 32DP "android:onclick=" SendHttp "Android:tex" t= "@string/btn_text"/> <imageview android:id= "@+id/iv_ie" android:layout_width= "Wrap_content" android:layout _height= "Wrap_content" android:layout_alignparentbottom= "true" android:layout_alignparentleft= "true" Android:
layout_alignright= "@+id/url_text" android:layout_below= "@+id/btn_text" android:src= "@drawable/ic_launcher"/>
</RelativeLayout>
2. Some types of seals
Encapsulation of URLs:
Package com.example.lession08_code.utis;
Import Java.io.InputStream;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
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 ();
Sets the requested information Httpurlconnection.setrequestmethod ("get");
Sets whether the request timed out httpurlconnection.setconnecttimeout (5000); Determines whether the server responds to success if (Httpurlconnection.getresponsecode () ==200) {//Gets the input stream object of the response InputStream Is=httpurlconnection.getin
Putstream ();
Byte Data[]=streamtools.istodata (IS);
Converts a string content=new string (data);
Content encoding mode if (Content.contains ("gb2312")) {content=new String (data, "gb2312");
}//Disconnect httpurlconnection.disconnect ();
}catch (Exception e) {e.printstacktrace ();
} return content; } public StatiC Bitmap sendgets (String path) {Bitmap bitmap=null;
try{//Set access URL URL url=new url (path);
Open Request HttpURLConnection httpurlconnection= (httpurlconnection) url.openconnection ();
Sets the requested information Httpurlconnection.setrequestmethod ("get");
Sets whether the request timed out httpurlconnection.setconnecttimeout (5000); Determines whether the server responds to success if (Httpurlconnection.getresponsecode () ==200) {//Gets the input stream object of the response InputStream Is=httpurlconnection.getin
Putstream ();
Change the flow of is directly to bitmap object Bitmap=bitmapfactory.decodestream (IS);
//Disconnect httpurlconnection.disconnect ();
}catch (Exception e) {e.printstacktrace ();
return bitmap;
}
}
Encapsulate class that determines whether a network is connected
Package com.example.lession08_code.utis;
Import Android.app.AlertDialog;
Import Android.content.ComponentName;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import android.content.Intent;
Import Android.net.ConnectivityManager;
Import Android.net.NetworkInfo;
Import Android.widget.Toast;
public class Networkutils {private context context;
Network link Management Object public Connectivitymanager Connectivitymanager;
Public Networkutils {this.context = context;
Gets the object of the network link Connectivitymanager = (connectivitymanager) context. Getsystemservice (Context.connectivity_service);
public Boolean Setactivenetwork () {Boolean flag=false;
Gets the available Network link object Networkinfo networkinfo = Connectivitymanager.getactivenetworkinfo ();
if (Networkinfo = null) {new Alertdialog.builder (context). Settitle ("Network not Available"). Setmessage ("Can set network?") . Setpositivebutton ("Confirm", 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);
Implementation Intent context.startactivity (intent); }). Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public
void OnClick (Dialoginterface dialog, int which) {}}). Show ()//Must be. Show ();
} if (networkinfo!=null) {flag=true;
return flag;
}
}
Encapsulation class for output stream
Package com.example.lession08_code.utis;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
public class Streamtools {public
static byte[] Istodata (InputStream is) throws ioexception{
/byte output stream
Bytearrayoutputstream bops=new Bytearrayoutputstream ();
Buffer
byte buffer[]=new byte[1024] reading the data;
Read the length of the record
int len=0;
while ((Len=is.read (buffer))!=-1) {
bops.write (buffer, 0, Len);
}
Converts the read content to byte array
byte Data[]=bops.tobytearray ();
return data;
}
Note: There are additional permissions issues to be added here
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission Android : Name= "Android.permission.INTERNET"/>
I hope this article will help you with the Android program.