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.
|
||||
String phone = mPhoneView.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;
|
||||
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
|
||||
// perform the user login attempt.
|
||||
showProgress(true);
|
||||
mAuthTask = new RequestHandler(phone, pin, this);
|
||||
mAuthTask = new RequestHandler(credentials, this);
|
||||
mAuthTask.execute((Void) null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
package com.example.hochi.nextcompanion;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
|
|
@ -12,20 +15,23 @@ import java.net.URLEncoder;
|
|||
|
||||
public class RequestHandler extends AsyncTask<Void, Void, String> {
|
||||
|
||||
private final String mPhone;
|
||||
private final String mPin;
|
||||
private AsyncTaskCallbacks<String> callback;
|
||||
private String[] mCredentials;
|
||||
|
||||
RequestHandler(String phone, String pin, AsyncTaskCallbacks<String> act) {
|
||||
mPhone = URLEncoder.encode(phone);
|
||||
mPin = pin;
|
||||
RequestHandler(String[] credentials, AsyncTaskCallbacks<String> act) {
|
||||
mCredentials = credentials;
|
||||
callback = act;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Void... params) {
|
||||
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;
|
||||
try {
|
||||
|
|
@ -37,7 +43,7 @@ public class RequestHandler extends AsyncTask<Void, Void, String> {
|
|||
"application/x-www-form-urlencoded");
|
||||
|
||||
connection.setRequestProperty("Content-Length", "" +
|
||||
Integer.toString(urlParameters.getBytes().length));
|
||||
Integer.toString(urlParameters.toString().getBytes().length));
|
||||
connection.setRequestProperty("Content-Language", "en-US");
|
||||
|
||||
connection.setUseCaches (false);
|
||||
|
|
@ -47,7 +53,7 @@ public class RequestHandler extends AsyncTask<Void, Void, String> {
|
|||
//Send request
|
||||
DataOutputStream wr = new DataOutputStream (
|
||||
connection.getOutputStream ());
|
||||
wr.writeBytes (urlParameters);
|
||||
wr.writeBytes (urlParameters.toString());
|
||||
wr.flush ();
|
||||
wr.close ();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue