enhance abstraction of RequestHandler
This commit is contained in:
parent
7f3159b0bc
commit
9defe416da
2 changed files with 20 additions and 9 deletions
|
|
@ -86,6 +86,11 @@ public class LoginActivity extends AppCompatActivity implements AsyncTaskCallbac
|
||||||
// Store values at the time of the login attempt.
|
// Store values at the time of the login attempt.
|
||||||
String phone = mPhoneView.getText().toString();
|
String phone = mPhoneView.getText().toString();
|
||||||
String pin = mPinView.getText().toString();
|
String pin = mPinView.getText().toString();
|
||||||
|
String[] credentials = {
|
||||||
|
"apikey=", getString(R.string.apikey),
|
||||||
|
"mobile=", mPhoneView.getText().toString(),
|
||||||
|
"pin=", mPinView.getText().toString()
|
||||||
|
};
|
||||||
|
|
||||||
boolean cancel = false;
|
boolean cancel = false;
|
||||||
View focusView = null;
|
View focusView = null;
|
||||||
|
|
@ -112,7 +117,7 @@ public class LoginActivity extends AppCompatActivity implements AsyncTaskCallbac
|
||||||
// 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 RequestHandler(phone, pin, this);
|
mAuthTask = new RequestHandler(credentials, this);
|
||||||
mAuthTask.execute((Void) null);
|
mAuthTask.execute((Void) null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
package com.example.hochi.nextcompanion;
|
package com.example.hochi.nextcompanion;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
|
|
@ -12,20 +15,23 @@ import java.net.URLEncoder;
|
||||||
|
|
||||||
public class RequestHandler extends AsyncTask<Void, Void, String> {
|
public class RequestHandler extends AsyncTask<Void, Void, String> {
|
||||||
|
|
||||||
private final String mPhone;
|
|
||||||
private final String mPin;
|
|
||||||
private AsyncTaskCallbacks<String> callback;
|
private AsyncTaskCallbacks<String> callback;
|
||||||
|
private String[] mCredentials;
|
||||||
|
|
||||||
RequestHandler(String phone, String pin, AsyncTaskCallbacks<String> act) {
|
RequestHandler(String[] credentials, AsyncTaskCallbacks<String> act) {
|
||||||
mPhone = URLEncoder.encode(phone);
|
mCredentials = credentials;
|
||||||
mPin = pin;
|
|
||||||
callback = act;
|
callback = act;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String doInBackground(Void... params) {
|
protected String doInBackground(Void... params) {
|
||||||
StringBuilder response = new StringBuilder();
|
StringBuilder response = new StringBuilder();
|
||||||
String urlParameters = "apikey=" + R.string.loginKey + "&mobile=" + mPhone + "&pin=" + mPin;
|
StringBuilder urlParameters = new StringBuilder();
|
||||||
|
int i=0;
|
||||||
|
while (i<mCredentials.length) {
|
||||||
|
urlParameters.append("&").append(mCredentials[i]).append(URLEncoder.encode(mCredentials[i+1]));
|
||||||
|
i=i+2;
|
||||||
|
}
|
||||||
|
|
||||||
HttpURLConnection connection = null;
|
HttpURLConnection connection = null;
|
||||||
try {
|
try {
|
||||||
|
|
@ -37,7 +43,7 @@ public class RequestHandler extends AsyncTask<Void, Void, String> {
|
||||||
"application/x-www-form-urlencoded");
|
"application/x-www-form-urlencoded");
|
||||||
|
|
||||||
connection.setRequestProperty("Content-Length", "" +
|
connection.setRequestProperty("Content-Length", "" +
|
||||||
Integer.toString(urlParameters.getBytes().length));
|
Integer.toString(urlParameters.toString().getBytes().length));
|
||||||
connection.setRequestProperty("Content-Language", "en-US");
|
connection.setRequestProperty("Content-Language", "en-US");
|
||||||
|
|
||||||
connection.setUseCaches (false);
|
connection.setUseCaches (false);
|
||||||
|
|
@ -47,7 +53,7 @@ public class RequestHandler extends AsyncTask<Void, Void, String> {
|
||||||
//Send request
|
//Send request
|
||||||
DataOutputStream wr = new DataOutputStream (
|
DataOutputStream wr = new DataOutputStream (
|
||||||
connection.getOutputStream ());
|
connection.getOutputStream ());
|
||||||
wr.writeBytes (urlParameters);
|
wr.writeBytes (urlParameters.toString());
|
||||||
wr.flush ();
|
wr.flush ();
|
||||||
wr.close ();
|
wr.close ();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue