Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1.  
  2. public class UserLoginTask extends AsyncTask<Void, Void, Boolean>
  3. {
  4. String errormessage;
  5. @Override
  6. protected void onPreExecute()
  7. {
  8. logindialog = ProgressDialog.show(MainLogin.this,"Please wait...", "Logging in...", true);
  9. logindialog.setCancelable(true);
  10. logindialog.setOnCancelListener(new OnCancelListener()
  11. {
  12.  
  13. @Override
  14. public void onCancel(DialogInterface arg0)
  15. {
  16. Toast.makeText(getBaseContext(), "login cancelling...", Toast.LENGTH_SHORT).show();
  17. mAuthTask.cancel(true);
  18. logindialog.dismiss();
  19. }
  20.  
  21. });
  22. }
  23. @Override
  24. protected Boolean doInBackground(Void... params)
  25. {
  26.  
  27. try
  28. {
  29. HttpClient httpclient = new DefaultHttpClient();
  30. String httpserver = "http://182.71.212.110/mscanner_demo";//"http://http://10.0.2.2:65320/UserDetail/GetIMEIDetails"; // // //replace code "http://mscanner.apphb.com";
  31. SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
  32. Editor editor = sharedPref.edit();
  33. editor.putString(getString(R.string.httpserver),httpserver);
  34. editor.commit();
  35. //server_validate_IMEI server_login_url http://localhost:65320/UserDetail/GetIMEIDetails
  36. //HttpPost httppost = new HttpPost(httpserver+ Constants.server_login_url);
  37. //List<NameValuePair> params1 = new ArrayList<NameValuePair>();
  38. //params1.add(new BasicNameValuePair("userId", userid.getText().toString()));
  39. //params1.add(new BasicNameValuePair("pwd", pswrd.getText().toString()));
  40.  
  41.  
  42. //TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  43. //String imsistring = telephonyManager.getSubscriberId();
  44. //String imei = telephonyManager.getDeviceId();
  45. //for imei
  46. telephonyInfo = TelephonyInfo.getInstance(getBaseContext());
  47. String imeiSIM11 = telephonyInfo.getImeiSIM1();
  48. String imeiSIM22 = telephonyInfo.getImeiSIM2();
  49. HttpPost httppost = new HttpPost(httpserver+ Constants.server_validate_IMEI);
  50. List<NameValuePair> params1 = new ArrayList<NameValuePair>();
  51. params1.add(new BasicNameValuePair("IMEI1", imeiSIM11));//911383459371629 imeiSIM11
  52. params1.add(new BasicNameValuePair("IMEI2", imeiSIM22));//911383459371629 imeiSIM22
  53. httppost.setEntity(new UrlEncodedFormEntity(params1));
  54.  
  55. HttpResponse response = httpclient.execute(httppost);
  56. HttpEntity entity = response.getEntity();
  57. BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()));
  58. StringBuilder sb = new StringBuilder();
  59. String line = null;
  60.  
  61. while ((line = in.readLine()) != null)
  62. {
  63. sb.append(line + "");
  64. }
  65.  
  66. // Append Server Response To Content String
  67.  
  68. Content = sb.toString();
  69.  
  70. if(sb.toString().equalsIgnoreCase("USER NOT FOUND")||sb.toString().equalsIgnoreCase("FAIL")||sb.toString().equalsIgnoreCase("DETAILS NOT FOUND"))
  71. {
  72. if (sb.toString().equalsIgnoreCase("USER NOT FOUND"))
  73. {
  74. errormessage="IMEI no. not registered.";
  75. return false;
  76. }
  77. else if (sb.toString().equalsIgnoreCase("DETAILS NOT FOUND"))
  78. {
  79. errormessage="IMEI not found.Contact your IT administrator.";
  80. return false;
  81. }
  82. else if (sb.toString().equalsIgnoreCase("FAIL"))
  83. {
  84. errormessage="Technical issue";
  85. return false;
  86. }
  87. }
  88. else
  89. {
  90. JSONArray json = new JSONArray(Content);
  91. if (json.length() > 0)
  92. {
  93. try
  94. {
  95. JSONObject c = json.getJSONObject(0);
  96. //editor.putString(getString(R.string.loginuser), userid.getText().toString());
  97. //editor.commit();
  98. //editor.putString(getString(R.string.loginpassword),pswrd.getText().toString());
  99. //editor.commit();
  100. editor.putString(getString(R.string.foldername),"mscanner/new folder");
  101. editor.commit();
  102. editor.putString(getString(R.string.directory),"androiddms");
  103. editor.commit();
  104. editor.putString(getString(R.string.username),c.getString("FTP_USER"));
  105. editor.commit();
  106. editor.putString(getString(R.string.password),c.getString("FTP_PWD"));
  107. editor.commit();
  108. editor.putString(getString(R.string.ftpserver),c.getString("FTP_IP"));
  109. editor.commit();
  110. editor.putString(getString(R.string.db_ip),c.getString("DATABASE_IP"));// change
  111. editor.commit();
  112. editor.putString(getString(R.string.dbuser),c.getString("DATABASE_USER"));
  113. editor.commit();
  114. editor.putString(getString(R.string.dbpassword),c.getString("DATABASE_PWD"));
  115. editor.commit();
  116. editor.putString(getString(R.string.dbname),c.getString("DATABASE_NAME"));
  117. editor.commit();
  118. editor.putString(getString(R.string.circle),c.getString("CIRCLE"));
  119. editor.commit();
  120. editor.putString(getString(R.string.dist_code),c.getString("DIST_CODE"));
  121. editor.commit();
  122. editor.putString(getString(R.string.IMEI),c.getString("IMEI"));
  123. editor.commit();
  124. editor.putString(getString(R.string.ZONE),c.getString("ZONE"));
  125. editor.commit();
  126. editor.putString(getString(R.string.retailerID),c.getString("RetID"));
  127. editor.commit();
  128. editor.putString(getString(R.string.storageMode),"SD Card");
  129. editor.commit();
  130. //sharedPref.getString(context.getResources().getString(R.string.storageMode), "notSet");
  131.  
  132. editor.apply();
  133.  
  134. /*
  135. * There is no need of this
  136. *
  137. HttpPost httppost1 = new HttpPost(httpserver+ Constants.server_uploadlogindata);
  138. List<NameValuePair> params2 = new ArrayList<NameValuePair>();
  139. params2.add(new BasicNameValuePair("userId", userid.getText().toString()));
  140. httppost1.setEntity(new UrlEncodedFormEntity(params2));
  141. response= httpclient.execute(httppost1);
  142.  
  143. entity = response.getEntity();
  144. in = new BufferedReader(new InputStreamReader(entity.getContent()));
  145. sb = new StringBuilder();
  146. line = null;
  147.  
  148. while ((line = in.readLine()) != null)
  149. {
  150. sb.append(line + "");
  151. }
  152. */
  153.  
  154. return true;
  155. }
  156. catch (Exception e)
  157. {
  158. errormessage=e.getMessage();
  159. return false;
  160. }
  161. }
  162. else
  163. {
  164. errormessage="Invalid Credentials";
  165. }
  166. }
  167. }
  168. catch (Exception ex)
  169. {
  170. errormessage=ex.getMessage();
  171. return false;
  172. }
  173. return false;
  174. }
  175. @Override
  176. protected void onPostExecute(final Boolean success)
  177. {
  178. mAuthTask = null;
  179. if (success)
  180. {
  181. logindialog.dismiss();
  182. Intent backserviceintent = new Intent(getBaseContext(), BackgroundService.class);
  183. startService(backserviceintent);
  184.  
  185. Intent intent = new Intent();
  186. intent.setClass(MainLogin.this, MainActivity.class);
  187. startActivity(intent);
  188. finish();
  189. }
  190. else
  191. {
  192. logindialog.dismiss();
  193. toast= Toast.makeText(getApplicationContext(),errormessage, Toast.LENGTH_SHORT);
  194. toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 150);
  195. toast.show();
  196. }
  197. }
  198. @Override
  199. protected void onCancelled()
  200. {
  201. mAuthTask = null;
  202. logindialog.dismiss();
  203. }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement