05_android entry _ GET (display the content returned by the server on the Control)

Source: Internet
Author: User

After clicking login, how can I write the data returned from the server to the specified control ?, How does one implement NYI on android? We will analyze and implement the Code below, hoping to help you learn about android. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + z8LD5s2ouf20 + signature + Ci8vIMn5w/e/2Lz + signature/Uyr63tbvYyv2 + 3b/itcS/2Lz + signature + signature/HIodPDu6fD + signature/rbUz/M8YnI + signature/Signature/rbUz/M8YnI + signature/b7dtcS/2Lz + signature + Cn08YnI + signature + Ciogzai5/ WFuZHJvaWQ6b25DbGljaz0 = "login" specifies the method, this method is required to accept the parameter v of the control object you click
*
* @ Param v
*/
Public void login (View v ){
// Obtain the click Control id
Int id = v. getId ();
// Determine how to process based on the id
Switch (id ){
// Handle Login Events
Case R. id. btn_login:
// Obtain the user name
Final String userName = et_name.getText (). toString ();
// Obtain the User Password
Final String userPass = et_pass.getText (). toString ();
If (TextUtils. isEmpty (userName) | TextUtils. isEmpty (userPass )){
Toast. makeText (this, "the user name or password cannot be blank", Toast. LENGTH_LONG). show ();
} Else {
System. out
. Println ("---------------------- send the request to the server ----------------------");
// Access the network (a network permission is required) // Android: name = "android. permission. INTERNET"/>
// Access the network (time-consuming operations) to avoid blocking the main thread (UI). A new sub-thread must be enabled for processing.
New Thread (){
Public void run (){
// Call the loginByGet Method
LoginByGet (userName, userPass );
};
}. Start ();
}
Break;
Default:
Break;
}

}


/**
* GET requests
*
* @ Param userName
* @ Param userPass
*/
Public void loginByGet (String userName, String userPass ){


Try {
// Set the request address through URLEncoder. encode (String s, String enc)
// Use the specified encoding mechanism to convert the string to the application/x-www-form-urlencoded format
String spec = "http: // 172.16.237.200: 8080/video/login. do? Username ="
+ URLEncoder. encode (userName, "UTF-8") + "& userpass ="
+ URLEncoder. encode (userPass, "UTF-8 ");
// Create a URL object based on the address (url for network access)
URL url = new URL (spec );
// Url. openConnection () Open the network link
HttpURLConnection urlConnection = (HttpURLConnection) url
. OpenConnection ();
UrlConnection. setRequestMethod ("GET"); // you can specify the request method.
UrlConnection. setReadTimeout (5000); // set the timeout time.
UrlConnection. setConnectTimeout (5000); // sets the connection timeout time.
// Set the Request Header
UrlConnection
. SetRequestProperty ("User-Agent ",
"Mozilla/5.0 (Windows NT 6.3; WOW64; rv: 27.0) Gecko/20100101 Firefox/27.0 ");
// Obtain the response Status Code 404 200 505 302
If (urlConnection. getResponseCode () = 200 ){
// Obtain the response input stream object
InputStream is = urlConnection. getInputStream ();


// Create a byte output stream object
ByteArrayOutputStream OS = new ByteArrayOutputStream ();
// Define the read Length
Int len = 0;
// Define the buffer
Byte buffer [] = new byte [1024];
// Read cyclically Based on the buffer size
While (len = is. read (buffer ))! =-1 ){
// Write data to the OS object based on the read Length
OS. write (buffer, 0, len );
}
// Release resources
Is. close ();
OS. close ();
// Return a string
String result = new String (OS. toByteArray ());
System. out. println ("***************" + result + "***************** *");

// Write the returned data on the control.
TV _result.setText (result );
} Else {
System. out. println ("------------------ link failed -----------------");
}
} Catch (Exception e ){
E. printStackTrace ();
}
}

}

By adding red code, you will find the following bug:

05-24 06:38:37.987: W/System.err(1170): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.05-24 06:38:37.997: W/System.err(1170): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)05-24 06:38:37.997: W/System.err(1170): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:824)05-24 06:38:38.007: W/System.err(1170): at android.view.View.requestLayout(View.java:16431)05-24 06:38:38.007: W/System.err(1170): at android.view.View.requestLayout(View.java:16431)05-24 06:38:38.007: W/System.err(1170): at android.view.View.requestLayout(View.java:16431)05-24 06:38:38.007: W/System.err(1170): at android.view.View.requestLayout(View.java:16431)05-24 06:38:38.007: W/System.err(1170): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)05-24 06:38:38.007: W/System.err(1170): at android.view.View.requestLayout(View.java:16431)05-24 06:38:38.007: W/System.err(1170): at android.widget.TextView.checkForRelayout(TextView.java:6600)05-24 06:38:38.007: W/System.err(1170): at android.widget.TextView.setText(TextView.java:3813)05-24 06:38:38.007: W/System.err(1170): at android.widget.TextView.setText(TextView.java:3671)05-24 06:38:38.017: W/System.err(1170): at android.widget.TextView.setText(TextView.java:3646)05-24 06:38:38.017: W/System.err(1170): at www.csdn.net.lesson03.LoginActivity.loginByGet(LoginActivity.java:134)05-24 06:38:38.027: W/System.err(1170): at www.csdn.net.lesson03.LoginActivity$1.run(LoginActivity.java:67)

The cause of the error is:

Only the view level created by the original thread can touch its control. Therefore, the content of the returned data control can be set only in the main thread.

So how can we achieve the effect? We can use the runOnUiThread (action); method provided by Activity to implement it. Then we can set the content of the return data control in this method. The specific code is as follows:

// Returns the final String result = new String (OS. toByteArray (); System. out. println ("***************" + result + "***************** *"); loginActivity. this. runOnUiThread (new Runnable () {// use this method to modify the content of the UI control in the main thread @ Overridepublic void run () {// write the returned data here on the control. What will happen to ntv_result.settext (result );}});

The test results are as follows:

The above code is for reference only. If you have any questions, please leave a message.

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.