Advertisement
Guest User

Untitled

a guest
Jun 13th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. @Override
  2. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  3. super.onSizeChanged(w, h, oldw, oldh);
  4.  
  5. width = w;
  6. height = h;
  7.  
  8. mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.floor_plan1).copy(Bitmap.Config.ARGB_8888, true);
  9. mBitmap.setHasAlpha(true);
  10. mCanvas = new Canvas(mBitmap);
  11. }
  12.  
  13. public static void login(String username, String password, String ipAddress, final Callback callback) throws JSONException {
  14.  
  15. OkHttpClient client = new OkHttpClient();
  16.  
  17. JSONObject jsonObject = new JSONObject();
  18. jsonObject.put("username", username);
  19. jsonObject.put("password", password);
  20. jsonObject.put("ip", ipAddress);
  21.  
  22. RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
  23. Request request = new Request.Builder()
  24. .url("http://"+ipAddress+"/api/v0/login")
  25. .post(body)
  26. .build();
  27.  
  28. client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
  29.  
  30.  
  31. // produces exception if db connection request has fail
  32. @Override
  33. public void onFailure(Request request, IOException e) {
  34. callback.onLoginFailure(e);
  35. }
  36.  
  37. // checks is db request passed or fail
  38. @Override public void onResponse(Response response){
  39. if (!response.isSuccessful()) {
  40. callback.onLoginFailure(new IOException("Unexpected code " + response));
  41. return;
  42. }
  43.  
  44. String jsonAsString = null;
  45.  
  46. try {
  47. jsonAsString = response.body().string();
  48.  
  49. JSONObject json = new JSONObject(jsonAsString);
  50.  
  51. if (json.getString("status").equals("ok")){
  52.  
  53. Device device = new Device();
  54. device.locationID = json.getInt("location_id");
  55. device.imageID = json.getInt("imageId");
  56. device.imageName = json.getString("imageName");
  57.  
  58. device.id = json.getInt("id");
  59.  
  60. Device.instance = device;
  61.  
  62. callback.onLoginSuccess(device);
  63. } else {
  64. throw new JSONException("Invalid service response");
  65. }
  66.  
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. callback.onLoginFailure(e);
  70. }
  71. }
  72. });
  73. }
  74. protected void onCreate(Bundle savedInstanceState) {
  75. super.onCreate(savedInstanceState);
  76. setContentView(R.layout.activity_login);
  77. activity = this;
  78. // declaring variables
  79. etUsername = (EditText)findViewById(R.id.etUsername);
  80. etPassword= (EditText)findViewById(R.id.etPassword);
  81. btnLogin = (Button)findViewById(R.id.btnLogin);
  82. etIpAddress = (EditText) findViewById(R.id.etIpAddress);
  83. final String username = etUsername.getText().toString();
  84. final String password = etPassword.getText().toString();
  85. final String ipAddress = etIpAddress.getText().toString();
  86.  
  87. Device device = new Device();
  88. device.validateLogin(username, password, ipAddress);
  89.  
  90. // setting up things for login button
  91. btnLogin.setOnClickListener(new View.OnClickListener() {
  92. @Override
  93. public void onClick(View v) {
  94.  
  95. String ipAddress = etIpAddress.getText().toString();
  96.  
  97. String username = etUsername.getText().toString().trim();
  98. String password = etPassword.getText().toString().trim();
  99.  
  100. Device device = new Device();
  101. device.saveSP(username, password, ipAddress);
  102.  
  103. performLogin(username, password, ipAddress);
  104. }
  105. });
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement