如何基于具有restful API的用户角色创建活动?

时间:2018-02-26 10:41:21

标签: android api android-asynctask restful-url role

我正在创建一个基于在线的Android应用,该应用会显示来自不同类型用户的通知,例如“管理员”,“用户”。当我登录 admin 类和布局,或者“用户”时,它会显示另一个布局,并且应该根据其角色打开活动屏幕。我会用意图打开不同的活动。如何从restful api中检查用户的角色?

这是我的代码

公共类LoginActivity扩展了AppCompatActivity {

private EditText username, password;
String json_string ;
private Context context;
String Response;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    SessionManager session = new SessionManager(getApplicationContext());
    HashMap<String, String> user = session.getUserDetails();
    String userId = user.get("userId").toString();
    String categoryId = user.get("catId").toString();
    String categoryType = user.get("catType").toString();
    String batchId= user.get("batchId").toString();

    Intent intent = getIntent();

    Bundle intentBundle = intent.getExtras();

    final String loggedUser = intentBundle.getString("username");
    final String loggedUser1 = intentBundle.getString("password");

    EditText loginUsername = (EditText) findViewById(R.id.email);
    EditText loginPassword = (EditText) findViewById(R.id.password);

    loginUsername.setText(loggedUser);

    Button UserAccount = (Button)findViewById(R.id.btnLogin);

    UserAccount.setOnClickListener(new View.OnClickListener()   {
        public void onClick(View v)  {
            new BackgroundTask(getBaseContext()).execute(loggedUser);
        }
    });
}


public class BackgroundTask extends AsyncTask<String, Void, String> {
    Context context;
    AlertDialog alertDialog;

    public BackgroundTask(Context userContext) {
        this.context = userContext;
    }

    String JSON_STRING;
    String result;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {


        if (android.os.Debug.isDebuggerConnected())
            android.os.Debug.waitForDebugger();

        String json_url = "#";
        try {
            String username = params[0];
            String password = params[1];

            URL url = new URL(json_url);

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");

            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);

            OutputStream outputStream = httpURLConnection.getOutputStream();

            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

            String post_data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");

            bufferedWriter.write(post_data);

            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

            StringBuilder stringBuilder = new StringBuilder();

            while ((JSON_STRING = bufferedReader.readLine()) != null) {

                stringBuilder.append(JSON_STRING + "\n");

            }

            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();

            result = stringBuilder.toString();
            return result;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }
        return null;
    }
    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
    @Override
    protected void onPostExecute(String result) {

        json_string = result.replace("\"", "");

        if (json_string.trim().contentEquals("admin")) {
            Intent adminIntent = new Intent(LoginActivity.this, WelcomeActivity.class);
            startActivity(adminIntent);

        } else if (json_string.trim().contentEquals("user")) {
            Intent userIntent = new Intent(LoginActivity.this, HotelScreen.class);
            startActivity(userIntent);
        }
    }
}

}

0 个答案:

没有答案