@Override
  public Object parseResponse(InputStream is) throws IOTException {

    BufferedReader in = null;
    try {
      in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
      ObjectMapper objectMapper = new ObjectMapper();
      JsonNode node = objectMapper.readTree(in);
      IOTLog.i("HTTP Result", node.toString());
      return objectMapper.readValue(node.toString(), getResultClass());

    } catch (Exception e) {
      e.printStackTrace();
      IOTLog.d("HttpRequestHandle", e.getMessage());
      if (e instanceof IOTException) {
        throw (IOTException) e;
      } else {
        throw new IOTException(-1, "服务器异常");
      }
    }
  }
Ejemplo n.º 2
0
  public boolean login(String loginName, String password) throws IOTException {

    HttpRequest request =
        new NoneAuthedHttpRequest(
            new HttpConfig.GetHttpConfig(), Constants.ServerAPIURI.GET_SESSION_ID);
    request.addParameter("dataType", "xml");
    ServiceContainer.getInstance()
        .getHttpHandler()
        .doRequest(
            request,
            new RequestListener<Session>() {

              @Override
              public void onRequestStart() {
                // TODO Auto-generated method stub

              }

              @Override
              public void onRequestGetControl(RequestControl control) {
                // TODO Auto-generated method stub

              }

              @Override
              public void onRequestCancelled() {
                // TODO Auto-generated method stub

              }

              @Override
              public void onRequestError(Exception e) {
                succeed = false;
                if (e instanceof IOTException) {
                  exception = (IOTException) e;
                } else {
                  exception = new IOTException(-1, e.getMessage());
                }
              }

              @Override
              public void onRequestResult(Session result) {
                // TODO Auto-generated method stub
                succeed = true;
                sessionID = result.getSessionID();
              }

              @Override
              public void onRequestComplete() {
                // TODO Auto-generated method stub
                synchronized (lock) {
                  lock.notify();
                }
              }
            });
    synchronized (lock) {
      try {
        lock.wait();
      } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }

      IOTLog.d("LOGINService", sessionID == null ? "null" : sessionID);
    }

    if (!succeed && exception != null) throw exception;

    HttpRequest request2 =
        new NoneAuthedHttpRequest(new HttpConfig.GetHttpConfig(), Constants.ServerAPIURI.LOGIN);

    String mangledPwd = EncryptUtil.getStringMD5(password + ":" + sessionID);

    request2.addParameter("dataType", "json");
    request2.addParameter("__session_id", sessionID);
    request2.addParameter("username", loginName);
    request2.addParameter("mangled_password", mangledPwd);
    request2.addParameter("lang", "zh-cn");
    request2.addParameter("timezone", Integer.toString(TimeZone.getDefault().getRawOffset()));

    ServiceContainer.getInstance()
        .getHttpHandler()
        .doRequest(
            request2,
            new RequestListener<User>() {

              @Override
              public void onRequestStart() {
                // TODO Auto-generated method stub

              }

              @Override
              public void onRequestGetControl(RequestControl control) {
                // TODO Auto-generated method stub

              }

              @Override
              public void onRequestCancelled() {
                // TODO Auto-generated method stub

              }

              @Override
              public void onRequestError(Exception e) {
                succeed = false;
                if (e instanceof IOTException) {
                  exception = (IOTException) e;
                } else {
                  exception = new IOTException(-1, e.getMessage());
                }
              }

              @Override
              public void onRequestResult(User result) {
                // TODO Auto-generated method stub
                succeed = true;
                loginUser = result;
              }

              @Override
              public void onRequestComplete() {
                // TODO Auto-generated method stub
                synchronized (lock) {
                  lock.notify();
                }
              }
            });
    synchronized (lock) {
      try {
        lock.wait();
      } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }

      IOTLog.d("LOGINService", loginUser == null ? "null" : loginUser.getUserID());
    }

    if (!succeed && exception != null) throw exception;

    if (succeed) {
      loginUser.setLoginName(loginName);
      loginUser.setPassword(password);
      ServiceContainer.getInstance().getSessionService().setSessionID(sessionID);
      ServiceContainer.getInstance().getSessionService().setLoginUser(loginUser);
    }

    return succeed;
  }