Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import javax.swing.JOptionPane;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class goAPI {
- String userIG = null;
- String post = null;
- public goAPI(String ig, String no) {
- userIG=ig;
- post = no;
- }
- public JSONObject get() throws JSONException{
- JSONObject result = new JSONObject();
- try {
- URL link = new URL("http://rahandiapi.herokuapp.com/instapost/" + userIG + "/" + post + "?key=betakey");
- System.out.println(link);
- URLConnection connect = link.openConnection();
- InputStream input = connect.getInputStream();
- String str = getStringFromInputStream(input);
- System.out.println(str);
- result = new JSONObject(str);
- System.out.println(result);
- }
- catch(Exception e) {
- result.put("error", e.toString());
- e.printStackTrace();
- }
- return result;
- }
- private static String getStringFromInputStream(InputStream is) {
- BufferedReader br = null;
- StringBuilder sb = new StringBuilder();
- String line;
- try {
- br = new BufferedReader(new InputStreamReader(is));
- while ((line = br.readLine()) != null) {
- sb.append(line);
- }
- } catch (IOException e) {
- e.printStackTrace();
- JOptionPane.showMessageDialog(null, "unknown error");
- } finally {
- if (br != null) {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- JOptionPane.showMessageDialog(null, "unknown error");
- }
- }
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment