Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- EditText username,password;
- String uname,pwd;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- username = (EditText) findViewById(R.id.username);
- password = (EditText) findViewById(R.id.password);
- }
- public void invokeLogin(View view){
- uname = username.getText().toString();
- pwd = password.getText().toString();
- login(uname, pwd);
- }
- public void login(final String uname, String pwd) {
- class loginAsync extends AsyncTask<String, Void, String> {
- private Dialog loadDialogue;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- loadDialogue = ProgressDialog.show(LoginActivity.this, "Please wait...", "Loading");
- }
- @Override
- protected String doInBackground(String... params) {
- JSONObject jsonObject;
- String result="";
- try {
- URL url = new URL("https://localhost/Android App/Login.php");
- jsonObject = new JSONObject();
- jsonObject.put("uname", params[0]);
- jsonObject.put("pwd", params[1]);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setReadTimeout(15000);
- httpURLConnection.setConnectTimeout(15000);
- httpURLConnection.setDoInput(true);
- httpURLConnection.setDoOutput(true);
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
- writer.write(getPostDataString(jsonObject));
- writer.flush();
- writer.close();
- outputStream.close();
- int responsecode = httpURLConnection.getResponseCode();
- if (responsecode == HttpURLConnection.HTTP_OK) {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
- StringBuilder sb = new StringBuilder(" ");
- String line;
- while ((line = bufferedReader.readLine()) != null) {
- sb.append(line);
- }
- bufferedReader.close();
- result = sb.toString();
- }
- else {
- return new String("false " + responsecode);
- }
- }
- catch (NullPointerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- @Override
- protected void onPostExecute(String result) {
- String s = result.trim();
- loadDialogue.dismiss();
- if(s.equalsIgnoreCase("success")){
- Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- finish();
- startActivity(intent);
- }else {
- Toast.makeText(getApplicationContext(), "Invalid User Name or Password", Toast.LENGTH_LONG).show();
- }
- }
- }
- }
- public String getPostDataString(JSONObject params) throws Exception {
- StringBuilder result = new StringBuilder();
- boolean first = true;
- Iterator<String> itr = params.keys();
- while (itr.hasNext()) {
- String key = itr.next();
- Object value = params.get(key);
- if (first)
- first = false;
- else {
- result.append("&");
- result.append(URLEncoder.encode(key, "UTF-8"));
- result.append("=");
- result.append(URLEncoder.encode(value.toString(), "UTF-8"));
- }
- }
- return result.toString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement