yoshiputri

Untitled

Dec 15th, 2017
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7.  
  8. import javax.swing.JOptionPane;
  9.  
  10. import org.json.JSONException;
  11. import org.json.JSONObject;
  12.  
  13. public class goAPI {
  14.     String userIG = null;
  15.     String post = null;
  16.     public goAPI(String ig, String no) {
  17.         userIG=ig;
  18.         post = no;
  19.     }
  20.     public JSONObject get() throws JSONException{
  21.         JSONObject result = new JSONObject();
  22.         try {
  23.             URL link = new URL("http://rahandiapi.herokuapp.com/instapost/" + userIG + "/" + post + "?key=betakey");
  24.             System.out.println(link);
  25.             URLConnection connect = link.openConnection();
  26.             InputStream input = connect.getInputStream();
  27.             String str = getStringFromInputStream(input);
  28.             System.out.println(str);
  29.             result = new JSONObject(str);
  30.             System.out.println(result);
  31.         }
  32.         catch(Exception e) {
  33.             result.put("error", e.toString());
  34.             e.printStackTrace();
  35.         }
  36.         return result;
  37.     }
  38.    
  39.     private static String getStringFromInputStream(InputStream is) {
  40.         BufferedReader br = null;
  41.         StringBuilder sb = new StringBuilder();
  42.    
  43.         String line;
  44.         try {
  45.             br = new BufferedReader(new InputStreamReader(is));
  46.             while ((line = br.readLine()) != null) {
  47.                 sb.append(line);
  48.             }
  49.    
  50.         } catch (IOException e) {
  51.             e.printStackTrace();
  52.             JOptionPane.showMessageDialog(null, "unknown error");
  53.         } finally {
  54.             if (br != null) {
  55.                 try {
  56.                     br.close();
  57.                 } catch (IOException e) {
  58.                     e.printStackTrace();
  59.                     JOptionPane.showMessageDialog(null, "unknown error");
  60.                 }
  61.             }
  62.         }
  63.    
  64.         return sb.toString();
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment