@Override
    protected JSONObject doInBackground(Bitmap... sub) {

      Bitmap bitmap = sub[0];
      b = bitmap;

      // Creates Byte Array from picture
      URL url;
      try {

        url = new URL("https://imgur-apiv3.p.mashape.com/3/image");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        String data =
            URLEncoder.encode("image", "UTF-8")
                + "="
                + URLEncoder.encode(getImageLink(bitmap), "UTF-8");

        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.addRequestProperty("X-Mashape-Key", SecretConstants.getImgurApiKey(c));
        conn.addRequestProperty("Authorization", "Client-ID " + "bef87913eb202e9");
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        conn.connect();
        StringBuilder stb = new StringBuilder();
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
          stb.append(line).append("\n");
        }
        wr.close();
        rd.close();
        return new JSONObject(stb.toString());

      } catch (IOException | JSONException e) {
        e.printStackTrace();
      }

      return null;
    }
Example #2
0
  public void doLoadImgur(String url) {

    final String finalUrl = url;
    final String finalUrl1 = url;
    if (url.endsWith("/")) {
      url = url.substring(0, url.length() - 1);
    }
    String hash = url.substring(url.lastIndexOf("/"), url.length());

    if (NetworkUtil.isConnected(getActivity())) {

      if (hash.startsWith("/")) hash = hash.substring(1, hash.length());
      LogUtil.v("Loading" + "https://imgur-apiv3.p.mashape.com/3/image/" + hash + ".json");
      Ion.with(this)
          .load("https://imgur-apiv3.p.mashape.com/3/image/" + hash + ".json")
          .addHeader("X-Mashape-Key", SecretConstants.getImgurApiKey(getActivity()))
          .addHeader("Authorization", "Client-ID " + "bef87913eb202e9")
          .asJsonObject()
          .setCallback(
              new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject obj) {

                  if (obj != null && !obj.isJsonNull() && obj.has("error")) {
                    LogUtil.v("Error loading content");
                    (getActivity()).finish();
                  } else {
                    try {
                      if (obj != null && !obj.isJsonNull() && obj.has("image")) {
                        String type =
                            obj.get("image")
                                .getAsJsonObject()
                                .get("image")
                                .getAsJsonObject()
                                .get("type")
                                .getAsString();
                        String urls =
                            obj.get("image")
                                .getAsJsonObject()
                                .get("links")
                                .getAsJsonObject()
                                .get("original")
                                .getAsString();

                        if (type.contains("gif")) {
                          doLoadGif(urls);
                        } else if (!imageShown) { // only load if there is no image
                          doLoadImage(urls);
                        }
                      } else if (obj.has("data")) {
                        String type = obj.get("data").getAsJsonObject().get("type").getAsString();
                        String urls = obj.get("data").getAsJsonObject().get("link").getAsString();
                        String mp4 = "";
                        if (obj.get("data").getAsJsonObject().has("mp4")) {
                          mp4 = obj.get("data").getAsJsonObject().get("mp4").getAsString();
                        }

                        if (type.contains("gif")) {
                          doLoadGif(((mp4 == null || mp4.isEmpty()) ? urls : mp4));
                        } else if (!imageShown) { // only load if there is no image
                          doLoadImage(urls);
                        }
                      } else {
                        if (!imageShown) doLoadImage(finalUrl1);
                      }
                    } catch (Exception e2) {
                      e2.printStackTrace();
                      Intent i = new Intent(getActivity(), Website.class);
                      i.putExtra(Website.EXTRA_URL, finalUrl);
                      getActivity().startActivity(i);
                    }
                  }
                }
              });
    }
  }