Put a picture in the Tomcat root directory ... webapps\root\people,jpg, using cell phone and server interaction J to get this picture, the desktop layout is simple linear layout, code:
<textview
Android:textsize= "25DP"
android:text= "Server picture address:"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<edittext
Android:id= "@+id/et_pct"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"/>
<button
Android:id= "@+id/btn_search"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"/>
<imageview
android:id= "@+id/iv_pic"
android:layout_width= "match_parent"
android:layout_height= " Match_parent "/>
by entering the server address, click the button to see the picture of Tomcat:
Main code:
Handler mhandler = new Handler () {
public void Handlemessage (Android.os.Message msg) {
Ivpic.setimagebitmap (MBITMAP1);
}
};
private void Initconnnect () {
New Thread (New Runnable () {
@Override
public void Run () {
String Url_path = Etpct.gettext (). toString (). Trim ();
if (Textutils.isempty (Url_path)) {
Toast.maketext (Mainactivity.this, "Please enter the image URL", Toast.length_short). Show ();
}else{
try {
URL url = new URL (url_path);
HttpURLConnection URLConnection = (httpurlconnection) url.openconnection ();
Urlconnection.setrequestmethod ("GET");
Urlconnection.setconnecttimeout (5000);
if (Urlconnection.getresponsecode () ==200) {
InputStream InputStream = Urlconnection.getinputstream ();
MBitmap1 = Bitmapfactory.decodestream (InputStream);
if (MBITMAP1 = = null) {
Toast.maketext (mainactivity.this, "Picture get Error", Toast.length_short). Show ();
} else {
Mhandler.sendemptymessage (0);
}
}
} catch (Exception e) {
E.printstacktrace ();
}
}
}
}). Start ();
}
Click the button trigger Initconnnect () This method, because the request server is more time-consuming, turn on the child thread to make the request.
Finally, don't forget to join the privilege: <uses-permission android:name= "Android.permission.INTERNET"/>
If you still cannot access, it is your computer firewall opened, the firewall shut down, you can. :
Note: IP address to use the computer IP address, can not be used 10.0.2.2.
。 This allows for simple access to the server image.
If you refer to the Xutils framework, the code is more convenient:
Mbitmaputils = new Bitmaputils (mainactivity.this);
String Url_path = Etpct.gettext (). toString (). Trim ();
Mbitmaputils.display (Ivpic,url_path);
This allows for access.
However, referencing xutils requires permission to join:
<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
Introduce this library on the line:
You can do it. is not still feel xutils more convenient.
Apps for Android and Tomcat servers