Apps for Android and Tomcat servers

Source: Internet
Author: User

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

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.