Reputation: 37
i am building an app on android where a user has to login to access the app. its connected to a remote server. i also want to welcome user when they have been directed to a new activity upon successful login "welcome +username", i understand the process has to be done in intent section, but i am using a switch statement. here is my full code.
public class LoginActivity extends Activity implements OnClickListener {
private EditText user, pass;
private Button mSubmit, mRegister;
// 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://10.0.2.2:1234/webservices/login.php";
// "http://10.0.2.2:1234/webservice/login.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.login);
final ActionBar actionBar = getActionBar();
/*actionBar.hide();*/
actionBar.setCustomView(R.layout.actionbar_login);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
/*actionBar.setBackgroundDrawable(getResources().getDrawable((R.drawable.actionbar)));*/
// setup input fields
user = (EditText) findViewById(R.id.username_login);
pass = (EditText) findViewById(R.id.password_login);
// setup buttons
mSubmit = (Button) findViewById(R.id.loginBtn);
mRegister = (Button) findViewById(R.id.register);
// register listeners
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.loginBtn:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(this, SignUpActivity.class);
startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity.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 username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
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 == 1) {
Log.d("Login Successful!", json.toString());
// save user data
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(LoginActivity.this);
Editor edit = sp.edit();
edit.putString("username", username);
edit.commit();
Intent i = new Intent(LoginActivity.this, StateActivity.class);
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;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(LoginActivity.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
Upvotes: 0
Views: 102
Reputation: 2931
Firstly this does not look like the right place to be doing this as I would imagine there would be some kind of callback in your code that is called when the user has been successfully authenticated? (I cannot see this from the code you have posted).
However... when you reach the point where you know the user has been authenticated successfully you can pass their username to the activity you want to say hello to them on using a number of methods...
Use a bundle on the Intent
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString("username", value);
Use PutExtra on the Intent
Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra("username", value);
Create a new Intent
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString("username", value);
mIntent.putExtras(mBundle);
Then in the activity you would do this to retrieve the username again...
String username = getIntent().getExtras().getString("username");
Hope that helps.
Upvotes: 1