Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. public class LoginActivity extends AppCompatActivity {
  2.  
  3. private static String response, username, password, fdid;
  4. private static HttpClient httpclient;
  5. EditText fdidTextBox, usernameTextBox, passwordTextBox;
  6. Button loginButton;
  7. User user;
  8.  
  9. public static final MediaType JSON
  10. = MediaType.parse("application/json; charset=utf-8");
  11.  
  12. OkHttpClient client = new OkHttpClient();
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_login);
  18.  
  19.  
  20. fdidTextBox = (EditText) findViewById(R.id.fdidEditText);
  21. usernameTextBox = (EditText) findViewById(R.id.usernameEditText);
  22. passwordTextBox = (EditText) findViewById(R.id.passwordEditText);
  23.  
  24. loginButton = (Button) findViewById(R.id.login_button);
  25. //only 5 digits for fdid
  26. int maxLength = 5;
  27. fdidTextBox.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength) {
  28. }});
  29. loginButtonListener();
  30.  
  31.  
  32. }
  33.  
  34.  
  35.  
  36.  
  37. private void loginButtonListener() {
  38. username = usernameTextBox.getText().toString();
  39. password = passwordTextBox.getText().toString();
  40. fdid = fdidTextBox.getText().toString();
  41. loginButton.setOnClickListener(new View.OnClickListener() {
  42. @Override
  43. public void onClick(View v) {
  44. makePostRequestOnNewThread();
  45.  
  46. }
  47. });
  48.  
  49. }
  50. private void makePostRequestOnNewThread() {
  51. Thread t = new Thread(new Runnable() {
  52. @Override
  53. public void run() {
  54. try {
  55. //getUrlContent("http://fireeport.com/api/login");
  56. postToServer("http://fireeport.com/api/login");
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. });
  62. t.start();
  63. }
  64.  
  65. String getUrlContent(String url) throws IOException {
  66. Request request = new Request.Builder()
  67. .url(url)
  68. .build();
  69.  
  70. Response response = client.newCall(request).execute();
  71. Log.d("result",response.body().string());
  72. return response.body().string();
  73.  
  74. }
  75.  
  76. String postToServer(String url) throws IOException {
  77. RequestBody formBody=new FormBody.Builder().add("username",username).add("password",password).add("fdid",fdid).build();
  78. Request request = new Request.Builder()
  79. .url(url).post(formBody)
  80. .build();
  81.  
  82. Response response = client.newCall(request).execute();
  83. Log.d("result",response.body().string());
  84. return response.body().string();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement