2012-05-14 38 views
0

我有一個用於登錄頁面的Web服務。但我想通過使用相同的Web服務登錄不同的用戶,如管理員和員工。如何通過JSON webservices登錄不同的用戶?

如果管理員登錄顯示一個活動A. 爲管理員: 公司:XXX 登錄ID:AAA 密碼:BBB

如果僱員登錄顯示另一個活動B. 公司:YYY 登錄ID:CCC 密碼:DDD

任何人都可以幫我..請問怎麼做?

謝謝。

回答

0

查看下面的代碼它可以幫助你。

package com.Barcode_Dynamic;

public class Login_activity extends Activity { 

    private EditText exit_user; 
    private EditText exit_pswd; 
    private Button btnlogin; 
    private Button btncancle; 
    private String Url_s = null; 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    boolean LOGIN_BOOL = false; 
    private Button btnBack; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 

     exit_user = (EditText) findViewById(R.id.edit_user); 
     exit_pswd = (EditText) findViewById(R.id.edit_pswd); 

     btnlogin = (Button) findViewById(R.id.btn_login); 
     btncancle = (Button) findViewById(R.id.btn_cancle); 

     btncancle.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       exit_user.setText(""); 
       exit_pswd.setText(""); 
      } 
     }); 



     btnlogin.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       final String USER = exit_user.getText().toString(); 
       final String PSWD = exit_pswd.getText().toString(); 
       if (USER.length() > 0 && PSWD.length() > 0) { 

        CharSequence contentTitle = getString(R.string.app_name); 
        final ProgressDialog progDailog = ProgressDialog.show(
          Login_activity.this, contentTitle, 
          "...", true); 

        final Handler handler = new Handler() { 
         @Override 
         public void handleMessage(Message msg) { 
          //showAlert("Import completed."); 
          if(LOGIN_BOOL) 
          { 
           showAlert("Login succes");        
           startActivity(new Intent(Login_activity.this, emailSettingFile.class)); 
          } 
         } 
        }; 

        new Thread() { 
         public void run() { 
          try { 

          nameValuePairs.add(new BasicNameValuePair("username", "" 
            + USER)); 
          nameValuePairs.add(new BasicNameValuePair("password", "" 
            + PSWD)); 
          InputStream is1 = call_Http(); 
          String str = getResult(is1); 
          if (str != null) { 
           meth_parse_json(str); 
          } 

          } catch (Exception e) { 
           // TODO: handle exception 

          } 
          handler.sendEmptyMessage(0); 
          progDailog.dismiss(); 
         } 

        }.start(); 



       } else { 
        showAlert("Please Enter Username & Password"); 

       } 

       startActi(); 
      } 
     }); 
    } 

    protected void startActi() { 
     // TODO Auto-generated method stub 
     if(LOGIN_BOOL) 
     { 

     } 
    } 

    private InputStream call_Http() { 
     InputStream is = null; 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(Url_s 
        + "/login_service.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse responce = httpclient.execute(httppost); 
      HttpEntity entity = responce.getEntity(); 
      is = entity.getContent(); 


     } catch (Exception e) { 
      // TODO: handle exception 

     } 

     return is; 
    } 

    private String getResult(InputStream is1) { 
     String result = null; 
     // TODO Auto-generated method stub 
     try { 
      BufferedReader bufr = new BufferedReader(new InputStreamReader(is1, 
        "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      sb.append(bufr.readLine() + "\n"); 
      String line = "0"; 
      while ((line = bufr.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 

      is1.close(); 
      result = sb.toString(); 

     } catch (Exception e) { 
      // TODO: handle exception 

     } 

     return result; 
    } 

    private void meth_parse_json(String str) { 
     // TODO Auto-generated method stub 
     Log.d("JSON :", "" + str); 
     try { 
      JSONObject job1 = new JSONObject(str); 

      // JSONArray details = job1.getJSONArray("details"); 
      String a = (String) job1.get("details"); 
      Log.d("JSON :", "" + a); 
      boolean bool = Boolean.parseBoolean(a); 
      if (bool) { 
       LOGIN_BOOL = true; 
      } else { 
       LOGIN_BOOL = false; 
      } 
     } catch (Exception e) { 
      // TODO: handle exception 

     } 
    } 
} 

上面的代碼爲android你必須創建Web服務的登錄。這只是如何做到這一點。

在meth_parse_json方法你必須檢查這是用於公司登錄或僱員登錄。

我假設你有mysql的知識。

如果這是幫助你,然後使其正確。

0

我想你有一個數據庫,你用你的web服務登錄。創建另一個名爲具有兩個字段的組或角色的表。對於表的SQL腳本可能類似於:

create table users (
    user_name   varchar(15) not null primary key, 
    user_pass   varchar(15) not null 
); 

create table user_roles (
    user_name   varchar(15) not null, 
    role_name   varchar(15) not null, 
    primary key (user_name, role_name) 
); 

現在只是獲取用戶擁有的角色,並決定他是否可以訪問活動與否。