Android Getting Started notes-data storage-Network

Source: Internet
Author: User

Network as a kind of data storage of Android, so greatly expand the use of the app, because any information we can get from the network, imagine we build a server, and then through the app to the server to request data, then to modify the display data, We only need to modify the data on the server, the app is just a display carrier. At present, there are many apps developed in this way, within the app can be nested a browser, of course, you can also use Android WebView, then the development can be separated, Android to do the part of Android, Web site to do the part of the site, greatly improve the development efficiency, It also adds to the app's versatility and versatility, and is not great. Today, let's briefly introduce Android over the network to get data:


As a test, I built a Tomcat7.0 server on my own machine, very simple, build can refer to other blogs to build one, as the test is very convenient. A Web site project (WEB1) was created and a TXT file was placed in the Web site: android.txt. Then we see how to get and read its contents over the network.


These two files are equivalent to the files on the server:


Let's look at the Android code:

        Getnetdatabtn.setonclicklistener (New View.onclicklistener () {             @Override              public void OnClick (View v) {                try { & nbsp                  url url = new URL (                              " Http://192.168.1.23:8080/Web1/android.txt ");                    urlconnection urlconn = url.openconnection ();                     inputstream is = Urlconn.getinputstream ();                     byte[] buffer = new byte[is.available ()];                     is.read (buffer);                     string words = encodingutils.getstring (buffer, "UTF-8");                     log.e (TAG, "words:" + words);                 } catch (IOException e) {                    e.printstacktrace ();                }            }        });

Here is not posted full code, mainactivity inside a button, click the corresponding to get file data.

The results obtained:



Attention:

1. Http://192.168.1.23:8080/Web1/android.txt This address, 192.168.1.23 is my IP address in the LAN, This is not the case with Android simulator 127.0.0.1来 access to the server on this computer, because this address has been occupied by the Android emulator itself, so use the LAN IP address to access. 8080 is the port number, depending on your own server configuration.

2. When we turn bytes into a string, it is best to specify the encoding format, the usual practice is to use encodingutils to convert, UTF-8 is an encoding format, you can use GB2312 and so on, to see some of the data it. This format should be determined according to the format of this file on the server.

3. Access to the network in Android requires permission: <uses-permission android:name= "Android.permission.INTERNET"/>



This example is very simple, just to guide, and then do a detailed explanation!




Android Getting Started notes-data storage-Network

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.