Различия

Здесь показаны различия между двумя версиями данной страницы.

Ссылка на это сравнение

Both sides previous revision Предыдущая версия
ru:sidebar:javaapi:codesamples:search [18/12/2013 13:01]
127.0.0.1 внешнее изменение
ru:sidebar:javaapi:codesamples:search [06/10/2015 13:58]
kopa
Строка 3: Строка 3:
 Пример показывает,​ каким образом можно получить все доступные пользователю объекты. Пример показывает,​ каким образом можно получить все доступные пользователю объекты.
  
-//:!: Внимание!//​ Для запуска данного примера на Wialon Kit во всех запросах следует заменить **<​nowiki>​http://​hst-api.wialon.com</​nowiki>​** на **<​nowiki>​http://​kit-api.wialon.com</​nowiki>​**, а для входа в систему использовать логин и пароль своей учетной записи или логин и пароль демо-пользователя:​ //**kitdemo kitdemo**//​.+//:!: Внимание!//​ Для запуска данного примера на Wialon Kit во всех запросах следует заменить **<​nowiki>​http://​hst-api.wialon.com</​nowiki>​** на **<​nowiki>​http://​kit-api.wialon.com</​nowiki>​**
 <code java> <code java>
  
Строка 17: Строка 17:
  
 public class UnitsSearchExample implements Runnable { public class UnitsSearchExample implements Runnable {
-    ​private Session session;+ private Session session;
  
-    ​// Login to server + // Login to server 
-    private void login(){ + private void login(){ 
-        // initialize Wialon session + // initialize Wialon session 
-        session.initSession("​http://​hst-api.wialon.com"​);​ + session.initSession("​http://​hst-api.wialon.com"​);​ 
-        // trying login + // trying login 
-        session.login("wialon_test",​ "test", new ResponseHandler() { + session.loginToken("2fe8024e0ab91aa6c8ed82717b71bddcECDC362358DF7D90986F5173D405CD0D42DE7B38", new ResponseHandler() { 
-            @Override + @Override 
-            public void onSuccess(String response) { + public void onSuccess(String response) { 
-                super.onSuccess(response);​ + super.onSuccess(response);​ 
-                // login succeed + // login succeed 
-                System.out.println(String.format("​Logged successfully. User name is %s", session.getCurrUser().getName()));​ + System.out.println(String.format("​Logged successfully. User name is %s", session.getCurrUser().getName()));​ 
-                //call search units + //call search units 
-                searchUnits();​ + searchUnits();​ 
-            }+ }
  
-            ​@Override + @Override 
-            public void onFailure(int errorCode, Throwable throwableError) { + public void onFailure(int errorCode, Throwable throwableError) { 
-                super.onFailure(errorCode,​ throwableError);​ + super.onFailure(errorCode,​ throwableError);​ 
-                // login failed, print error + // login failed, print error 
-                System.out.println(Errors.getErrorText(errorCode));​ + System.out.println(Errors.getErrorText(errorCode));​ 
-            +
-        }); + }); 
-    }+ }
  
-    ​private void searchUnits(){ + private void searchUnits(){ 
-        //Create new search specification + //Create new search specification 
-        SearchSpec searchSpec=new SearchSpec();​ + SearchSpec searchSpec=new SearchSpec();​ 
-        //Set items type to search avl_units + //Set items type to search avl_units 
-        searchSpec.setItemsType(Item.ItemType.avl_unit);​ + searchSpec.setItemsType(Item.ItemType.avl_unit);​ 
-        //Set property name to search + //Set property name to search 
-        searchSpec.setPropName("​sys_name"​);​ + searchSpec.setPropName("​sys_name"​);​ 
-        //Set property value mask to search all units + //Set property value mask to search all units 
-        searchSpec.setPropValueMask("​*"​);​ + searchSpec.setPropValueMask("​*"​);​ 
-        //Set sort type by units name + //Set sort type by units name 
-        searchSpec.setSortType("​sys_name"​);​ + searchSpec.setSortType("​sys_name"​);​ 
-        //Send search by created search specification with items base data flag and from 0 to maximum number + //Send search by created search specification with items base data flag and from 0 to maximum number 
-        session.searchItems(searchSpec,​ 1, Item.dataFlag.base.getValue(),​ 0, Integer.MAX_VALUE,​ new SearchResponseHandler() { + session.searchItems(searchSpec,​ 1, Item.dataFlag.base.getValue(),​ 0, Integer.MAX_VALUE,​ new SearchResponseHandler() { 
-            @Override + @Override 
-            public void onSuccessSearch(Item... items) { + public void onSuccessSearch(Item... items) { 
-                super.onSuccessSearch(items);​ + super.onSuccessSearch(items);​ 
-                // Search succeed + // Search succeed 
-                System.out.println("​Search items is successful"​);​ + System.out.println("​Search items is successful"​);​ 
-                printUnitsNames(items);​ + printUnitsNames(items);​ 
-                logout(); + logout(); 
-            +
-            @Override + @Override 
-            public void onFailure(int errorCode, Throwable throwableError) { + public void onFailure(int errorCode, Throwable throwableError) { 
-                super.onFailure(errorCode,​ throwableError);​ + super.onFailure(errorCode,​ throwableError);​ 
-                // search item failed, print error + // search item failed, print error 
-                System.out.println(Errors.getErrorText(errorCode));​ + System.out.println(Errors.getErrorText(errorCode));​ 
-                logout(); + logout(); 
-            +
-        }); + }); 
-    }+ }
  
-    ​private void printUnitsNames(Item... items){ + private void printUnitsNames(Item... items){ 
-        if (items!=null && items.length>​0) { + if (items!=null && items.length>​0) { 
-            System.out.println(String.format("​%d units found\r\nPrinting their names...",​ items.length));​ + System.out.println(String.format("​%d units found\r\nPrinting their names...",​ items.length));​ 
-            //Print items names + //Print items names 
-            for (Item item : items) + for (Item item : items) 
-                System.out.println(String.format("​\t%s",​ item.getName()));​ + System.out.println(String.format("​\t%s",​ item.getName()));​ 
-        +
-    +
-    // Logout + // Logout 
-    private void logout(){ + private void logout(){ 
-        session.logout(new ResponseHandler() { + session.logout(new ResponseHandler() { 
-            @Override + @Override 
-            public void onSuccess(String response) { + public void onSuccess(String response) { 
-                super.onSuccess(response);​ + super.onSuccess(response);​ 
-                // logout succeed + // logout succeed 
-                System.out.println("​Logout successfully"​);​ + System.out.println("​Logout successfully"​);​ 
-                System.exit(0);​ + System.exit(0);​ 
-            }+ }
  
-            ​@Override + @Override 
-            public void onFailure(int errorCode, Throwable throwableError) { + public void onFailure(int errorCode, Throwable throwableError) { 
-                super.onFailure(errorCode,​ throwableError);​ + super.onFailure(errorCode,​ throwableError);​ 
-                // logout failed, print error + // logout failed, print error 
-                System.out.println(Errors.getErrorText(errorCode));​ + System.out.println(Errors.getErrorText(errorCode));​ 
-                System.exit(0);​ + System.exit(0);​ 
-            +
-        }); + }); 
-    }+ }
  
-    ​@Override + @Override 
-    public void run() { + public void run() { 
-        // get instance of current Session + // get instance of current Session 
-        session=Session.getInstance();​ + session=Session.getInstance();​ 
-        login(); + login(); 
-    }+ }
  
-    ​public static void main(String[] args){ + public static void main(String[] args){ 
-        new Thread(new UnitsSearchExample()).start();​ + new Thread(new UnitsSearchExample()).start();​ 
-    }+ }
 } }
 </​code>​ </​code>​
Follow us on Facebook Gurtam Wialon Twitter Gurtam Wialon info@gurtam.com   |   Copyright © 2002-2024 Gurtam