Simple configuration and use of Tomcat in Android

Source: Internet
Author: User
Tags dedicated server

Because learning Android has been a while, but in school, the server is a short board ah, no dedicated server to our students practiced hand, so only find a way. Of course, Tomcat is the choice of the two.

Read the information on the Internet, or feel that their records are better.

Because I studied Android, the JDK has been configured. If you don't know, see "Environment variable configuration in Java"

First we need to download tomcat:http://tomcat.apache.org/(please choose your own version), I use Tomcat 7.0.55.

I download down is a compressed package, choose a good path after the decompression. (This step is installed)

The extracted files have these:

Then, we double-click the Bin folder to find a bat file (under Windows system ha),

Double-click the file. (That is, start the Tomcat service)

If the startup is unsuccessful, the general situation is that the console comes out immediately and disappears, indicating that Tomcat did not find the Java Runtime environment. The environment variables for the JDK need to be configured.

If the startup succeeds, the following startup success information appears:

After successful launch, we open the browser, enter the http://localhost:8080/in the address bar, can see the following interface (input: http://127.0.0.1:8080 can also):

At this point, our tomcat was completely successful.

If you want to see the status of the service, you can click the ServiceStatus button:

At this point, a dialog will pop up to let you enter your account number and password, we will click Cancel First, then jump to another page:

The translation of the English, you can know, is to let us add the cof/tomcat-users.xml in the dotted box in the two lines of code. The user name and password at the Red line is required when logging in.

After landing the interface is as follows: (Basic configuration can be seen)

Below, let's use a simple example:

To be in the Apache-tomcat-7.0.55\webapps\root\mytest directory. A picture named Tomcat.png.

(Note: The root folder in the WebApps folder under Tomcat Decompression is the default deployment directory for Tomcat, MyTest is a new folder I created under the root folder)

Below, we do an Android image viewer:

First, the layout main.xml:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" >    <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Picture path" />        <EditTextAndroid:id= "@+id/et_path"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter picture path"/>        <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "Look"Android:text= "View"/>        <ImageViewAndroid:id= "@+id/iv"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:src= "@drawable/ic_launcher"Android:scaletype= "Centerinside"/></LinearLayout>

Then the mainactivity:

 PackageCn.itcast.imagelooker;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL;Importorg.apache.http.conn.ConnectTimeoutException;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText Et_path; PrivateImageView IV; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); Et_path=(EditText) Findviewbyid (R.id.et_path); IV=(ImageView) Findviewbyid (R.ID.IV); }     Public voidLook (View v) {String path=Et_path.gettext (). toString (); if(Path.equals ("") {Toast.maketext ( This, "The path to the picture is empty", 1). Show (); } Else {            Try {                //a different way//url url = new URL (path);//Bitmap Bitmap = null;//bitmap = Bitmapfactory.decodestream (Url.openstream ());//Iv.setimagebitmap (bitmap); //Build PathURL url =NewURL (path); // //Open ConnectionHttpURLConnection conn =(HttpURLConnection) url.openconnection (); //Request MethodConn.setrequestmethod ("GET"); //Timeout periodConn.setconnecttimeout (5000); Bitmap Bitmap=NULL; Bitmap=Bitmapfactory.decodestream (Url.openstream ()); if(Conn.getresponsecode () = = 200) {InputStream is=Conn.getinputstream (); //turn the flow into a pictureBitmap Bitmap1 =Bitmapfactory.decodestream (IS); if(Bitmap1 = =NULL) {Toast.maketext ( This, "gets the picture stream to be empty", 1). Show (); }Else{iv.setimagebitmap (BITMAP1); }                 }Else{Toast.maketext ( This, "The path entered does not exist", 1). Show (); }            }Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); if(Einstanceofmalformedurlexception) {Toast.maketext ( This, "Malformed Path entered", 1). Show (); } Else if(Einstanceofconnecttimeoutexception) {Toast.maketext ( This, "Connection Timeout error", 1). Show (); } Else if(EinstanceofIOException) {Toast.maketext ( This, "Get Data Error", 1). Show (); } Else{Toast.maketext ( This, "Unknown error", 1). Show (); }            }        }    }}

In addition, do not forget the rights of thousand outside:

<android:name= "Android.permission.INTERNET"/>

Run the program.

(When the IP address is entered correctly in the input box, click the button to view the picture named Tomcat.png.)

Note: Why is the address of the input Yes 100.70.132.251?

100.70.132.251 This address is the computer address obtained by Ipconfig. Unlike the Android Emulator (simulator), the genymotion with the Android emulator should be 10.0.2.2. 10.0.2.2 is a specific IP that is set by the simulator and is the alias of your computer, alias remembers, so use 10.0.2.2 on the emulator to access your computer natively. Remember that you cannot use both the localhost:8080 and 127.0.0.1 paths, because the Android emulator (simulator) uses it as localhost, which means that the code localhost or 127.0.0.1来 access, are all access to the simulator yourself!

The following is a blog post about the installation of Tomcat, which is more detailed than me.

Http://www.cnblogs.com/Johness/archive/2012/07/20/2600937.html

Simple configuration and use of Tomcat in Android

Related Article

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.