Can't open an url with loadJSONObject but it works with HTTP Library

Demo based on prev post.

Kf

//REFERENCES: https://forum.processing.org/two/discussion/5782/loadstrings-url-403-error


//===========================================================================
// IMPORTS:
import java.net.*;
import java.io.*;


//===========================================================================
// PROCESSING DEFAULT FUNCTIONS:

void settings(){
  size(400,600);
}

void setup(){

  textAlign(CENTER,CENTER);
  rectMode(CENTER);
  
  fill(255);
  strokeWeight(2);
  noLoop();
}

void draw(){
  background(0);
  String txt=getData("https://www.colourlovers.com/api/palettes/top?format=json"); /  
  JSONArray json = parseJSONArray(txt);  
  println(txt);
  
}

String getData(String urlsite){
  
  String htmlText="";;

  try {
    // Create a URL object
    URL url = new URL(urlsite);
    URLConnection conn = url.openConnection();
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB;     rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
    // Read all of the text returned by the HTTP server
    BufferedReader in = new BufferedReader
      (new InputStreamReader(conn.getInputStream(), "UTF-8"));
 
    String readLine;
 
    while ( (readLine = in.readLine ()) != null) {
      // Keep in mind that readLine() strips the newline characters
      System.out.println(readLine);
      htmlText = htmlText+'\n'+readLine;
    }
    in.close();
  } 
  catch (MalformedURLException e) {
    e.printStackTrace();
  } 
  catch (IOException e) {
    e.printStackTrace();
  }
  
  return htmlText;
}

1 Like