Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.abc.intech1;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONException;
- import org.json.JSONObject;
- /**
- * Created by selva on 3/7/2018.
- */
- public class loginactivity extends Activity
- {
- Button btnlogin;
- EditText tusername,tpassword;
- TextView msg;
- ProgressDialog pDialog;
- String username,password;
- private static final String url_login = "http://192.168.1.79:8080/webapp1/loginservlet";
- JSONParser jsonParser = new JSONParser();
- public void onCreate(Bundle b)
- {
- super.onCreate(b);
- setContentView(R.layout.loginlayout);
- //mapping
- btnlogin=findViewById(R.id.btnlogin);
- tusername=findViewById(R.id.tusername);
- tpassword=findViewById(R.id.tpassword);
- msg=findViewById(R.id.msg);
- btnlogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- username=tusername.getText().toString();
- password=tpassword.getText().toString();
- if(username.length()==0 || password.length()==0)
- {
- msg.setText("Username or password is missing");
- }
- else
- {
- username=tusername.getText().toString();
- password=tpassword.getText().toString();
- new getlogindetails().execute();
- }
- }
- });
- }
- class getlogindetails extends AsyncTask<String, String, String>
- {
- protected void onPreExecute()
- {
- super.onPreExecute();
- pDialog = new ProgressDialog(loginactivity.this);
- pDialog.setMessage("Logging in. Please wait...");
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(true);
- pDialog.show();
- }
- protected String doInBackground(String... params)
- {
- int success;
- try
- {
- Uri.Builder builder = new Uri.Builder()
- .appendQueryParameter("username", username)
- .appendQueryParameter("password", password);
- String query = builder.build().getEncodedQuery();
- JSONObject json = jsonParser.makeHttpRequest(url_login, query);
- if(json !=null)
- {
- Log.d("Login", json.toString());
- success = json.getInt("result");
- if (success == 1)
- {
- Intent myintent = new Intent(getApplicationContext(), menuactivity.class);
- startActivity(myintent);
- }
- else
- {
- loginactivity.this.runOnUiThread(new Runnable()
- {
- public void run()
- {
- Toast.makeText(getApplicationContext(), "Not Found", Toast.LENGTH_LONG).show();
- }
- });
- }
- }
- else
- {
- loginactivity.this.runOnUiThread(new Runnable()
- {
- public void run()
- {
- Toast.makeText(loginactivity.this,"Unable to contact server",Toast.LENGTH_LONG).show();
- }
- });
- return null;
- }
- }
- catch (JSONException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- protected void onPostExecute(String file_url)
- {
- pDialog.dismiss();
- }
- }
- }
- package org.webapp.servlets;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.Servlet;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.webapp.engine.users;
- public class loginservlet extends HttpServlet implements Servlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public loginservlet() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- response.getWriter().append("Served at: ").append(request.getContextPath());
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
- {
- PrintWriter out = response.getWriter();
- JSONObject json=new JSONObject();
- try
- {
- //response.setContentType("application/json");
- response.setContentType("text/html");
- String username,password;
- username=request.getParameter("username");
- password=request.getParameter("password");
- users u=new users();
- u.init();
- if(u.opencon())
- {
- if(u.validate(username, password))
- {
- json.put("result", 1);
- }
- else
- {
- json.put("result", 2);
- }
- }
- else
- {
- json.put("result", 2);
- }
- }
- catch(JSONException e)
- {
- }
- out.flush();
- out.print(json);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement