Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nettracker.filerep.mytracker;
- /**
- * Created by Jeremy on 19/03/2015.
- */
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.http.NameValuePair;
- import org.apache.http.message.BasicNameValuePair;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class Login extends Activity implements OnClickListener {
- private EditText email, pass;
- private Button mSubmit;
- // Progress Dialog
- private ProgressDialog pDialog;
- // JSON parser class
- JSONParser jsonParser = new JSONParser();
- //php login script location:
- //localhost :
- //testing on your device
- //put your local ip instead, on windows, run CMD > ipconfig
- //or in mac's terminal type ifconfig and look for the ip under en0 or en1
- // private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";
- //testing on Emulator:
- //private static final String LOGIN_URL = "http://10.0.2.2:1234/webservice/login.php";
- //testing from a real server:
- private static final String LOGIN_URL = "http://www.filerep.net/tracker/androidlogin.php";
- //JSON element ids from repsonse of php script:
- private static final String TAG_SUCCESS = "success";
- private static final String TAG_MESSAGE = "message";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //setup input fields
- email = (EditText) findViewById(R.id.email);
- pass = (EditText) findViewById(R.id.password);
- //setup buttons
- mSubmit = (Button) findViewById(R.id.login);
- mSubmit.setOnClickListener(this);
- // mRegister.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- // determine which button was pressed:
- switch (v.getId()) {
- case R.id.login:
- new AttemptLogin().execute();
- break;
- default:
- break;
- }
- }
- // private boolean textFieldsAreEmptyOrHaveSpaces() {
- // String tempUserName = txtUserName.getText().toString().trim();
- // String tempWebsite = txtWebsite.getText().toString().trim();
- //
- // if (tempWebsite.length() == 0 || hasSpaces(tempWebsite) || tempUserName.length() == 0 || hasSpaces(tempUserName)) {
- // Toast.makeText(this, R.string.textfields_empty_or_spaces, Toast.LENGTH_LONG).show();
- // return true;
- // }
- //
- // return false;
- // }
- //
- // private boolean hasSpaces(String str) {
- // return ((str.split(" ").length > 1) ? true : false);
- // }
- //AsyncTask is a seperate thread than the thread that runs the GUI
- //Any type of networking should be done with asynctask.
- class AttemptLogin extends AsyncTask<String, String, String> {
- /**
- * Before starting background thread Show Progress Dialog
- */
- boolean failure = false;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog = new ProgressDialog(Login.this);
- pDialog.setMessage("Attempting login...");
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(true);
- pDialog.show();
- }
- @Override
- protected String doInBackground(String... args) {
- // TODO Auto-generated method stub
- // Check for success tag
- int success;
- String emailString = email.getText().toString();
- String passwordString = pass.getText().toString();
- try {
- // Building Parameters
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- params.add(new BasicNameValuePair("email", emailString));
- params.add(new BasicNameValuePair("password", passwordString));
- Log.d("request!", "starting");
- // getting product details by making HTTP request
- JSONObject json = jsonParser.makeHttpRequest(
- LOGIN_URL, "POST", params);
- // check your log for json response
- Log.d("Login attempt", json.toString());
- // json success tag
- success = json.getInt(TAG_SUCCESS);
- if (success > 0) {
- Log.d("Login Successful! " , json.toString());
- Intent i = new Intent(Login.this, activity_gpstracker.class);
- i.putExtra("user_id",success);
- finish();
- startActivity(i);
- return json.getString(TAG_MESSAGE);
- } else {
- Log.d("Login Failure!", json.getString(TAG_MESSAGE));
- return json.getString(TAG_MESSAGE);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * After completing background task Dismiss the progress dialog
- * *
- */
- protected void onPostExecute(String file_url) {
- // dismiss the dialog once product deleted
- pDialog.dismiss();
- if (file_url != null) {
- Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement