Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.nsu.visitorsys">
- <uses-permission android:name="android.permission.INTERNET"></uses-permission>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme"
- android:usesCleartextTraffic="true">
- <activity android:name=".loginactivity">
- <intent-filter >
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".menuactivity">
- </activity>
- </application>
- </manifest>
- --inside loginactivity
- --declarations
- public static final int CONNECTION_TIMEOUT=10000;
- public static final int READ_TIMEOUT=150000;
- --put this inside loginactivity, inside the class ,outside the oncreate
- --change the ip address as well in the below code
- private class AsyncLogin extends AsyncTask<String, String, String>
- {
- ProgressDialog pdLoading = new ProgressDialog(loginactivity.this);
- HttpURLConnection conn;
- URL url = null;
- @Override
- protected void onPreExecute()
- {
- super.onPreExecute();
- pdLoading.setMessage("\tLoading...");
- pdLoading.setCancelable(false);
- pdLoading.show();
- }
- @Override
- protected String doInBackground(String... params)
- {
- try
- {
- url = new URL("http://172.25.101.48/NUSVisitorWeb/login.php");
- }
- catch (MalformedURLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return "exception";
- }
- try
- {
- conn = (HttpURLConnection)url.openConnection();
- conn.setReadTimeout(READ_TIMEOUT);
- conn.setConnectTimeout(CONNECTION_TIMEOUT);
- conn.setRequestMethod("POST");
- conn.setDoInput(true);
- conn.setDoOutput(true);
- Uri.Builder builder = new Uri.Builder()
- .appendQueryParameter("username", params[0])
- .appendQueryParameter("password", params[1]);
- String query = builder.build().getEncodedQuery();
- OutputStream os = conn.getOutputStream();
- BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(os, "UTF-8"));
- writer.write(query);
- writer.flush();
- writer.close();
- os.close();
- conn.connect();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- return "exception";
- }
- try
- {
- int response_code = conn.getResponseCode();
- if (response_code == HttpURLConnection.HTTP_OK)
- {
- InputStream input = conn.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
- StringBuilder result = new StringBuilder();
- String line;
- while ((line = reader.readLine()) != null)
- {
- result.append(line);
- }
- return(result.toString());
- }
- else
- {
- return("unsuccessful");
- }
- } catch (IOException e)
- {
- e.printStackTrace();
- return "exception";
- }
- finally {
- conn.disconnect();
- }
- }
- @Override
- protected void onPostExecute(String result)
- {
- try
- {
- JSONObject json=new JSONObject(result);
- if(json.getString("status").equals("true"))
- {
- Toast.makeText(loginactivity.this, "Login Success...", Toast.LENGTH_LONG).show();
- Intent intent = new Intent(loginactivity.this,menuactivity.class);
- startActivity(intent);
- loginactivity.this.finish();
- }
- else if(json.getString("status").equals("false"))
- {
- Toast.makeText(loginactivity.this, "Invalid username or password", Toast.LENGTH_LONG).show();
- }
- else
- {
- Toast.makeText(loginactivity.this, "Please check server connection", Toast.LENGTH_LONG).show();
- }
- }
- catch (JSONException e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement