Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.sorausagi.lingr;
- import java.io.IOException;
- import org.junit.Test;
- import org.sorausagi.lingr.LingrUtil.LingrSession;
- public class LingrTest {
- private static final String USER = "";
- private static final String PASSWORD = "";
- @Test
- public void start() throws IOException {
- LingrSession session = createSessionAndGetRooms(USER, PASSWORD);
- System.out.println("\"lingrSession\":" + session);
- String counter = findKeyAndGetValue(LingrUtil.subscribe(session),
- "counter");
- System.out.println("------counter=" + counter);
- String observe = LingrUtil.observe(session, counter); // blockされる
- System.out.println(observe);
- LingrUtil.post(session, "SoraUsagi", "受け取った!" + observe);
- }
- public static LingrSession createSessionAndGetRooms(String user,
- String password) throws IOException {
- String response1 = LingrUtil
- .createSessionAndGetResponse(user, password);
- if (response1.contains("\"status\":\"error\"")) {
- throw new RuntimeException(response1);
- }
- String session = findKeyAndGetValue(response1, "session");
- String publicId = findKeyAndGetValue(response1, "public_id");
- String nickname = findKeyAndGetValue(response1, "nickname");
- String response2 = LingrUtil.getRooms(session);
- String[] rooms = findKeyAndGetValues(response2, "rooms");
- return new LingrSession(session, publicId, nickname, rooms);
- }
- LingrSession state;
- static String[] findKeyAndGetValues(String string, String key) {
- int indexOf1 = string.indexOf("\"" + key + "\":[");
- int indexOf2 = string.indexOf(']', indexOf1);
- String[] _rooms = string.substring(
- indexOf1 + key.length() + 2/* two quote */+ 1/* semicolon */
- + 1/* open square */, indexOf2 - 1/* close square */)
- .split("\\,");
- String[] rooms = new String[_rooms.length];
- for (int i = 0; i < _rooms.length; i++) {
- rooms[i] = _rooms[i].replaceAll("\"", "");
- }
- return rooms;
- }
- static String findKeyAndGetValue(String string, String key) {
- int indexOf1 = string.indexOf("\"" + key + "\"");
- int indexOf2 = string.indexOf(',', indexOf1);
- if (indexOf2 == -1) {
- indexOf2 = string.indexOf('}', indexOf1);
- ;
- }
- String value = string.substring(indexOf1 + key.length() + 3/*two quotes and semicolon*/, indexOf2);
- return value.replaceAll("\"", "");
- }
- }
Add Comment
Please, Sign In to add comment