SHARE
TWEET

KoneksiActivity.java

cabalz1445 Dec 8th, 2019 (edited) 130 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ------------ SCRIPT UNTUK KoneksiActivity.java --------------------------------------------------------------
  2.  
  3. package com.ggwp.perpusktg;
  4.  
  5. import java.io.IOException;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.io.*;
  9. import java.net.*;
  10.  
  11. public class KoneksiActivity {
  12.  
  13.     //mendapatkan nilai bit yg diperlukan
  14.     public String call(String url){
  15.         int BUFFER_SIZE = 2000;
  16.         InputStream in = null;
  17.         try{
  18.             in = openHttpConnection(url);
  19.         } catch (IOException e) {
  20.             e.printStackTrace();
  21.             return "";
  22.         }
  23.         //membaca nilai bit menjadi nilai karakter
  24.         InputStreamReader inputStreamReader = new InputStreamReader(in);
  25.         int charRead;
  26.         String string = "";
  27.         char[] inputBuffer = new char[BUFFER_SIZE];
  28.         try{
  29.             while ((charRead = inputStreamReader.read(inputBuffer)) > 0) {
  30.                 String readString = String.copyValueOf(inputBuffer,0,charRead);
  31.                 string += readString;
  32.                 inputBuffer = new char[BUFFER_SIZE];
  33.             }
  34.             in.close();
  35.         } catch (IOException e) {
  36.             e.printStackTrace();
  37.             return "";
  38.         }
  39.         return string;
  40. }
  41. //membuka URL dan meminta respon dari input streamreader
  42. private InputStream openHttpConnection(String url) throws IOException {
  43.         InputStream in = null;
  44.         int response = -1;
  45.         URL url1 = new URL(url);
  46.         URLConnection conn = url1.openConnection();
  47.         if(!(conn instanceof HttpURLConnection)) throw new IOException("Not An Http Connection");
  48.         try{
  49.                 HttpURLConnection httpurlconnection = (HttpURLConnection) conn;
  50.                 httpurlconnection.setAllowUserInteraction(false);
  51.                 httpurlconnection.setInstanceFollowRedirects(true);
  52.                 httpurlconnection.setRequestMethod("GET");
  53.                 httpurlconnection.connect();
  54.                 response = httpurlconnection.getResponseCode();
  55.                 if(response == HttpURLConnection.HTTP_OK) {
  56.                     in = httpurlconnection.getInputStream();
  57.                 }
  58.         } catch (Exception e) {
  59.                     // TODO: handle exception
  60.                     throw new IOException("Error Connecting");
  61.                 }
  62.                 return in;
  63.         }
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top