Advertisement
outattacker

Login

Apr 24th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. package intivestudio.com.seaexam;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.support.v7.widget.Toolbar;
  8. import android.os.AsyncTask;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.Toast;
  14.  
  15. import org.apache.http.NameValuePair;
  16. import org.apache.http.message.BasicNameValuePair;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19. import android.view.View.OnClickListener;
  20.  
  21. import java.util.ArrayList;
  22. import java.util.List;
  23.  
  24. /**
  25.  * Created by outattacker on 24/04/16.
  26.  */
  27. public class Login extends AppCompatActivity implements View.OnClickListener {
  28.  
  29.     Toolbar toolbar;
  30.     private ProgressDialog pDialog;
  31.     private EditText user, pass;
  32.     private Button mSubmit, mRegister;
  33.     private static final String LOGIN_URL = "http://127.0.0.1/exam/login.php";
  34.     private static final String TAG_SUCCESS = "success";
  35.     private static final String TAG_MESSAGE = "message";
  36.  
  37.     JSONParser jsonParser = new JSONParser();
  38.  
  39.     @Override
  40.     protected void onCreate(Bundle savedInstanceState) {
  41.         super.onCreate(savedInstanceState);
  42.         setContentView(R.layout.login);
  43.  
  44.         toolbar = (Toolbar) findViewById(R.id.tool_bar);
  45.         setSupportActionBar(toolbar);
  46.         getSupportActionBar().setTitle("Login");
  47.  
  48.         //setup input fields
  49.         user = (EditText)findViewById(R.id.nama);
  50.         pass = (EditText)findViewById(R.id.password);
  51.  
  52.         //setup buttons
  53.         mSubmit = (Button)findViewById(R.id.btLogin);
  54.         mRegister = (Button)findViewById(R.id.btDaftar);
  55.  
  56.         //register listeners
  57.         mSubmit.setOnClickListener(this);
  58.         mRegister.setOnClickListener(this);
  59.     }
  60.  
  61.     @Override
  62.     public void onClick(View v) {
  63.         // determine which button was pressed:
  64.         switch (v.getId()) {
  65.             case R.id.btLogin:
  66.                 new AttemptLogin().execute();
  67.                 break;
  68.             case R.id.btDaftar:
  69.                 Intent i = new Intent(this, Register.class);
  70.                 startActivity(i);
  71.                 break;
  72.  
  73.             default:
  74.                 break;
  75.         }
  76.     }
  77.  
  78.     class AttemptLogin extends AsyncTask<String, String, String> {
  79.  
  80.         /**
  81.          * Before starting background thread Show Progress Dialog
  82.          * */
  83.         boolean failure = false;
  84.  
  85.         @Override
  86.         protected void onPreExecute() {
  87.             super.onPreExecute();
  88.             pDialog = new ProgressDialog(Login.this);
  89.             pDialog.setMessage("Attempting login...");
  90.             pDialog.setIndeterminate(false);
  91.             pDialog.setCancelable(true);
  92.             pDialog.show();
  93.         }
  94.  
  95.         @Override
  96.         protected String doInBackground(String... args) {
  97.             // TODO Auto-generated method stub
  98.             // Check for success tag
  99.             int success;
  100.             String username = user.getText().toString();
  101.             String password = pass.getText().toString();
  102.             try {
  103.                 // Building Parameters
  104.                 List<NameValuePair> params = new ArrayList<NameValuePair>();
  105.                 params.add(new BasicNameValuePair("nama", username));
  106.                 params.add(new BasicNameValuePair("password", password));
  107.  
  108.                 Log.d("request!", "starting");
  109.                 // getting product details by making HTTP request
  110.                 JSONObject json = jsonParser.makeHttpRequest(
  111.                         LOGIN_URL, "POST", params);
  112.  
  113.                 // check your log for json response
  114.                 Log.d("Login attempt", json.toString());
  115.  
  116.                 // json success tag
  117.                 success = json.getInt(TAG_SUCCESS);
  118.                 if (success == 1) {
  119.                     Log.d("Login Successful!", json.toString());
  120.                     Intent i = new Intent(Login.this, ReadComments.class);
  121.                     startActivity(i);
  122.                     finish();
  123.                     return json.getString(TAG_MESSAGE);
  124.                 }else{
  125.                     Log.d("Login Failure!", json.getString(TAG_MESSAGE));
  126.                     return json.getString(TAG_MESSAGE);
  127.  
  128.                 }
  129.             } catch (JSONException e) {
  130.                 e.printStackTrace();
  131.             }
  132.  
  133.             return null;
  134.  
  135.         }
  136.         /**
  137.          * After completing background task Dismiss the progress dialog
  138.          * **/
  139.         protected void onPostExecute(String file_url) {
  140.             // dismiss the dialog once product deleted
  141.             pDialog.dismiss();
  142.             if (file_url != null){
  143.                 Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
  144.             }
  145.  
  146.         }
  147.  
  148.     }
  149.  
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement