document.write("
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().setTitle("Login");
View view = inflater.inflate(R.layout.fragment_login, container, false);
try{
InitializeModel(view);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(IsControlValid()){
AccountViewModel account = GetUserDetails(getActivity());
if (account.getStatusCode() == 200){
TokenViewModel token = GetToken(getActivity(), account);
if (token.getStatusCode() == 200){
SaveTokenSession(token);
NavigateToMainActivity();
}else{
Toast messageBox = Toast.makeText(getActivity() , token.getMessage() , Toast.LENGTH_LONG);
messageBox.show();
}
}else{
Toast messageBox = Toast.makeText(getActivity() , account.getMessage() , Toast.LENGTH_LONG);
messageBox.show();
}
}
}
});
}catch (Exception ex){
String v = ex.getMessage();
}
return view;
}
LoginFragment.java - Snippet hosted by \"Cacher\"
namespace AndroidApi.Models.Repository
{
public class AccountRepository : IAccountRepository
{
private static string connectionString;
public AccountRepository()
{
if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["appctxt"]))
connectionString = ConfigurationManager.AppSettings["appctxt"];
}
public AccountViewModel GetByEmailAndPassword(AccountViewModel item)
{
AccountViewModel result = null;
DataSet dsResult = new DataSet();
string[] tableNames = new string[] { "CustomerAccessKey" };
SqlParameter[] sqlParameters = new SqlParameter[]
{
new SqlParameter("@email", item.Email),
new SqlParameter("@password", item.Password),
};
SqlHelper.FillDataset(connectionString, CommandType.StoredProcedure, "UserGetByEmailAndPassword", dsResult, tableNames, sqlParameters);
if (dsResult.Tables.Count > 0 && dsResult.Tables[0].Rows.Count > 0)
{
result = SqlHelper.ToType<AccountViewModel>(dsResult.Tables[0].Rows[0]);
result.StatusCode = 200;
result.Message = "OK";
}
return result;
}
}
}
AccountRepository.cs - Snippet hosted by \"Cacher\"
namespace AndroidApi.Controllers.Api
{
[AllowAnonymous]
[RoutePrefix("v1/account")]
public class AccountController : ApiController
{
private readonly IAccountRepository _repository;
public AccountController(IAccountRepository repository)
{
_repository = repository;
}
[HttpPost]
[Route("login")]
public IHttpActionResult Login([FromBody] AccountViewModel item)
{
try
{
if (item == null)
return BadRequest("No user info");
AccountViewModel result = _repository.GetByEmailAndPassword(item);
if (result == null)
return NotFound();
return Ok(result);
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
}
}
AccountController.cs - Snippet hosted by \"Cacher\"
public class Login extends AsyncTask<String, String, String> {
ProgressDialog progressDialog;
HttpURLConnection connection = null;
BufferedReader reader = null;
Activity activity;
public Login(Activity activity) {
this.activity = activity;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// display a progress dialog for good user experiance
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Please Wait");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
String result = null;
AccountViewModel account;
Gson gson;
try {
URL url = new URL(params[0]);
String email = params[1];
String password = params[2];
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
JSONObject jsonObject = new JSONObject();
jsonObject.put("email", email);
jsonObject.put("password", password);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(jsonObject.toString());
wr.flush();
wr.close();
int statusCode = connection.getResponseCode();
if(statusCode == 200){
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line ="";
while ((line = reader.readLine()) != null){
buffer.append(line);
}
String jsonObjectHome = buffer.toString();
result = jsonObjectHome;
}
if (statusCode == 400){
account = new AccountViewModel();
account.setStatusCode(statusCode);
account.setMessage("bad request");
gson = new Gson();
result = gson.toJson(account);
}
if (statusCode == 404){
account = new AccountViewModel();
account.setStatusCode(statusCode);
account.setMessage("User not found please sign up");
gson = new Gson();
result = gson.toJson(account);
}
if (statusCode == 500){
account = new AccountViewModel();
account.setStatusCode(statusCode);
account.setMessage("Internal server error");
gson = new Gson();
result = gson.toJson(account);
}
} catch (Exception e) {
Log.e("MainActivity", e.getMessage(), e);
} finally {
if(connection != null) {
connection.disconnect();
}
try {
if(reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.cancel();
}
}
AsyncTaskLogin.java - Snippet hosted by \"Cacher\"
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
try{
if (isNetworkConnected(AccountActivity.this, getBaseContext())){
NavigateToLogin();
}else {
NavigateToNoInternet();
Toast messageBox = Toast.makeText(AccountActivity.this , "No internet ..." , Toast.LENGTH_LONG);
messageBox.show();
}
}catch (Exception ex){
Log.e("Error", ex.getMessage());
}
}
private void NavigateToLogin() {
LoginFragment fragment = new LoginFragment();
android.support.v4.app.FragmentTransaction fmTransaction = getSupportFragmentManager().beginTransaction();
fmTransaction.replace(R.id.MainFrameLayout, fragment);
fmTransaction.commit();
}
AccountActivity.java - Snippet hosted by \"Cacher\"
");