According to the 08_android _ android-async-http open source project introduction and usage introduction, we will introduce the usage of related classes in the android-async-http open source project through the most common login cases. I hope it will help you learn about the android-async-http open source project.
1. Continue to use 03_android entry _ use RelativeLayout to implement the layout file of this case on the login interface
2. The server-side Code adopts the 04_android entry _ HttpURLConnection GET method to implement server code snippets in the login case (5. On the server, I only write a Servlet to process the corresponding requests)
3. reference the android-async-http open source project in the Application
Method 1: Find the source code library \ src \ main \ java com package of the downloaded file and paste it to the src directory in the project.
Method 2: Put the latest jar package under android-async-http-master \ releases under the application to libs. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140526/2014052609121518.png" alt = "\">
4. LoginActivity Code (the code is annotated in detail. Please read it carefully)
Package com. example. lesson03; import org. apache. http. header; import com. loopj. android. http. asyncHttpClient; import com. loopj. android. http. asyncHttpResponseHandler; import com. loopj. android. http. requestParams; import android. app. activity; import android. OS. bundle; import android. text. textUtils; import android. view. view; import android. widget. editText; import android. widget. textView; import android. widget. toast; public class LoginActivity extends Activity {// declare the control private EditText et_name, et_pass; private TextView TV _result; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the control object et_name = (EditText) findViewById (R. id. et_name); et_pass = (EditText) findViewById (R. id. et_pass); TV _result = (TextView) findViewById (R. id. TV _result);}/*** click the event triggered by the button control * @ param v */public void login (View v) {// obtain the control's idint id = v. getId (); // handle switch (id) {case R. id. btn_login: // obtain the control text String userName = et_name.getText (). toString (); // username String userPass = et_pass.getText (). toString (); // User Password // determine whether the user name and password are empty if (TextUtils. isEmpty (userName. trim () | TextUtils. isEmpty (userPass. trim () {Toast. makeText (this, "the user name or password cannot be blank", Toast. LENGTH_LONG ). show ();} else {// send a request to the server // call: loginByAsyncHttpClientPost (userName, userPass); loginByAsyncHttpClientGet (userName, userPass);} break ;}} /*** Post implementation using AsyncHttpClient * @ param userName * @ param userPass */public void encode (String userName, String userPass) {AsyncHttpClient client = new AsyncHttpClient (); // create the client object String url of the asynchronous request = "http: // 172.16.237.200: 8080/video/login. do "; // define the request address // create the encapsulated object RequestParams params of the Request Parameters = new RequestParams (); params. put ("username", userName); // set the request parameter name and parameter value params. put ("userpass", userPass); // set the request parameter name and parameter // execute the post method client. post (url, params, new AsyncHttpResponseHandler () {/*** Method for successful processing * statusCode: response status code; headers: Corresponding header information such as response time, response server; * responseBody: the byte of the response content */@ Overridepublic void onSuccess (int statusCode, Header [] headers, byte [] responseBody) {if (statusCode = 200) {TV _result.setText (new String (responseBody); // you can specify the displayed text.}/*** error: error message of failed response is encapsulated in this exception object */@ Overridepublic void onFailure (int statusCode, Header [] headers, byte [] responseBody, Throwable error) {error. printStackTrace (); // print the error message }});} /*** use AsyncHttpClient's Get method to implement * @ param userName * @ param userPass */public void loginByAsyncHttpClientGet (String userName, String userPass) {// create an asynchronous client object AsyncHttpClient client = new AsyncHttpClient (); // request url String url = "http: // 172.16.237.200: 8080/video/login. do "; // create the encapsulated object RequestParams params of Request Parameters = new RequestParams (); params. put ("username", userName); // set the request parameter name and parameter value params. put ("userpass", userPass); // set the request parameter name and parameter // The url address parameter when sending the get request. The anonymous callback object is the client. get (url, params, new AsyncHttpResponseHandler () {@ Overridepublic void onSuccess (int statusCode, Header [] headers, byte [] responseBody) {// Method System for successful processing. out. println ("statusCode -----------------" + statusCode); // traverses the header information for (int I = 0; I
5. The program running result is as follows: