In the previous article "how to configure 163 mailbox in Android", we can use the following example:
The business logic is as follows: In the example, a text box is used to simulate user information. When the program exits, it stores the text file information and forwards it to the mail sending page, let the user Save the information to the mailbox. The Code is as follows:
Save the sent information to your mailbox
Package com. Terry;
Import Android. App. activity;
Import Android. content. intent;
Import android.net. Uri;
Import Android. OS. Bundle;
Import Android. View. keyevent;
Import Android. widget. edittext;
Public class networkactivity extends activity {
Private edittext myedittext;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Myedittext = (edittext) findviewbyid (R. Id. myedittext4 );
}
@ Override
Public Boolean onkeydown (INT keycode, keyevent event ){
// Todo auto-generated method stub
If (keycode = keyevent. keycode_back)
{
// Sending email address
Uri uri = URI. parse ("mailto: 529782485@qq.com ");
// Create an intent
Intent it = new intent (intent. action_sendto, Uri );
It. putextra (Android. content. Intent. extra_subject, "Network Storage ");
It. putextra (Android. content. Intent. extra_text, myedittext. gettext ());
Startactivity (it );
This. Finish ();
Return true;
}
Return super. onkeydown (keycode, event );
}
}
Extension
Since data is sent and saved to the network, the data on the network can also be obtained. Below we will use a small example to load a novel program with a TXT file from the network.
The logic code is as follows: first create a URL address that we want to access, then retrieve the linked file, and read the data cyclically. The Code is as follows:
Read XML code reference
Package com. Terry;
Import java. Io. bufferedinputstream;
Import java. Io. inputstream;
Import java.net. url;
Import java.net. urlconnection;
Import org. Apache. http. util. bytearraybuffer;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. textview;
Public class readxmlactivity extends activity {
@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. readxml );
Textview TV = (textview) findviewbyid (R. Id. textview01 );
String MSG = "";
Try {
URL url = new URL ("http://www.az1314.com/txt/00.txt"); // the data file to be accessed
Urlconnection con = URL. openconnection ();
Inputstream is = con. getinputstream ();
Bufferedinputstream Bis = new bufferedinputstream (is );
Bytearraybuffer BAF = new bytes arraybuffer (100 );
Int current = 0;
While (current = bis. Read ())! =-1 ){
BAF. append (byte) Current );
}
MSG = new string (BAF. tobytearray ());
} Catch (exception e ){
// Todo: handle exception
MSG = E. getmessage ();
}
TV. settext (MSG );
}
}
Method description:
There are six URL construction methods:
URL (string spec)
Create a new URL instance through passed string analysis.
URL (URL context, string spec)
Create a new URL instance through passed string analysis. A URL parameter is required.
URL (URL context, string spec, urlstreamhandler handler)
Create a new URL instance through passed string Analysis
URL (string protocol, string host, string file)
To create a URL instance using the given parameters, you must specify the protocol and host file name.
URL (string protocol, string host, int port, string file)
To create a URL instance with the specified parameters, you must specify the protocol, host, port, and file name.
URL (string protocol, string host, int port, string file, urlstreamhandler handler)
To create a URL instance with the specified parameters, you must specify the protocol, host, port, file name, and handler.
Urlconnection introduction:
It provides a Connection source with a specific protocol class.
Getinputstream is mainly used to read the resource data of a urlconnection and returns an inputstream. This article will use this method to obtain data
Urlconnection is not described here. If you are interested, you can check the SDK. Run the following code:
Because it runs in the simulator. Garbled characters appear. If you have a real machine, try it on the real machine.
Tip: You need to grant permissions in the configuration file to access the external network. If you do not have permissions, the following interface will appear:
The permission code is as follows:
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Activity Android: Name = ". networkactivity"
Android: Label = "@ string/app_name">
</Activity>
<Activity Android: Name = ". readxmlactivity"> <intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter> </activity>
</Application>
<Uses-Permission Android: Name = "android. Permission. Internet"/>
So far, the four types of data storage are complete.
References: http://httpwww.wokoko.com/bbs/thread-1923-1-1.html