public ServerInfoJson getServerInfo(HttpUrl serverUrl, String serverID, String id)
      throws IOException {
    // set timeout to 30 seconds
    OkHttpClient client =
        defaultClient
            .newBuilder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .build();

    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("version", 1);
    jsonObject.addProperty("command", "get_server_info");
    jsonObject.addProperty("stop_when_error", "false");
    jsonObject.addProperty("stop_when_success", "false");
    jsonObject.addProperty("id", id);
    jsonObject.addProperty("serverID", serverID);
    RequestBody requestBody =
        RequestBody.create(MediaType.parse("text/plain"), gson.toJson(jsonObject));
    Request request = new Request.Builder().url(serverUrl).post(requestBody).build();
    Response response = client.newCall(request).execute();
    InputStream in = response.body().byteStream();
    JsonReader reader = null;
    ServerInfoJson serverInfoJson = null;
    try {
      reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
      serverInfoJson = gson.fromJson(reader, ServerInfoJson.class);
    } finally {
      if (reader != null) {
        reader.close();
      }
    }

    if (serverInfoJson != null) {
      if (serverInfoJson.server != null) {
        // server info found!
        return serverInfoJson;
      } else if (serverInfoJson.sites != null && !serverInfoJson.sites.isEmpty()) {
        String site = serverInfoJson.sites.get(0);
        return getServerInfo(
            new HttpUrl.Builder().scheme("http").host(site).addPathSegment("Serv.php").build(),
            serverID,
            id);
      }
    }

    throw new IOException("No server info found!");
  }
 private static void readJSON() {
   Gson gson = new GsonBuilder().create();
   JsonReader reader = null;
   try {
     reader = new JsonReader(new FileReader("html_files.json"));
     htmlJSON = gson.fromJson(reader, HashMap.class);
     reader.close();
     reader = new JsonReader(new FileReader("termID.json"));
     termsIDSMap = gson.fromJson(reader, TreeMap.class);
     reader.close();
     reader = new JsonReader(new FileReader("index.json"));
     reverseIndex = gson.fromJson(reader, HashMap.class);
   } catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } finally {
     if (reader != null) {
       try {
         reader.close();
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
   }
 }
  private int importFile(File importPath, ImporterExporter importer) {
    File file = new File(importPath, importer.getFileName());
    if (!file.exists() || !file.canRead()) {
      return ERROR_FILE_ACCESS;
    }

    importer.initializeImport(mContext);

    try {
      InputStream in = new FileInputStream(file);

      Gson gson = new Gson();

      JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
      reader.beginArray();

      while (reader.hasNext()) {
        importer.importRecord(mContext, gson, reader);
      }

      reader.endArray();
      reader.close();
    } catch (JsonParseException | IOException e) {
      // the given Json might not be valid or unreadable
      Timber.e(e, "JSON show import failed");
      return ERROR;
    }

    return SUCCESS;
  }
 public static Set<String> fromString(String str) {
   TreeSet<String> result = new TreeSet<String>();
   if (str == null || str.isEmpty()) {
     return result;
   }
   try {
     JsonReader reader = new JsonReader(new StringReader(str));
     reader.beginObject();
     while (reader.hasNext()) {
       String key = reader.nextName();
       if (JSON_STARRED_SESSIONS_KEY.equals(key)) {
         reader.beginArray();
         while (reader.hasNext()) {
           result.add(reader.nextString());
         }
         reader.endArray();
       } else {
         reader.skipValue();
       }
     }
     reader.endObject();
     reader.close();
   } catch (Exception ex) {
     Log.w(TAG, "Ignoring invalid remote content.", ex);
     return null;
   }
   return result;
 }
  private void getdata() throws IOException {

    // open URL for JSON parser:

    URL obj_URL = new URL(url);
    InputStream in = obj_URL.openStream();
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

    // read charge points array:

    reader.beginArray();
    while (!isCancelled() && reader.hasNext()) {
      ChargePoint chargePoint = gson.fromJson(reader, ChargePoint.class);
      chargePoints.add(chargePoint);
    }
    reader.endArray();
    reader.close();
  }
  public ArrayList<Clientes> leerFlujoJson(InputStream in) throws IOException {
    // Nueva instancia de la clase Gson
    Gson gson = new Gson();

    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    ArrayList<Clientes> animales = new ArrayList<>();

    // Iniciar el array
    reader.beginArray();

    while (reader.hasNext()) {
      // Lectura de objetos
      Clientes cliente = gson.fromJson(reader, Clientes.class);
      animales.add(cliente);
    }

    reader.endArray();
    reader.close();
    return animales;
  }
  public ServerInfoJson requestTunnel(ServerInfoJson infoJson, String serverID, String id)
      throws IOException {
    if (infoJson == null || infoJson.env == null || Util.isEmpty(infoJson.env.control_host)) {
      return null;
    }

    // set timeout to 30 seconds
    OkHttpClient client =
        defaultClient
            .newBuilder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .build();

    final String server = infoJson.env.control_host;

    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("command", "request_tunnel");
    jsonObject.addProperty("version", 1);
    jsonObject.addProperty("serverID", serverID);
    jsonObject.addProperty("id", id);

    RequestBody requestBody =
        RequestBody.create(MediaType.parse("text/plain"), gson.toJson(jsonObject));
    Request request =
        new Request.Builder()
            .url(HttpUrl.parse("http://" + server + "/Serv.php"))
            .post(requestBody)
            .build();
    Response response = client.newCall(request).execute();
    JsonReader reader = null;
    try {
      InputStream in = response.body().byteStream();
      reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
      return gson.fromJson(reader, ServerInfoJson.class);
    } finally {
      if (reader != null) {
        reader.close();
      }
    }
  }
Esempio n. 8
0
  @Override
  public KadMessage read(final InputStream in)
      throws IOException, ClassCastException, ClassNotFoundException {
    Reader utf8Reader = null;
    JsonReader reader = null;

    KadMessage msg = null;
    try {
      utf8Reader = new InputStreamReader(in, "UTF-8");
      reader = new JsonReader(utf8Reader);

      reader.beginArray();
      final String clazzName = gson.fromJson(reader, String.class);
      msg = gson.fromJson(reader, Class.forName(classPackage + clazzName));
      reader.endArray();

    } finally {
      reader.close();
      utf8Reader.close();
      in.close();
    }

    return msg;
  }
Esempio n. 9
0
  // Return the page list from the chapter's directory if it exists, null otherwise
  public List<Page> getSavedPageList(Source source, Manga manga, Chapter chapter) {
    List<Page> pages = null;
    File chapterDir = getAbsoluteChapterDirectory(source, manga, chapter);
    File pagesFile = new File(chapterDir, PAGE_LIST_FILE);

    JsonReader reader = null;
    try {
      if (pagesFile.exists()) {
        reader = new JsonReader(new FileReader(pagesFile.getAbsolutePath()));
        Type collectionType = new TypeToken<List<Page>>() {}.getType();
        pages = gson.fromJson(reader, collectionType);
      }
    } catch (FileNotFoundException e) {
      Timber.e(e.getCause(), e.getMessage());
    } finally {
      if (reader != null)
        try {
          reader.close();
        } catch (IOException e) {
          /* Do nothing */
        }
    }
    return pages;
  }