Здесь показаны различия между двумя версиями данной страницы.
Both sides previous revision Предыдущая версия Следущая версия | Предыдущая версия Последняя версия Both sides next revision | ||
ru:sidebar:javaapi:codesamples:search [07/08/2013 11:01] kopa |
ru:sidebar:javaapi:codesamples:search [18/12/2013 13:01] 127.0.0.1 внешнее изменение |
||
---|---|---|---|
Строка 1: | Строка 1: | ||
{{indexmenu_n>2}} | {{indexmenu_n>2}} | ||
====== Поиск объектов ====== | ====== Поиск объектов ====== | ||
- | Пример показывает каким образом можно получить все доступные объекты пользователю. | + | Пример показывает, каким образом можно получить все доступные пользователю объекты. |
//:!: Внимание!// Для запуска данного примера на 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>**, а для входа в систему использовать логин и пароль своей учетной записи или логин и пароль демо-пользователя: //**kitdemo kitdemo**//. | ||
<code java> | <code java> | ||
+ | |||
+ | package com.wialon.java.test; | ||
+ | |||
+ | |||
import com.wialon.core.Errors; | import com.wialon.core.Errors; | ||
import com.wialon.core.Session; | import com.wialon.core.Session; | ||
import com.wialon.extra.SearchSpec; | import com.wialon.extra.SearchSpec; | ||
import com.wialon.item.Item; | import com.wialon.item.Item; | ||
- | import com.wialon.remote.ResponseHandler; | + | import com.wialon.remote.handlers.ResponseHandler; |
+ | import com.wialon.remote.handlers.SearchResponseHandler; | ||
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.login("wialon_test", "test", 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 ResponseHandler() { | + | 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> |
|