Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何通过JSON Web服务登录不同的用户?_Android_Json - Fatal编程技术网

Android 如何通过JSON Web服务登录不同的用户?

Android 如何通过JSON Web服务登录不同的用户?,android,json,Android,Json,我有一个用于登录页面的web服务。但我想通过使用相同的Web服务登录不同的用户,如Admin和Employee 如果管理员登录显示一个活动A。 对于管理员: 公司:xxx 登录ID:AAA 密码:BBB 如果员工登录显示另一个活动B。 公司:YYY 登录ID:CCC 密码:DDD 有人能帮我吗。。请问怎么做 谢谢。检查下面的代码,它可能会对您有所帮助 包装com.Barcode\u动态 public class Login_activity extends Activity { pri

我有一个用于登录页面的web服务。但我想通过使用相同的Web服务登录不同的用户,如Admin和Employee

如果管理员登录显示一个活动A。 对于管理员: 公司:xxx 登录ID:AAA 密码:BBB

如果员工登录显示另一个活动B。 公司:YYY 登录ID:CCC 密码:DDD

有人能帮我吗。。请问怎么做


谢谢。

检查下面的代码,它可能会对您有所帮助

包装com.Barcode\u动态

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

        }
    }
}
公共类登录\u活动扩展活动{
私有编辑文本退出用户;
私人编辑文本退出_pswd;
私人按钮;
私人按钮BTNCNCLE;
私有字符串Url_s=null;
ArrayList nameValuePairs=新的ArrayList();
boolean LOGIN\u BOOL=false;
私人按钮btnBack;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
exit_user=(EditText)findViewById(R.id.edit_user);
exit_pswd=(EditText)findViewById(R.id.edit_pswd);
btnlogin=(按钮)findViewById(R.id.btn\u登录);
BTNCNCLE=(按钮)findViewById(R.id.btn\u cancle);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
退出用户设置文本(“”);
退出pswd.setText(“”);
}
});
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
final String USER=exit_USER.getText().toString();
最后一个字符串PSWD=exit_PSWD.getText().toString();
if(USER.length()>0&&PSWD.length()>0){
CharSequence contentTitle=getString(R.string.app_name);
最终ProgressDialog progDailog=ProgressDialog.show(
登录\u activity.this,contentTitle,
“……”,对);
最终处理程序=新处理程序(){
@凌驾
公共无效handleMessage(消息消息消息){
//showAlert(“导入完成”);
如果(登录)
{
showAlert(“登录成功”);
startActivity(新意图(Login_activity.this、emailSettingFile.class));
}
}
};
新线程(){
公开募捐{
试一试{
nameValuePairs.add(新的BasicNameValuePairs(“用户名”)
+用户);
nameValuePairs.add(新的BasicNameValuePairs(“密码”)
+PSWD);
InputStream为1=调用_Http();
字符串str=getResult(is1);
如果(str!=null){
meth_parse_json(str);
}
}捕获(例外e){
//TODO:处理异常
}
handler.sendEmptyMessage(0);
progDailog.disclose();
}
}.start();
}否则{
showAlert(“请输入用户名和密码”);
}
startActi();
}
});
}
受保护的void startActi(){
//TODO自动生成的方法存根
如果(登录)
{
}
}
私有输入流调用_Http(){
InputStream=null;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(Url)
+“/login_service.php”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
//TODO:处理异常
}
回报是;
}
私有字符串getResult(InputStream为1){
字符串结果=null;
//TODO自动生成的方法存根
试一试{
BufferedReader bufr=新的BufferedReader(新的InputStreamReader(is1,
“iso-8859-1”),第8节);
StringBuilder sb=新的StringBuilder();
sb.append(bufr.readLine()+“\n”);
字符串行=“0”;
而((line=bufr.readLine())!=null){
sb.追加(第+行“\n”);
}
is1.close();
结果=sb.toString();
}捕获(例外e){
//TODO:处理异常
}
返回结果;
}
私有void meth_parse_json(字符串str){
//TODO自动生成的方法存根
Log.d(“JSON:”,“”+str);
试一试{
JSONObject job1=新JSONObject(str);
//JSONArray details=job1.getJSONArray(“details”);
字符串a=(字符串)job1.get(“详细信息”);
Log.d(“JSON:”,“+a”);
布尔布尔=boolean.parseBoolean(a);
如果(bool){
LOGIN\u BOOL=true;
}否则{
LOGIN\u BOOL=false;
}
}捕获(例外e){
//TODO:处理异常
}
}
}
以上android代码必须创建用于登录的web服务。这就是如何做到这一点的方法

在meth_parse_json方法中,您必须检查这是公司登录还是员工登录
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)
);