Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login_layout);
- usernameEditText = (EditText) findViewById(R.id.id_registered_no);
- passwordEditText = (EditText) findViewById(R.id.id_login_pin);
- sendPostReqButton = (Button)findViewById(R.id.id_login2);
- sendPostReqButton.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- if (usernameEditText.getText().length() == 0 || passwordEditText.getText().length() == 0) {
- Toast.makeText(getBaseContext(), "All fields are mandatory ", Toast.LENGTH_LONG).show();
- } else {
- if (v.getId() == R.id.id_login2) {
- dialog = ProgressDialog.show(this, "", " Please wait...", true);
- String givenUsername = usernameEditText.getEditableText().toString();
- String givenPassword = passwordEditText.getEditableText().toString();
- System.out.println("Given username :" + givenUsername + " Given password :" + givenPassword);
- sendPostRequest(givenUsername, givenPassword);
- }
- }
- }
- private void sendPostRequest(String givenUsername, String givenPassword) {
- class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
- @Override
- protected String doInBackground(String... params) {
- String paramUsername = params[0];
- String paramPassword = params[1];
- System.out.println("*** doInBackground ** paramUsername " +
- paramUsername + " paramPassword :" + paramPassword);
- HttpClient httpClient = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost
- ("http://166.62.50.115:8080/login");
- BasicNameValuePair usernameBasicNameValuePair = new
- BasicNameValuePair("username", paramUsername);
- BasicNameValuePair passwordBasicNameValuePAir = new
- BasicNameValuePair("password", paramPassword);
- List<NameValuePair> nameValuePairList = new
- ArrayList<NameValuePair>();
- nameValuePairList.add(usernameBasicNameValuePair);
- nameValuePairList.add(passwordBasicNameValuePAir);
- try {
- UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList, HTTP.UTF_8);
- httpPost.setEntity(urlEncodedFormEntity);
- try {
- HttpResponse httpResponse = httpClient.execute(httpPost);
- Log.d("Http response", httpResponse.toString());
- InputStream inputStream = httpResponse.getEntity().getContent();
- InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
- BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
- StringBuilder stringBuilder = new StringBuilder();
- String bufferedStrChunk = null;
- while((bufferedStrChunk = bufferedReader.readLine()) != null){
- stringBuilder.append(bufferedStrChunk);
- }
- return stringBuilder.toString();
- } catch (ClientProtocolException cpe) {
- System.out.println("First Exception caz of HttpResponese :" + cpe);
- cpe.printStackTrace();
- } catch (IOException ioe) {
- System.out.println("Second Exception caz of HttpResponse :" + ioe);
- ioe.printStackTrace();
- }
- } catch (UnsupportedEncodingException uee) {
- System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
- uee.printStackTrace();
- }
- return null;
- }
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- if(result.equals("")){
- dialog.cancel();
- Toast.makeText(getApplicationContext(), "Successfully logged in", Toast.LENGTH_LONG).show();
- }else{
- dialog.cancel();
- Toast.makeText(getApplicationContext(), "Invalid Number or Pin", Toast.LENGTH_LONG).show();
- }
- }
- }
- SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
- sendPostReqAsyncTask.execute(givenUsername, givenPassword);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement