Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- super.onSizeChanged(w, h, oldw, oldh);
- width = w;
- height = h;
- mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.floor_plan1).copy(Bitmap.Config.ARGB_8888, true);
- mBitmap.setHasAlpha(true);
- mCanvas = new Canvas(mBitmap);
- }
- public static void login(String username, String password, String ipAddress, final Callback callback) throws JSONException {
- OkHttpClient client = new OkHttpClient();
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("username", username);
- jsonObject.put("password", password);
- jsonObject.put("ip", ipAddress);
- RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
- Request request = new Request.Builder()
- .url("http://"+ipAddress+"/api/v0/login")
- .post(body)
- .build();
- client.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
- // produces exception if db connection request has fail
- @Override
- public void onFailure(Request request, IOException e) {
- callback.onLoginFailure(e);
- }
- // checks is db request passed or fail
- @Override public void onResponse(Response response){
- if (!response.isSuccessful()) {
- callback.onLoginFailure(new IOException("Unexpected code " + response));
- return;
- }
- String jsonAsString = null;
- try {
- jsonAsString = response.body().string();
- JSONObject json = new JSONObject(jsonAsString);
- if (json.getString("status").equals("ok")){
- Device device = new Device();
- device.locationID = json.getInt("location_id");
- device.imageID = json.getInt("imageId");
- device.imageName = json.getString("imageName");
- device.id = json.getInt("id");
- Device.instance = device;
- callback.onLoginSuccess(device);
- } else {
- throw new JSONException("Invalid service response");
- }
- } catch (Exception e) {
- e.printStackTrace();
- callback.onLoginFailure(e);
- }
- }
- });
- }
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- activity = this;
- // declaring variables
- etUsername = (EditText)findViewById(R.id.etUsername);
- etPassword= (EditText)findViewById(R.id.etPassword);
- btnLogin = (Button)findViewById(R.id.btnLogin);
- etIpAddress = (EditText) findViewById(R.id.etIpAddress);
- final String username = etUsername.getText().toString();
- final String password = etPassword.getText().toString();
- final String ipAddress = etIpAddress.getText().toString();
- Device device = new Device();
- device.validateLogin(username, password, ipAddress);
- // setting up things for login button
- btnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String ipAddress = etIpAddress.getText().toString();
- String username = etUsername.getText().toString().trim();
- String password = etPassword.getText().toString().trim();
- Device device = new Device();
- device.saveSP(username, password, ipAddress);
- performLogin(username, password, ipAddress);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement