Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. public class MainActivity extends ActionBarActivity {
  2.  
  3. protected EditText username;
  4. private EditText password;
  5. protected String enteredUsername;
  6. private final String serverUrl = "http://192.168.0.103/androidlogin/index.php";
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12.  
  13. username = (EditText)findViewById(R.id.username_field);
  14. password = (EditText)findViewById(R.id.password_field);
  15. Button loginButton = (Button)findViewById(R.id.login);
  16. Button registerButton = (Button)findViewById(R.id.register_button);
  17.  
  18. loginButton.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. enteredUsername = username.getText().toString();
  22. String enteredPassword = password.getText().toString();
  23.  
  24. if(enteredUsername.equals("") || enteredPassword.equals("")){
  25. Toast.makeText(MainActivity.this, "Username or password must be filled", Toast.LENGTH_LONG).show();
  26. return;
  27. }
  28. if(enteredUsername.length() <= 1 || enteredPassword.length() <= 1){
  29. Toast.makeText(MainActivity.this, "Username or password length must be greater than one", Toast.LENGTH_LONG).show();
  30. return;
  31. }
  32. // request authentication with remote server4
  33. AsyncDataClass asyncRequestObject = new AsyncDataClass();
  34. asyncRequestObject.execute(serverUrl, enteredUsername, enteredPassword);
  35. }
  36. });
  37.  
  38. registerButton.setOnClickListener(new View.OnClickListener() {
  39. @Override
  40. public void onClick(View v) {
  41. Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
  42. startActivity(intent);
  43. }
  44. });
  45. }
  46. @Override
  47. public boolean onCreateOptionsMenu(Menu menu) {
  48. // Inflate the menu; this adds items to the action bar if it is present.
  49. getMenuInflater().inflate(R.menu.menu_main, menu);
  50. return true;
  51. }
  52. @Override
  53. public boolean onOptionsItemSelected(MenuItem item) {
  54. // Handle action bar item clicks here. The action bar will
  55. // automatically handle clicks on the Home/Up button, so long
  56. // as you specify a parent activity in AndroidManifest.xml.
  57. int id = item.getItemId();
  58.  
  59. //noinspection SimplifiableIfStatement
  60. if (id == R.id.action_settings) {
  61. return true;
  62. }
  63.  
  64. return super.onOptionsItemSelected(item);
  65. }
  66. private class AsyncDataClass extends AsyncTask<String, Void, String> {
  67.  
  68. @Override
  69. protected String doInBackground(String... params) {
  70.  
  71. HttpParams httpParameters = new BasicHttpParams();
  72. HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
  73. HttpConnectionParams.setSoTimeout(httpParameters, 5000);
  74.  
  75. HttpClient httpClient = new DefaultHttpClient(httpParameters);
  76. HttpPost httpPost = new HttpPost(params[0]);
  77.  
  78. String jsonResult = "";
  79. try {
  80. List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
  81. nameValuePairs.add(new BasicNameValuePair("username", params[1]));
  82. nameValuePairs.add(new BasicNameValuePair("password", params[2]));
  83. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  84.  
  85. HttpResponse response = httpClient.execute(httpPost);
  86. jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
  87.  
  88. } catch (ClientProtocolException e) {
  89. e.printStackTrace();
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93. return jsonResult;
  94. }
  95. @Override
  96. protected void onPreExecute() {
  97. super.onPreExecute();
  98. }
  99. @Override
  100. protected void onPostExecute(String result) {
  101. super.onPostExecute(result);
  102. System.out.println("Resulted Value: " + result);
  103. if(result.equals("") || result == null){
  104. Toast.makeText(MainActivity.this, "Server connection failed", Toast.LENGTH_LONG).show();
  105. return;
  106. }
  107. int jsonResult = returnParsedJsonObject(result);
  108. if(jsonResult == 0){
  109. Toast.makeText(MainActivity.this, "Invalid username or password", Toast.LENGTH_LONG).show();
  110. return;
  111. }
  112. if(jsonResult == 1){
  113. Intent intent = new Intent(MainActivity.this, LoginActivity.class);
  114. intent.putExtra("USERNAME", enteredUsername);
  115. intent.putExtra("MESSAGE", "You have been successfully login");
  116. startActivity(intent);
  117. }
  118. }
  119. private StringBuilder inputStreamToString(InputStream is) {
  120. String rLine = "";
  121. StringBuilder answer = new StringBuilder();
  122. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  123. try {
  124. while ((rLine = br.readLine()) != null) {
  125. answer.append(rLine);
  126. }
  127. } catch (IOException e) {
  128. // TODO Auto-generated catch block
  129. e.printStackTrace();
  130. }
  131. return answer;
  132. }
  133. }
  134. private int returnParsedJsonObject(String result){
  135.  
  136. JSONObject resultObject = null;
  137. int returnedResult = 0;
  138. try {
  139. resultObject = new JSONObject(result);
  140. returnedResult = resultObject.getInt("success");
  141. } catch (JSONException e) {
  142. e.printStackTrace();
  143. }
  144. return returnedResult;
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement