We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › xml via rest-style api
Page Index Toggle Pages: 1
xml via rest-style api (Read 1034 times)
xml via rest-style api
May 28th, 2008, 8:59pm
 
hello processing friends,
im tryin to get a xml response from a certain api out of processing (for some nights now), but seems like it can only be done with a php proxy script...maybe somebody can give me a hint how to do it without using a proxy. the api specs are here:
http://www.discogs.com/help/api

thanks
Re: xml via rest-style api
Reply #1 - May 28th, 2008, 9:47pm
 
If you show us what you've got so far it'll be much easier to help you.

It shoudl be noted that if you're wanting this as an applet on a webpage, you'll have to use a php proxy to access that, due to the security restrictions in java, unless you sign your applet.
Re: xml via rest-style api
Reply #2 - May 28th, 2008, 11:11pm
 
ah ok...
i wanted it to use it as live visualisation in the club,
so i can send a request an get it visual on the fly...
tried several codes, thats why i started this as a general question but this one i think i had the best results with:

import processing.net.*;

Client client;

void setup()
{
 size(200, 200);
 noStroke();
 // Open a TCP socket to the host:
 client = new Client(this, "www.discogs.com", 80);

 // Print the IP address of the host:
 println(client.ip());

 // Send the HTTP GET request:
 client.write("GET /artist/Aphex+Twin?f=xml&api_key=111 HTTP/1.1 \r\n");
 client.write("HOST: discogs.com\n\n");
}

void draw()
{
 background(0);
 // Print the results of the GET:
 if (client.available() > 0) {
   int inByte = client.read();
   print((char)inByte);
 }
}



Re: xml via rest-style api
Reply #3 - May 28th, 2008, 11:28pm
 
You don't need to use the net library and suchlike, you can just do:

Code:

String[] res=loadStrings("http://www.discogs.com/artist/Aphex+Twin?f=xml&api_key=111");


Or, since it's XML you could do similar, but directly into an XMLElement object (I think):

Code:
import processing.xml.*;
XMLElement e;

void setup()
{
//..
e=new XMLElement(this,"http://www.discogs.com/artist/Aphex+Twin?f=xml&api_key=111");
}
Re: xml via rest-style api
Reply #4 - May 28th, 2008, 11:51pm
 
tried it with XMLElement but the problem is that the header looks like:  <resp stat="ok" version="1.0" requests="4">
so processing wont recognize it...

and with the loadStrings() method i only get some strangly encoded symbols since, the xml structure they give out is utf-16 i found out and i wont get it back to normal...

String API_KEY = "111";
String search = "Kompakt";

String locationEncoded = URLEncoder.encode(search);
String url = "http://www.discogs.com/label/" +
locationEncoded + "?f=xml&api_key=" + API_KEY;
     

String lines[] = loadStrings(url);
println (lines);


but thank you very much for taking care of me...
Re: xml via rest-style api
Reply #5 - May 29th, 2008, 7:02am
 
I tried the last snippet and got a 400 error, perhaps a problem with the API key (I don't have one, not sure how it works, one have to create an account to know).
I see "Clients must send the "Accept-Encoding: gzip" header. The service will gzip most API responses so your application must be able to decode gzipped data." so I don't know if you get UTF-16 (which Java should understand) or actually gzipped data (which you must decompress before using).
Same issue with XMLElement, I suppose.
Unless I miss something else (after all, the Accept-Encoding header isn't sent).
Page Index Toggle Pages: 1