Advertisement
Guest User

Untitled

a guest
Feb 25th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. public class TaskHandler {
  2.  
  3. private String URL;
  4. private User userObj;
  5. private String results;
  6. private JSONDownloaderTask task; ;
  7.  
  8. public TaskHandler( String url, User user) {
  9. this.URL = url;
  10. this.userObj = user;
  11. }
  12.  
  13. public String handleTask() {
  14. Log.d("Two", "In the function");
  15. task = new JSONDownloaderTask();
  16. Log.d("Three", "In the function");
  17. task.execute(URL);
  18. return results;
  19. }
  20.  
  21. private class JSONDownloaderTask extends AsyncTask<String, Void, String> {
  22.  
  23. private String username;// = userObj.getUsername();
  24. private String password; //= userObj.getPassword();
  25.  
  26. public HttpStatus status_code;
  27.  
  28. public JSONDownloaderTask() {
  29. Log.d("con", "Success");
  30. this.username = userObj.getUsername();
  31. this.password = userObj.getPassword();
  32.  
  33. Log.d("User" + this.username , " Pass" + this.password);
  34. }
  35.  
  36. private AsyncProgressActivity progressbar = new AsyncProgressActivity();
  37.  
  38. @Override
  39. protected void onPreExecute() {
  40. progressbar.showLoadingProgressDialog();
  41. }
  42.  
  43. @Override
  44. protected String doInBackground(String... params) {
  45.  
  46. final String url = params[0]; //getString(R.string.api_staging_uri) + "Authenticate/";
  47.  
  48. // Populate the HTTP Basic Authentitcation header with the username and password
  49. HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
  50. HttpHeaders requestHeaders = new HttpHeaders();
  51. requestHeaders.setAuthorization(authHeader);
  52. requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
  53.  
  54. // Create a new RestTemplate instance
  55. RestTemplate restTemplate = new RestTemplate();
  56.  
  57. restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
  58.  
  59. try {
  60. // Make the network request
  61. Log.d(this.getClass().getName(), url);
  62.  
  63. ResponseEntity<Message> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(requestHeaders), Message.class);
  64. status_code = response.getStatusCode();
  65. return response.getBody().toString();
  66. } catch (HttpClientErrorException e) {
  67.  
  68. status_code = e.getStatusCode();
  69. return new Message(0, e.getStatusText(), e.getLocalizedMessage(), "error").toString();
  70. } catch ( Exception e ) {
  71. Log.d(this.getClass().getName() ,e.getLocalizedMessage());
  72. return "Unknown Exception";
  73. }
  74. }
  75.  
  76.  
  77. @Override
  78. protected void onPostExecute(String result) {
  79. progressbar.dismissProgressDialog();
  80.  
  81.  
  82. switch ( status_code ) {
  83. case UNAUTHORIZED:
  84. result = "Invalid username or passowrd"; break;
  85. case ACCEPTED:
  86. result = "Invalid username or passowrd" + status_code; break;
  87. case OK:
  88. result = "Successful!"; break;
  89. }
  90. }
  91. }
  92. }
  93.  
  94. public class AsyncProgressActivity extends Activity {
  95.  
  96. protected static final String TAG = AsyncProgressActivity.class.getSimpleName();
  97.  
  98. private ProgressDialog progressDialog;
  99. private boolean destroyed = false;
  100.  
  101. @Override
  102. protected void onDestroy() {
  103. super.onDestroy();
  104. destroyed = true;
  105. }
  106.  
  107. public void showLoadingProgressDialog() {
  108. Log.d("Here", "Progress");
  109. this.showProgressDialog("Authenticating...");
  110. Log.d("Here", "afer p");
  111. }
  112.  
  113. public void showProgressDialog(CharSequence message) {
  114. Log.d("Here", "Message");
  115. if (progressDialog == null) {
  116. progressDialog = new ProgressDialog(this);
  117. progressDialog.setIndeterminate(true);
  118. }
  119. Log.d("Here", "Message 2");
  120.  
  121. progressDialog.setMessage(message);
  122. progressDialog.show();
  123. }
  124.  
  125. public void dismissProgressDialog() {
  126. if (progressDialog != null && !destroyed) {
  127. progressDialog.dismiss();
  128. }
  129. }
  130. }
  131.  
  132. public class LoginActivity extends AsyncProgressActivity implements OnClickListener {
  133.  
  134. Button login_button;
  135. HttpStatus status_code;
  136.  
  137. /** Called when the activity is first created. */
  138. @Override
  139. public void onCreate(Bundle savedInstanceState) {
  140. super.onCreate(savedInstanceState);
  141. //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  142.  
  143. setContentView(R.layout.main);
  144.  
  145. login_button = (Button) findViewById(R.id.btnLogin);
  146. login_button.setOnClickListener(this);
  147. ViewServer.get(this).addWindow(this);
  148. }
  149.  
  150. public void onDestroy() {
  151. super.onDestroy();
  152. ViewServer.get(this).removeWindow(this);
  153. }
  154.  
  155. public void onResume() {
  156. super.onResume();
  157. ViewServer.get(this).setFocusedWindow(this);
  158. }
  159.  
  160. public void onClick(View v) {
  161. if ( v.getId() == R.id.btnLogin ) {
  162.  
  163. User userobj = new User();
  164. String result;
  165. userobj.setUsername( ((EditText) findViewById(R.id.username)).getText().toString());
  166. userobj.setPassword(((EditText) findViewById(R.id.password)).getText().toString() );
  167.  
  168.  
  169. TaskHandler handler = new TaskHandler(getString(R.string.api_staging_uri) + "Authenticate/", userobj);
  170. Log.d(this.getClass().getName(), "One");
  171. result = handler.handleTask();
  172. Log.d(this.getClass().getName(), "After two");
  173. Utilities.showAlert(result, LoginActivity.this);
  174. }
  175. }
  176.  
  177. public class Utilities {
  178.  
  179. public static void showAlert(String message, Context context) {
  180.  
  181. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
  182. alertDialogBuilder.setTitle("Login");
  183.  
  184. alertDialogBuilder.setMessage(message)
  185. .setCancelable(false)
  186. .setPositiveButton("OK",new DialogInterface.OnClickListener() {
  187. public void onClick(DialogInterface dialog,int id) {
  188. dialog.dismiss();
  189. //dialog.cancel();
  190. }
  191. });
  192. alertDialogBuilder.setIcon(drawable.ic_dialog_alert);
  193.  
  194. // create alert dialog
  195. AlertDialog alertDialog = alertDialogBuilder.create();
  196.  
  197. // show it
  198. alertDialog.show();
  199. }
  200. }
  201.  
  202. Log.d("Here", "Message");
  203. if (progressDialog == null) {
  204. progressDialog = new ProgressDialog(this);
  205. progressDialog.setIndeterminate(true);
  206. }
  207. Log.d("Here", "Message 2");
  208.  
  209. public String handleTask() {
  210. Log.d("Two", "In the function");
  211. task = new JSONDownloaderTask();
  212. Log.d("Three", "In the function");
  213. task.execute(URL);
  214. return results;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement