Android客户端与服务端交互之登陆示例

今天了解了一下android客户端与服务端是怎样交互的,发现其实跟web有点类似吧,然后网上找了大神的登陆示例,是基于IntentService的


1.后台使用简单的servlet,支持GET或POST。这个servlet最终返回给前台一个字符串flag,值是true或false,表示登录是否成功。

  servlet使用之前需要配置,主义servlet的servlet-name要和servlet-mapping的servlet-name一致,否则找不到路径

 我是在myEclipse上创建的一个web service 项目,然后部署到tomcat服务器上以便android客户端访问

[html]  view plain copy
  1. <servlet>  
  2.         <servlet-name>helloWorld</servlet-name>  
  3.         <servlet-class>com.zhongzhong.wap.action.HelloServlet</servlet-class>  
  4.     </servlet>  
  5.     <servlet-mapping>  
  6.         <servlet-name>helloWorld</servlet-name>  
  7.         <url-pattern>/queryOrder</url-pattern>  
  8.     </servlet-mapping>  


[java]  view plain copy
  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3.   
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. import javax.servlet.http.HttpSession;  
  9.   
  10. import com.zhongzhong.wap.bean.UserBean;  
  11.   
  12. public class HelloServlet extends HttpServlet {  
  13.   
  14.     @Override  
  15.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  16.             throws ServletException, IOException {  
  17.         doPost(req, resp);  
  18.     }  
  19.   
  20.     @Override  
  21.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  22.             throws ServletException, IOException {  
  23.           
  24.          resp.setContentType("text/html");    
  25.             PrintWriter out = resp.getWriter();    
  26.             Boolean flag = false;      
  27.             String userName = req.getParameter("un");    
  28.             String password = req.getParameter("pw");     
  29.             if(userName.equals("htp")&&password.equals("123"))  
  30.             {  
  31.                 flag = true;  
  32.             }  
  33.               
  34.             else flag = false;  
  35.             System.out.println("userName:"+userName+" password:"+password);  
  36.             out.print(flag);    
  37.             out.flush();    
  38.             out.close();   
  39.     }  
  40.   
  41. }  

2.然后我是在安卓的ADT上创建一个安卓项目, 建立两个Activity,分别作为登录界面和登录成功界面。

[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/textView1"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentTop="true"  
  16.         android:layout_centerHorizontal="true"  
  17.         android:layout_marginTop="40dp"  
  18.         android:text="HelloWorld登陆示例" />  
  19.   
  20.     <EditText  
  21.         android:id="@+id/et_user"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_below="@+id/textView1"  
  25.         android:layout_centerHorizontal="true"  
  26.         android:layout_marginTop="33dp"  
  27.         android:ems="10"  
  28.         android:hint="请输入账号" >  
  29.   
  30.         <requestFocus />  
  31.     </EditText>  
  32.   
  33.     <EditText  
  34.         android:id="@+id/et_psw"  
  35.         android:layout_width="wrap_content"  
  36.         android:layout_height="wrap_content"  
  37.         android:layout_below="@+id/et_user"  
  38.         android:layout_centerHorizontal="true"  
  39.         android:layout_marginTop="40dp"  
  40.         android:ems="10"  
  41.         android:hint="请输入密码"  
  42.         android:inputType="textPassword" />  
  43.   
  44.     <Button  
  45.         android:id="@+id/btn_login"  
  46.         android:layout_width="wrap_content"  
  47.         android:layout_height="wrap_content"  
  48.         android:layout_below="@+id/et_psw"  
  49.         android:layout_centerHorizontal="true"  
  50.         android:layout_marginTop="37dp"  
  51.         android:text="登陆" />  
  52.   
  53. </RelativeLayout>  

[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".NaviActivity" >  
  10.   
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_alignParentTop="true"  
  15.         android:layout_centerHorizontal="true"  
  16.         android:layout_marginTop="46dp"  
  17.         android:text="登陆成功" />  
  18.   
  19. </RelativeLayout>  


3.HTTP的访问公共类,用于处理GET和POST请求

[java]  view plain copy
  1. package com.example.logindemo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.NameValuePair;  
  9. import org.apache.http.client.HttpClient;  
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  11. import org.apache.http.client.methods.HttpGet;  
  12. import org.apache.http.client.methods.HttpPost;  
  13. import org.apache.http.impl.client.DefaultHttpClient;  
  14. import org.apache.http.message.BasicNameValuePair;  
  15. import org.apache.http.util.EntityUtils;  
  16.   
  17. import android.content.Entity;  
  18. import android.util.Log;  
  19.   
  20. public class HttpUtil {  
  21.     // 创建HttpClient对象  
  22.     public static HttpClient httpClient = new DefaultHttpClient();  
  23.     public static final String BASE_URL = "http://192.168.3.14:8090/HelloWord/";  
  24.   
  25.     /** 
  26.      *  
  27.      * @param url 
  28.      *            发送请求的URL 
  29.      * @return 服务器响应字符串 
  30.      * @throws Exception 
  31.      */  
  32.     public static String getRequest(String url) throws Exception {  
  33.         // 创建HttpGet对象。  
  34.         HttpGet get = new HttpGet(url);  
  35.         // 发送GET请求  
  36.         HttpResponse httpResponse = httpClient.execute(get);  
  37.         // 如果服务器成功地返回响应  
  38.         if (httpResponse.getStatusLine().getStatusCode() == 200) {  
  39.             // 获取服务器响应字符串  
  40.             String result = EntityUtils.toString(httpResponse.getEntity());  
  41.             return result;  
  42.         } else {  
  43.             Log.d("服务器响应代码", (new Integer(httpResponse.getStatusLine()  
  44.                     .getStatusCode())).toString());  
  45.             return null;  
  46.         }  
  47.     }  
  48.   
  49.     /** 
  50.      *  
  51.      * @param url 
  52.      *            发送请求的URL 
  53.      * @param params 
  54.      *            请求参数 
  55.      * @return 服务器响应字符串 
  56.      * @throws Exception 
  57.      */  
  58.     public static String postRequest(String url, Map<String, String> rawParams)  
  59.             throws Exception {  
  60.         // 创建HttpPost对象。  
  61.         HttpPost post = new HttpPost(url);  
  62.         // 如果传递参数个数比较多的话可以对传递的参数进行封装  
  63.         List<NameValuePair> params = new ArrayList<NameValuePair>();  
  64.         for (String key : rawParams.keySet()) {  
  65.             // 封装请求参数  
  66.             params.add(new BasicNameValuePair(key, rawParams.get(key)));  
  67.         }  
  68.         // 设置请求参数  
  69.         post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));  
  70.         // 发送POST请求  
  71.         HttpResponse httpResponse = httpClient.execute(post);  
  72.         // 如果服务器成功地返回响应  
  73.         if (httpResponse.getStatusLine().getStatusCode() == 200) {  
  74.             // 获取服务器响应字符串  
  75.             String result = EntityUtils.toString(httpResponse.getEntity());  
  76.             return result;  
  77.         }  
  78.         return null;  
  79.     }  
  80. }  

4. IntentService服务,用于在后台以队列方式处理耗时操作。

[java]  view plain copy
  1. package com.example.logindemo;  
  2.   
  3. import java.util.HashMap;  
  4.   
  5. import android.app.IntentService;  
  6. import android.content.Intent;  
  7. import android.util.Log;  
  8.   
  9. public class ConnectService extends IntentService {  
  10.     private static final String ACTION_RECV_MSG = "com.example.logindemo.action.RECEIVE_MESSAGE";  
  11.   
  12.     public ConnectService() {  
  13.         super("TestIntentService");  
  14.         // TODO Auto-generated constructor stub  
  15.     }  
  16.       
  17.     @Override  
  18.     protected void onHandleIntent(Intent intent) {  
  19.         // TODO Auto-generated method stub  
  20.         /**  
  21.          * 经测试,IntentService里面是可以进行耗时的操作的   
  22.          * IntentService使用队列的方式将请求的Intent加入队列,  
  23.          * 然后开启一个worker thread(线程)来处理队列中的Intent    
  24.          * 对于异步的startService请求,IntentService会处理完成一个之后再处理第二个    
  25.          */    
  26.         Boolean flag = false;    
  27.         //通过intent获取主线程传来的用户名和密码字符串    
  28.         String username = intent.getStringExtra("username");    
  29.         String password = intent.getStringExtra("password");    
  30.         flag = doLogin(username, password);    
  31.         Log.d("登录结果", flag.toString());    
  32.             
  33.         Intent broadcastIntent = new Intent();    
  34.         broadcastIntent.setAction(ACTION_RECV_MSG);      
  35.         broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);      
  36.         broadcastIntent.putExtra("result", flag.toString());    
  37.         sendBroadcast(broadcastIntent);    
  38.   
  39.     }  
  40.       
  41.      // 定义发送请求的方法    
  42.     private Boolean doLogin(String username, String password)    
  43.     {    
  44.         String strFlag = "";    
  45.         // 使用Map封装请求参数    
  46.         HashMap<String, String> map = new HashMap<String, String>();    
  47.         map.put("un", username);    
  48.         map.put("pw", password);    
  49.         // 定义发送请求的URL    
  50.       String url = HttpUtil.BASE_URL + "queryOrder?un=" + username + "&pw=" + password;  //GET方式    
  51.        // String url = HttpUtil.BASE_URL + "LoginServlet"; //POST方式    
  52.         Log.d("url", url);    
  53.         Log.d("username", username);    
  54.         Log.d("password", password);    
  55.         try {    
  56.             // 发送请求    
  57.             strFlag = HttpUtil.postRequest(url, map);  //POST方式    
  58. //          strFlag = HttpUtil.getRequest(url);  //GET方式    
  59.             Log.d("服务器返回值", strFlag);    
  60.         } catch (Exception e) {    
  61.             // TODO Auto-generated catch block    
  62.             e.printStackTrace();    
  63.         }    
  64.             
  65.         if(strFlag.trim().equals("true")){    
  66.             return true;    
  67.         }else{    
  68.             return false;    
  69.         }    
  70.             
  71.     }    
  72. }  

5。 在AndroidManifest.xml中注册IntentService。注意uses-permission节点,为程序开启访问网络的权限。

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.logindemo"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="18" />  
  10.   
  11.     <uses-permission android:name="android.permission.INTERNET" />  
  12.   
  13.     <application  
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name="com.example.logindemo.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.         <activity  
  28.             android:name="com.example.logindemo.NaviActivity"  
  29.             android:label="@string/title_activity_navi" >  
  30.         </activity>  
  31.   
  32.         <service android:name="com.example.logindemo.ConnectService" >  
  33.         </service>  
  34.     </application>  
  35.   
  36. </manifest>  

6.登陆界面处理,注意

  1. 按钮监听事件中,使用Intent将要传递的值传给service
  2. 接收广播类中,同样使用Intent将要传递的值传给下一个Activity。
  3. 在onCreate()中,动态注册接收广播类的实例receiver。
  4. 在接收广播类中,不要使用完毕后忘记注销接收器,否则会报一个Are you missing a call to unregisterReceiver()? 的异常。

[java]  view plain copy
  1. package com.example.logindemo;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.BroadcastReceiver;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.content.IntentFilter;  
  9. import android.util.Log;  
  10. import android.view.Menu;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15. import android.widget.Toast;  
  16.   
  17. public class MainActivity extends Activity {  
  18.      private static final String ACTION_RECV_MSG = "com.example.logindemo.action.RECEIVE_MESSAGE";  
  19.     private Button loginBtn;  
  20.     private EditText et_username;  
  21.     private EditText et_password;  
  22.     private String userName;  
  23.     private String passWord;  
  24.     private MessageReceiver receiver ;  
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_main);  
  29.         initView();  
  30.         //动态注册receiver      
  31.         IntentFilter filter = new IntentFilter(ACTION_RECV_MSG);      
  32.         filter.addCategory(Intent.CATEGORY_DEFAULT);      
  33.         receiver = new MessageReceiver();      
  34.         registerReceiver(receiver, filter);   
  35.     }  
  36.       
  37.     private void initView() {  
  38.         // TODO Auto-generated method stub  
  39.         et_username = (EditText)findViewById(R.id.et_user);  
  40.         et_password =( EditText)findViewById(R.id.et_psw);  
  41.         loginBtn = (Button)findViewById(R.id.btn_login);  
  42.         loginBtn.setOnClickListener(new OnClickListener() {  
  43.               
  44.             @Override  
  45.             public void onClick(View v) {  
  46.                 // TODO Auto-generated method stub  
  47.                 if(matchLoginMsg())  
  48.                 {  
  49.                     // 如果校验成功    
  50.                     Intent msgIntent = new Intent(MainActivity.this, ConnectService.class);    
  51.                     msgIntent.putExtra("username", et_username.getText().toString().trim());    
  52.                     msgIntent.putExtra("password", et_password.getText().toString().trim());    
  53.                     startService(msgIntent);   
  54.                 }  
  55.                   
  56.             }  
  57.         });  
  58.     }  
  59.       
  60.     protected boolean matchLoginMsg() {  
  61.         // TODO Auto-generated method stub  
  62.         userName = et_username.getText().toString().trim();  
  63.         passWord = et_password.getText().toString().trim();  
  64.         if(userName.equals(""))  
  65.         {  
  66.             Toast.makeText(MainActivity.this"账号不能为空",Toast.LENGTH_SHORT).show();  
  67.             return false;  
  68.         }  
  69.         if(passWord.equals(""))  
  70.         {  
  71.             Toast.makeText(MainActivity.this"密码不能为空",Toast.LENGTH_SHORT).show();  
  72.             return false;  
  73.         }  
  74.         return true;  
  75.     }  
  76.     //接收广播类      
  77.     public class MessageReceiver extends BroadcastReceiver {      
  78.         @Override      
  79.         public void onReceive(Context context, Intent intent) {      
  80.            String message = intent.getStringExtra("result");      
  81.            Log.i("MessageReceiver", message);    
  82.         // 如果登录成功    
  83.             if (message.equals("true")){    
  84.                 // 启动Main Activity    
  85.                 Intent nextIntent = new Intent(MainActivity.this, NaviActivity.class);    
  86.                 startActivity(nextIntent);    
  87.                 // 结束该Activity    
  88.                 finish();    
  89.                 //注销广播接收器    
  90.                 context.unregisterReceiver(this);    
  91.             }else{    
  92.                 Toast.makeText(MainActivity.this"用户名或密码错误,请重新输入!",Toast.LENGTH_SHORT).show();   
  93.             }    
  94.                 
  95.         }      
  96.     }      
  97.     @Override  
  98.     public boolean onCreateOptionsMenu(Menu menu) {  
  99.         // Inflate the menu; this adds items to the action bar if it is present.  
  100.         getMenuInflater().inflate(R.menu.main, menu);  
  101.         return true;  
  102.     }  
  103.   
  104. }  


运行截图:



转载自http://blog.csdn.net/softwave

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值