Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/images"
- android:orientation="vertical"
- tools:context="com.altavista.dc.serviceprovider.MainActivity"
- android:weightSum="1"
- >
- <TextView
- android:layout_width="wrap_content"
- android:text="Username"
- android:textSize="18dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginTop="125dp" />
- <EditText
- android:id="@+id/etUsername"
- android:layout_width="match_parent"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:text="Password"
- android:textSize="18dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center"/>
- <EditText
- android:id="@+id/etPassword"
- android:layout_width="match_parent"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:inputType="textPassword"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/bLogin"
- android:layout_width="match_parent"
- android:text="Login"
- android:textSize="18dp"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:layout_marginLeft="50dp"
- android:layout_marginRight="50dp"
- android:layout_marginBottom="10dp"/>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <TextView
- android:id="@+id/tvForgotlink"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:text="Forgot password?"
- android:textStyle="bold"
- android:layout_marginStart="50dp"
- />
- <TextView
- android:id="@+id/tvRegisterlink"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:text="Register here"
- android:textStyle="bold"
- android:layout_marginStart="50dp"/>
- </LinearLayout>
- import android.content.Intent;
- import android.net.Uri;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends AppCompatActivity implements
- View.OnClickListener{
- Button bLogin;
- EditText etUsername,etPassword;
- TextView tvRegisterlink,tvForgotlink;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- etUsername=(EditText)findViewById(R.id.etUsername);
- etPassword=(EditText)findViewById(R.id.etPassword);
- bLogin=(Button)findViewById(R.id.bLogin);
- bLogin.setOnClickListener(this);
- tvRegisterlink.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- String u=etUsername.getText().toString();
- String p=etPassword.getText().toString();
- String url="http://";
- JSONObject obj2=new JSONObject();
- try {
- obj2.put("email",u);
- obj2.put("saltedPassword",p);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- switch (v.getId()) {
- case R.id.bLogin:
- try {
- JSONObject returnVal=new JSONObject(ConnectionHelper.post(obj2, url));
- Boolean val= returnVal.getBoolean("response_bool");
- if(val){
- startActivity(new Intent(this,Home.class));
- }else{
- Toast.makeText(getApplicationContext(), "Username or password incorrect", Toast.LENGTH_SHORT).show();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- break;
- case R.id.tvRegisterlink:
- startActivity(new Intent(this,Register.class));
- break;
- }
- }
- package com.altavista.dc.serviceprovider;
- import android.app.Activity;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- public class ConnectionHelper {
- public static String post(JSONObject json, String url) throws IOException, JSONException {
- URL obj = new URL(url);
- HttpURLConnection con = (HttpURLConnection) obj.openConnection();
- //add request header
- con.setRequestMethod("POST");
- con.setRequestProperty("Content-Type", "application/json");
- // Send post request
- con.setDoOutput(true);
- DataOutputStream wr = new DataOutputStream(con.getOutputStream());
- wr.writeBytes(json.toString());
- wr.flush();
- wr.close();
- if (con.getResponseCode() == 200) {
- BufferedReader in = new BufferedReader(
- new InputStreamReader(con.getInputStream()));
- String inputLine;
- StringBuffer response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- return response.toString();
- } else {
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("response", con.getResponseCode());
- jsonObject.put("message", "error");
- return jsonObject.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement