根据08_android入门_android-async-http开源项目介绍及使用方法的介绍,我们通过最常见的登陆案例进行介绍android-async-http开源项目中有关类的使用.希望对你学习android-async-http开源项目有所帮助.
1.继续采用03_android入门_采用RelativeLayout实现登陆界面为该案例的布局文件
2.服务器端的代码采用04_android入门_采用HttpURLConnection的GET方式实现登陆案例中的服务器代码片段(5.关于服务器中我仅写一个Servlet进行处理相应的请求处理)
3.在应用中引用android-async-http开源项目
方法一:找到下载文件的源码library\src\main\java的com包黏贴到项目中的src目录下
方法二:把android-async-http-master\releases下面最新的jar包放到应用到libs下
4.LoginActivity的代码(代码中详细加了注释,请仔细阅读)
- 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 {
-
-
- private EditText et_name, et_pass;
- private TextView tv_result;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
-
- et_name = (EditText) findViewById(R.id.et_name);
- et_pass = (EditText) findViewById(R.id.et_pass);
- tv_result = (TextView) findViewById(R.id.tv_result);
- }
-
-
- public void login(View v) {
-
- int id = v.getId();
-
- switch (id) {
- case R.id.btn_login:
-
- String userName = et_name.getText().toString();
- String userPass = et_pass.getText().toString();
-
- if (TextUtils.isEmpty(userName.trim())
- || TextUtils.isEmpty(userPass.trim())) {
- Toast.makeText(this, "用户名或者密码不能为空", Toast.LENGTH_LONG).show();
- } else {
-
-
- loginByAsyncHttpClientGet(userName, userPass);
- }
- break;
- }
- }
-
-
- public void loginByAsyncHttpClientPost(String userName, String userPass) {
- AsyncHttpClient client = new AsyncHttpClient();
- String url = "http://172.16.237.200:8080/video/login.do"; // 定义请求的地址
-
- RequestParams params = new RequestParams();
- params.put("username", userName);
- params.put("userpass", userPass);
-
- client.post(url, params, new AsyncHttpResponseHandler() {
-
- @Override
- public void onSuccess(int statusCode, Header[] headers,
- byte[] responseBody) {
- if (statusCode == 200) {
- tv_result.setText(new String(responseBody));
- }
- }
-
-
- @Override
- public void onFailure(int statusCode, Header[] headers,
- byte[] responseBody, Throwable error) {
- error.printStackTrace();
- }
- });
- }
-
-
- public void loginByAsyncHttpClientGet(String userName, String userPass) {
-
- AsyncHttpClient client = new AsyncHttpClient();
-
- String url = "http://172.16.237.200:8080/video/login.do";
-
- RequestParams params = new RequestParams();
- params.put("username", userName);
- params.put("userpass", userPass);
-
-
- client.get(url, params,new AsyncHttpResponseHandler() {
- @Override
- public void onSuccess(int statusCode, Header[] headers,
- byte[] responseBody) {
-
- System.out
- .println("statusCode-------------------" + statusCode);
-
- for (int i = 0; i < headers.length; i++) {
- Header header = headers[i];
- System.out.println("header------------Name:"
- + header.getName() + ",--Value:"
- + header.getValue());
- }
-
- tv_result.setText(new String(responseBody));
- }
-
- @Override
- public void onFailure(int statusCode, Header[] headers,
- byte[] responseBody, Throwable error) {
-
- error.printStackTrace();
- }
- });
- }
-
- }
5.程序运行结果如下图: