asked on
package com.android.json.login;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.AsyncTask;
import android.text.Layout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Login extends Activity implements OnClickListener {
private EditText editUsername, editPassword;
private TextView tvwStatus;
private Button btnLogin;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://m.binghamtonyoga.com/login.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// *** Setup GUI Objects ***
editUsername = (EditText)findViewById(R.id.editUsername);
editPassword = (EditText)findViewById(R.id.editPassword);
tvwStatus = (TextView)findViewById(R.id.tvwStatus);
btnLogin = (Button)findViewById(R.id.btnLogin);
// *** Register OnClick Listener ***
btnLogin.setOnClickListener(this);
}
public void onClick(View v) {
// *** Which Button was Clicked? ***
switch (v.getId()) {
case R.id.btnLogin:
new AttemptLogin().execute();
break;
default:
break;
}
}
// ***************************************************************************
// Async Task to Attempt Login
// ***************************************************************************
class AttemptLogin extends AsyncTask<String,Void,String> {
boolean failure = false;
@Override
protected String doInBackground(String... args) {
// Check for success tag
int success;
String username = editUsername.getText().toString();
String password = editPassword.getText().toString();
try {
// *** Build Parameters ***
List params = new ArrayList();
params.add(new BasicNameValuePair("txtUsername", username));
params.add(new BasicNameValuePair("txtPassword", password));
// *** Make HTTP Request to Login with Parameters ***
Log.d("***HTTPRequest URL:", LOGIN_URL);
Log.d("***HTTPRequest Type:", "POST");
Log.d("***HTTPRequest Param(0):", params.get(0).toString());
Log.d("***HTTPRequest Param(1):", params.get(1).toString());
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
Log.d("***HTTPRequest Response:", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("***LOGIN SUCCESS***", json.toString());
Intent i = new Intent(Login.this, Layout.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;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}