Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- apply plugin: 'com.android.application'
- android {
- compileSdkVersion 23
- buildToolsVersion "24.0.2"
- defaultConfig {
- applicationId "com.foodies.myfoodies"
- minSdkVersion 14
- targetSdkVersion 23
- versionCode 1
- versionName "1.0"
- testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
- }
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- debug {
- debuggable true
- }
- }
- }
- dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
- exclude group: 'com.android.support', module: 'support-annotations'
- })
- compile 'com.android.support:appcompat-v7:23.4.0'
- compile 'com.android.volley:volley:1.0.0'
- testCompile 'junit:junit:4.12'
- }
- public class LoginActivity extends AppCompatActivity {
- EditText emailBox, passwordBox;
- Button loginButton;
- TextView registerLink;
- String URL = "http://[ip address]:8081/MyFoodies/LoginRegisterWebService/login";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- emailBox = (EditText)findViewById(R.id.emailBox);
- passwordBox = (EditText)findViewById(R.id.passwordBox);
- loginButton = (Button)findViewById(R.id.loginButton);
- registerLink = (TextView)findViewById(R.id.registerLink);
- loginButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>(){
- @Override
- public void onResponse(String rs) {
- if(rs.equals("true")){
- Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_LONG).show();
- startActivity(new Intent(LoginActivity.this,Home.class));
- }
- else{
- Toast.makeText(LoginActivity.this, "Incorrect Details", Toast.LENGTH_LONG).show();
- }
- }
- },new Response.ErrorListener(){
- @Override
- public void onErrorResponse(VolleyError volleyError) {
- Toast.makeText(LoginActivity.this, "Some error occurred -> "+volleyError, Toast.LENGTH_LONG).show();;
- }
- }) {
- @Override
- protected Map<String, String> getParams() throws AuthFailureError {
- Map<String, String> parameters = new HashMap<String, String>();
- parameters.put("email", emailBox.getText().toString());
- parameters.put("password", passwordBox.getText().toString());
- return parameters;
- }
- };
- request.setRetryPolicy(new DefaultRetryPolicy(
- 7000,
- DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
- DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
- RequestQueue rQueue = Volley.newRequestQueue(LoginActivity.this);
- int socketTimeout = 10000;//10 seconds - change to what you want
- RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
- request.setRetryPolicy(policy);
- rQueue.add(request);
- }
- });
- registerLink.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
- }
- });
- }
- }
- @Path("/LoginRegisterWebService")
- public class LoginRegisterWebService {
- final static String url = "jdbc:mysql://localhost:3307/foodhub";
- final static String user = "root";
- final static String pass = "root";
- @POST
- @Path("/login")
- @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
- @Produces(MediaType.TEXT_HTML)
- public String login(@FormParam("email") String email, @FormParam("password") String password){
- String result="false";
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection(url, user, pass);
- PreparedStatement ps = con.prepareStatement("select * from foodhub.login where email=? and UserPassword=?");
- ps.setString(1, email);
- ps.setString(2, password);
- ResultSet rs = ps.executeQuery();
- if(rs.next()){
- result = "true";
- }
- con.close();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return result;
- }
- @POST
- @Path("/register")
- @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
- @Produces(MediaType.TEXT_HTML)
- public String register(@FormParam("email") String email, @FormParam("password") String password){
- String result="false";
- int x = 0;
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection(url, user, pass);
- PreparedStatement ps = con.prepareStatement("insert into login(email, UserPassword) values(?,?)");
- ps.setString(1, email);
- ps.setString(2, password);
- x = ps.executeUpdate();
- if(x==1){
- result = "true";
- }
- con.close();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return result;
- }
- }
Add Comment
Please, Sign In to add comment