abstract AsyncTask
This commit is contained in:
parent
9cc7ff766b
commit
abe95da7d1
3 changed files with 122 additions and 125 deletions
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.example.hochi.nextcompanion;
|
||||||
|
|
||||||
|
interface AsyncTaskCallbacks<T> {
|
||||||
|
void onTaskComplete(T result);
|
||||||
|
}
|
||||||
|
|
@ -3,58 +3,34 @@ package com.example.hochi.nextcompanion;
|
||||||
import android.animation.Animator;
|
import android.animation.Animator;
|
||||||
import android.animation.AnimatorListenerAdapter;
|
import android.animation.AnimatorListenerAdapter;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.support.design.widget.Snackbar;
|
|
||||||
import android.support.v7.app.AppCompatActivity;
|
|
||||||
import android.app.LoaderManager.LoaderCallbacks;
|
|
||||||
|
|
||||||
import android.content.CursorLoader;
|
import android.content.SharedPreferences;
|
||||||
import android.content.Loader;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.database.Cursor;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.os.AsyncTask;
|
|
||||||
|
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.ContactsContract;
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.View.OnClickListener;
|
import android.view.View.OnClickListener;
|
||||||
import android.view.inputmethod.EditorInfo;
|
import android.view.inputmethod.EditorInfo;
|
||||||
import android.widget.ArrayAdapter;
|
|
||||||
import android.widget.AutoCompleteTextView;
|
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static android.Manifest.permission.READ_CONTACTS;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A login screen that offers login via email/password.
|
* A login screen that offers login via email/password.
|
||||||
*/
|
*/
|
||||||
public class LoginActivity extends AppCompatActivity {
|
public class LoginActivity extends AppCompatActivity implements AsyncTaskCallbacks<String> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Keep track of the login task to ensure we can cancel it if requested.
|
* Keep track of the login task to ensure we can cancel it if requested.
|
||||||
*/
|
*/
|
||||||
private UserLoginTask mAuthTask = null;
|
private RequestHandler mAuthTask = null;
|
||||||
|
|
||||||
// UI references.
|
// UI references.
|
||||||
private TextView mPhoneView;
|
private TextView mPhoneView;
|
||||||
|
|
@ -136,7 +112,7 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
// Show a progress spinner, and kick off a background task to
|
// Show a progress spinner, and kick off a background task to
|
||||||
// perform the user login attempt.
|
// perform the user login attempt.
|
||||||
showProgress(true);
|
showProgress(true);
|
||||||
mAuthTask = new UserLoginTask(email, password);
|
mAuthTask = new RequestHandler(email, password, this);
|
||||||
mAuthTask.execute((Void) null);
|
mAuthTask.execute((Void) null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -182,65 +158,14 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents an asynchronous login/registration task used to authenticate
|
|
||||||
* the user.
|
|
||||||
*/
|
|
||||||
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
|
|
||||||
|
|
||||||
private final String mPhone;
|
|
||||||
private final String mPin;
|
|
||||||
|
|
||||||
UserLoginTask(String phone, String pin) {
|
|
||||||
mPhone = URLEncoder.encode(phone);
|
|
||||||
mPin = pin;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Boolean doInBackground(Void... params) {
|
public void onTaskComplete(String response) {
|
||||||
Boolean success = false;
|
if (!response.isEmpty()) {
|
||||||
String urlParameters = "apikey=rXXqTgQZUPZ89lzB&mobile=" + mPhone + "&pin=" + mPin;
|
|
||||||
|
|
||||||
HttpURLConnection connection = null;
|
|
||||||
try {
|
try {
|
||||||
//Create connection
|
JSONObject jObject = new JSONObject(response);
|
||||||
URL url = new URL("https://api.nextbike.net/api/login.json");
|
|
||||||
connection = (HttpURLConnection) url.openConnection();
|
|
||||||
connection.setRequestMethod("POST");
|
|
||||||
connection.setRequestProperty("Content-Type",
|
|
||||||
"application/x-www-form-urlencoded");
|
|
||||||
|
|
||||||
connection.setRequestProperty("Content-Length", "" +
|
|
||||||
Integer.toString(urlParameters.getBytes().length));
|
|
||||||
connection.setRequestProperty("Content-Language", "en-US");
|
|
||||||
|
|
||||||
connection.setUseCaches (false);
|
|
||||||
connection.setDoInput(true);
|
|
||||||
connection.setDoOutput(true);
|
|
||||||
|
|
||||||
//Send request
|
|
||||||
DataOutputStream wr = new DataOutputStream (
|
|
||||||
connection.getOutputStream ());
|
|
||||||
wr.writeBytes (urlParameters);
|
|
||||||
wr.flush ();
|
|
||||||
wr.close ();
|
|
||||||
|
|
||||||
//Get Response
|
|
||||||
InputStream is = connection.getInputStream();
|
|
||||||
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
|
|
||||||
String line;
|
|
||||||
StringBuilder response = new StringBuilder();
|
|
||||||
while((line = rd.readLine()) != null) {
|
|
||||||
response.append(line);
|
|
||||||
response.append('\r');
|
|
||||||
}
|
|
||||||
rd.close();
|
|
||||||
try {
|
|
||||||
JSONObject jObject = new JSONObject(response.toString());
|
|
||||||
JSONObject userObject = jObject.getJSONObject("user");
|
JSONObject userObject = jObject.getJSONObject("user");
|
||||||
String loginkey = userObject.getString("loginkey");
|
String loginkey = userObject.getString("loginkey");
|
||||||
Log.d("DEBUG", loginkey);
|
Log.d("DEBUG", loginkey);
|
||||||
success=true;
|
|
||||||
SharedPreferences sharedPref = getSharedPreferences("persistence", MODE_PRIVATE);
|
SharedPreferences sharedPref = getSharedPreferences("persistence", MODE_PRIVATE);
|
||||||
SharedPreferences.Editor editor = sharedPref.edit();
|
SharedPreferences.Editor editor = sharedPref.edit();
|
||||||
editor.putString("loginKey", loginkey);
|
editor.putString("loginKey", loginkey);
|
||||||
|
|
@ -248,40 +173,12 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
success=false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
|
|
||||||
e.printStackTrace();
|
|
||||||
|
|
||||||
} finally {
|
|
||||||
|
|
||||||
if(connection != null) {
|
|
||||||
connection.disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPostExecute(final Boolean success) {
|
|
||||||
mAuthTask = null;
|
|
||||||
showProgress(false);
|
|
||||||
|
|
||||||
if (success) {
|
|
||||||
finish();
|
finish();
|
||||||
} else {
|
} else {
|
||||||
mPinView.setError(getString(R.string.error_incorrect_pin));
|
mPinView.setError(getString(R.string.error_incorrect_pin));
|
||||||
mPinView.requestFocus();
|
mPinView.requestFocus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCancelled() {
|
|
||||||
mAuthTask = null;
|
|
||||||
showProgress(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,95 @@
|
||||||
|
package com.example.hochi.nextcompanion;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
public class RequestHandler extends AsyncTask<Void, Void, String> {
|
||||||
|
|
||||||
|
private final String mPhone;
|
||||||
|
private final String mPin;
|
||||||
|
private AsyncTaskCallbacks<String> callback;
|
||||||
|
|
||||||
|
RequestHandler(String phone, String pin, AsyncTaskCallbacks<String> act) {
|
||||||
|
mPhone = URLEncoder.encode(phone);
|
||||||
|
mPin = pin;
|
||||||
|
callback = act;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String doInBackground(Void... params) {
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
String urlParameters = "apikey=rXXqTgQZUPZ89lzB&mobile=" + mPhone + "&pin=" + mPin;
|
||||||
|
|
||||||
|
HttpURLConnection connection = null;
|
||||||
|
try {
|
||||||
|
//Create connection
|
||||||
|
URL url = new URL("https://api.nextbike.net/api/login.json");
|
||||||
|
connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.setRequestProperty("Content-Type",
|
||||||
|
"application/x-www-form-urlencoded");
|
||||||
|
|
||||||
|
connection.setRequestProperty("Content-Length", "" +
|
||||||
|
Integer.toString(urlParameters.getBytes().length));
|
||||||
|
connection.setRequestProperty("Content-Language", "en-US");
|
||||||
|
|
||||||
|
connection.setUseCaches (false);
|
||||||
|
connection.setDoInput(true);
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
//Send request
|
||||||
|
DataOutputStream wr = new DataOutputStream (
|
||||||
|
connection.getOutputStream ());
|
||||||
|
wr.writeBytes (urlParameters);
|
||||||
|
wr.flush ();
|
||||||
|
wr.close ();
|
||||||
|
|
||||||
|
//Get Response
|
||||||
|
InputStream is = connection.getInputStream();
|
||||||
|
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
|
||||||
|
String line;
|
||||||
|
while((line = rd.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
response.append('\r');
|
||||||
|
}
|
||||||
|
rd.close();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
if(connection != null) {
|
||||||
|
connection.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return response.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(final String response) {
|
||||||
|
//mAuthTask = null;
|
||||||
|
//showProgress(false);
|
||||||
|
callback.onTaskComplete(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCancelled() {
|
||||||
|
//mAuthTask = null;
|
||||||
|
//showProgress(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue