Zhao Yazhi _android http GET request

Source: Internet
Author: User

<!--Add Network access rights--
<uses-permission android:name= "Android.permission.INTERNET"/>


Layout file Activity_login.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "wrap_content "android:layout_height=" wrap_content "> <textview android:id=" @+id/username "Android:layout_wid Th= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "20sp" android:layout_alignbaseline= "@+id /editusername "android:text=" @string/username "/> <edittext android:id=" @+id/editusername "Androi        D:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "true" android:layout_torightof= "@+id/username" android:hint= "@string/edit_username"/> <text View android:id= "@+id/password" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android Oid:textsize= "20SP" android:layout_below= "@+id/username" android:layout_alignbaseline= "@+id/editpass" Android: text= "@string/password"/> <edittext ANdroid:id= "@+id/editpass" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an Droid:layout_alignparentright= "true" android:layout_torightof= "@+id/password" android:layout_below= "@+id/edi        Tusername "android:hint=" @string/edit_password "/> <button android:id=" @+id/btn_login "        Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "80DP"         android:layout_below= "@+id/password" android:layout_marginleft= "50DP" android:text= "@string/btn_sure" android:onclick= "Login"/> <checkbox android:id= "@+id/checkbox" Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "50DP" Android:la Yout_alignbaseline= "@+id/btn_login" android:layout_below= "@+id/editpass" android:layout_torightof= "@+id/btn_ Login "Android:text= "@string/checkbox"/> <textview android:id= "@+id/tv_result" android:layout_width= "Wrap_ Content "android:layout_height=" Wrap_content "android:layout_alignparentbottom=" true "android:text=" @  String/tip_result "/></relativelayout>


String.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "App_name" >lesson03</ string>    <string name= "Hello_world" >hello world!</string>    <string name= "Action_settings" >Settings</string>    <string name= "username" > User name </string><string name= "password" > password </string>   <string name= "Edit_username" > Please enter user name </string><string name= "Edit_password" > Please enter password </string> <string name= "btn_sure" > OK </string>    <string name= "checkbox" > Remember password < /string>        <string name= "Tip_result" > Data returned by the server </string></resources>


Flow character Tool class Streamtools.java

Package Com.example.util;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.inputstream;public class Streamtools {/** * Convert the stream into a string * @param is * @return */public static string Streamtostr (Inpu TStream is) {The output stream of the try {//byte bytearrayoutputstream OS = new Bytearrayoutputstream ();//define read length int len = 0;//define buffer byte buffer[ ] = new byte[1024];//is read from the input stream and written to the OS object while (len = is.read (buffer) = =-1) {os.write (buffer, 0, Len);} Close stream Is.close (); Os.close ();//write to the byte stream return new String (Os.tobytearray ());} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace (); return null;}}}


Activity Class Loginactivity.java


Package Com.example.android_http;import Java.io.inputstream;import Java.net.httpurlconnection;import Java.net.URL; Import Android.app.activity;import android.os.bundle;import Android.text.textutils;import Android.view.Menu;import Android.view.view;import Android.widget.edittext;import Android.widget.toast;import Com.example.util.StreamTools; public class Loginactivity extends Activity {private EditText et_username;private EditText et_password;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_login); Findview ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.login, menu); return true;} public void Findview () {Et_password = (EditText) Findviewbyid (r.id.editpass); et_username = (EditText) Findviewbyid ( R.id.editusername);} public void Login (View v) {//Get the Idint ID of the Click control = V.getid ();//Determine by ID switch (ID) {CAse r.id.btn_login://for login action//Toast.maketext (Getapplicationcontext (), "--", 0). Show ();//Get the username password final String userName = Et_username.gettext (). ToString (), final String userpass = Et_password.gettext (). ToString ();//Determine if NULL if ( Textutils.isempty (userName) | | Textutils.isempty (Userpass)) {Toast.maketext (Getapplicationcontext (), "User name or password cannot be empty", 0). Show ();} else { Toast.maketext (Getapplicationcontext (), "Send and Seek service", 0). Show ();//access network (requires a network of permissions)//access network (good operation) sub-thread, avoid blocking the main thread ui//http:/ /172.16.237.200:8080/video/login.do?username=chj&userpass=123//all time-consuming operations are to be in the thread of new threads () {public void run () { try {//request address String spec = "Http://172.16.237.200:8080/video/login.do?username=" + username + "&userpass=" + userpass ;//Create URL object (Network access URL) URL url = new URL (spec) based on address,//Connection object opened with HTTP protocol httpurlconnection URLConnection = (httpurlconnection) Url.openconnection (); Urlconnection.setrequestmethod ("get");//Make Request Urlconnection.setreadtimeout (5000) in get mode;// Set timeout urlconnection.setconnecttimeout (5000);//Set Connection Timeout URLCONNECTION.SEtrequestproperty ("User-agent", "mozilla/5.0" (Windows NT 6.3; WOW64; rv:27.0) gecko/20100101 firefox/27.0 ");//Get the corresponding code, 505 302if (urlconnection.getresponsecode () = = 200) {//Get the network back The input stream InputStream is = Urlconnection.getinputstream (); String result = Streamtools.streamtostr (IS); SYSTEM.OUT.PRINTLN ("The data returned is:" + result);} else {System.out.println ("Request URL failed");}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}};}. Start ();} Break;default:break;}}}



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.