Advertisement
ulpher

Untitled

Apr 24th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.82 KB | None | 0 0
  1. package nettracker.filerep.mytracker;
  2.  
  3. /**
  4.  * Created by Jeremy on 19/03/2015.
  5.  */
  6.  
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import org.apache.http.NameValuePair;
  11. import org.apache.http.message.BasicNameValuePair;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14.  
  15. import android.app.Activity;
  16. import android.app.ProgressDialog;
  17. import android.content.Intent;
  18. import android.os.AsyncTask;
  19. import android.os.Bundle;
  20. import android.util.Log;
  21. import android.view.View;
  22. import android.view.View.OnClickListener;
  23. import android.widget.Button;
  24. import android.widget.EditText;
  25. import android.widget.Toast;
  26.  
  27. public class Login extends Activity implements OnClickListener {
  28.  
  29.     private EditText email, pass;
  30.     private Button mSubmit;
  31.  
  32.  
  33.     // Progress Dialog
  34.     private ProgressDialog pDialog;
  35.  
  36.     // JSON parser class
  37.     JSONParser jsonParser = new JSONParser();
  38.  
  39.     //php login script location:
  40.  
  41.     //localhost :
  42.     //testing on your device
  43.     //put your local ip instead,  on windows, run CMD > ipconfig
  44.     //or in mac's terminal type ifconfig and look for the ip under en0 or en1
  45.     // private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";
  46.  
  47.     //testing on Emulator:
  48.     //private static final String LOGIN_URL = "http://10.0.2.2:1234/webservice/login.php";
  49.  
  50.     //testing from a real server:
  51.     private static final String LOGIN_URL = "http://www.filerep.net/tracker/androidlogin.php";
  52.  
  53.     //JSON element ids from repsonse of php script:
  54.     private static final String TAG_SUCCESS = "success";
  55.     private static final String TAG_MESSAGE = "message";
  56.  
  57.     @Override
  58.     protected void onCreate(Bundle savedInstanceState) {
  59.         // TODO Auto-generated method stub
  60.         super.onCreate(savedInstanceState);
  61.         setContentView(R.layout.activity_main);
  62.  
  63.         //setup input fields
  64.         email = (EditText) findViewById(R.id.email);
  65.         pass = (EditText) findViewById(R.id.password);
  66.  
  67.         //setup buttons
  68.         mSubmit = (Button) findViewById(R.id.login);
  69.  
  70.  
  71.         mSubmit.setOnClickListener(this);
  72. //        mRegister.setOnClickListener(this);
  73.  
  74.     }
  75.  
  76.     @Override
  77.     public void onClick(View v) {
  78.         // determine which button was pressed:
  79.         switch (v.getId()) {
  80.             case R.id.login:
  81.                 new AttemptLogin().execute();
  82.                 break;
  83.             default:
  84.                 break;
  85.         }
  86.     }
  87. //    private boolean textFieldsAreEmptyOrHaveSpaces() {
  88. //        String tempUserName = txtUserName.getText().toString().trim();
  89. //        String tempWebsite = txtWebsite.getText().toString().trim();
  90. //
  91. //        if (tempWebsite.length() == 0 || hasSpaces(tempWebsite) || tempUserName.length() == 0 || hasSpaces(tempUserName)) {
  92. //            Toast.makeText(this, R.string.textfields_empty_or_spaces, Toast.LENGTH_LONG).show();
  93. //            return true;
  94. //        }
  95. //
  96. //        return false;
  97. //    }
  98. //
  99. //    private boolean hasSpaces(String str) {
  100. //        return ((str.split(" ").length > 1) ? true : false);
  101. //    }
  102.  
  103.     //AsyncTask is a seperate thread than the thread that runs the GUI
  104.     //Any type of networking should be done with asynctask.
  105.     class AttemptLogin extends AsyncTask<String, String, String> {
  106.  
  107.         /**
  108.          * Before starting background thread Show Progress Dialog
  109.          */
  110.         boolean failure = false;
  111.  
  112.         @Override
  113.         protected void onPreExecute() {
  114.             super.onPreExecute();
  115.             pDialog = new ProgressDialog(Login.this);
  116.             pDialog.setMessage("Attempting login...");
  117.             pDialog.setIndeterminate(false);
  118.             pDialog.setCancelable(true);
  119.             pDialog.show();
  120.         }
  121.  
  122.         @Override
  123.         protected String doInBackground(String... args) {
  124.  
  125.             // TODO Auto-generated method stub
  126.             // Check for success tag
  127.             int success;
  128.             String emailString = email.getText().toString();
  129.             String passwordString = pass.getText().toString();
  130.             try {
  131.                 // Building Parameters
  132.                 List<NameValuePair> params = new ArrayList<NameValuePair>();
  133.                 params.add(new BasicNameValuePair("email", emailString));
  134.                 params.add(new BasicNameValuePair("password", passwordString));
  135.  
  136.                 Log.d("request!", "starting");
  137.                 // getting product details by making HTTP request
  138.                 JSONObject json = jsonParser.makeHttpRequest(
  139.                         LOGIN_URL, "POST", params);
  140.  
  141.                 // check your log for json response
  142.                 Log.d("Login attempt", json.toString());
  143.  
  144.                 // json success tag
  145.                 success = json.getInt(TAG_SUCCESS);
  146.                 if (success > 0) {
  147.                     Log.d("Login Successful! " , json.toString());
  148.                     Intent i = new Intent(Login.this, activity_gpstracker.class);
  149.                     i.putExtra("user_id",success);
  150.                     finish();
  151.                     startActivity(i);
  152.                     return json.getString(TAG_MESSAGE);
  153.                 } else {
  154.                     Log.d("Login Failure!", json.getString(TAG_MESSAGE));
  155.                     return json.getString(TAG_MESSAGE);
  156.  
  157.                 }
  158.             } catch (JSONException e) {
  159.                 e.printStackTrace();
  160.             }
  161.  
  162.             return null;
  163.  
  164.         }
  165.  
  166.         /**
  167.          * After completing background task Dismiss the progress dialog
  168.          * *
  169.          */
  170.         protected void onPostExecute(String file_url) {
  171.             // dismiss the dialog once product deleted
  172.  
  173.             pDialog.dismiss();
  174.             if (file_url != null) {
  175.                 Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
  176.             }
  177.  
  178.         }
  179.  
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement