We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Now the code below is a mixture of two sketches. The goal here is that the user inputs a search term. Upon hitting enter the applet searches tinysong for the song and returns the song id (this works in its own sketch). The term is simultaneously searched for on twitter using the twitter4j library(this also works on its own.)
However when I put the two together i get the following error "jsonObject is ambiguous" ???????
for the purpose of this example I removed my api keys..
Any help would be awesome.
import controlP5.*;
//import java.net.*;
/////////////////////
ControlP5 controlP5;
controlP5.Label label;
Textfield myTextfield; /// setting up text-input field
int myBitFontIndex;
PFont p = createFont("Avenir",32);
///////////////////
/* TinySong API*/ // this is how you search the grooveshark database.
String tinyK = "";
int songIdNum;
String searchT; // the user inputs their query
String searchString;
ConfigurationBuilder cb = new ConfigurationBuilder();
Twitter twitterInstance;
Query queryForTwitter;
ArrayList tweets;
String[] userName = new String[0];
String[] tweetsArr = new String[0];
void setup(){
size(1024,640);
////////////////////
cb.setOAuthConsumerKey("");
cb.setOAuthConsumerSecret("");
cb.setOAuthAccessToken("");
cb.setOAuthAccessTokenSecret("");
twitterInstance = new TwitterFactory( cb.build() ).getInstance();
///////////////////
int txtFw = 440;
controlP5 = new ControlP5(this);
controlP5.setControlFont(p);
myTextfield = controlP5.addTextfield("enter a song name",(width/2)-(txtFw/2),(height/11)*2,txtFw,80);
myTextfield.setFocus(true);
//myTextfield.valueLabel().setFont(myBitFontIndex);
myTextfield.valueLabel().style().marginTop = -6;
////////////////////////////////////
}
void draw(){
background(0);
finTweets();
}
void keyPressed(){
if(key == ENTER || key == RETURN){
searchT = myTextfield.getText();
searchT = searchT.replaceAll(" ","+");
FetchTweets();
println(searchT);
searchString = "http://tinysong.com/b/"+searchT+"?format=json&key="+tinyK;
///////////////////
///////////////////
println(searchString);
try{
String url = searchString;
JSONObject wholeJson = new JSONObject(join(loadStrings(url),""));
JSONObject resp = wholeJson; // remove .getJSONObject(""); as this json file has no objects
songIdNum = resp.getInt("SongID");
// songIdNum = resp.getInt("m");
println("The song ID for "+searchT+" is: "+songIdNum);
}
catch(JSONException e){
println("there was an error parsing the JSONObject");
}
}
}
void FetchTweets(){
String twitQ = "#"+searchT;
queryForTwitter = new Query(twitQ);
try{
QueryResult result = twitterInstance.search(queryForTwitter);
tweets = (ArrayList) result.getTweets();
}
catch (TwitterException te){
println("couldn't connect this time" + te);
}//end catch
}//end function
void finTweets(){
for(int i=0; i<tweets.size(); i++){
Status t = (Status) tweets.get(i);
userName = append(userName, t.getUser().getScreenName());
tweetsArr = append(tweetsArr, t.getText());// adds tweets to array
//get geoLoc later
text(userName[i] + "said: " + tweetsArr[i],20,15+i*30,width-20,40);
}//end loop
}
Answers
One thing to mention is that I have my json.jar and twitter4j.jar in my code file
Processing v2.0b3 mac os
That means
JSONObject
is defined in two places — presumably once in each of the libraries you're using, and Processing doesn't know which one to use.Do you need the
JSON.jar
file now that Processing 2 has JSON support built in? That looks like the most likely source of conflict to me. Otherwise you can try and refer to the types using their full package structure (e.g. something liketwitter4j.JSONObject
, although it's hard to say exactly without seeing the import statements); see these examples for further info.Hope that helps.
Cheers velvetKevorkian, I don't have any import statements as the jar libraries are placed in a code file beside the sketch. I started to figure that must be the issue. I didn't realise that processing now as a JSONObject class built in, cool.
so should the following (ln 88-89) code read: processing.data.JSONObject wholeJson = new JSONObject(join(loadStrings(url),"")); processing.data.JSONObject resp = wholeJson;
and remove the code folder?
Try pulling the JSON library out and using it as is — it should only be an issue if Twitter4j also has a JSONObject class. If that doesn't work then what you've put above looks good to me, although I haven't tested it :)
(You'll want to keep the code folder for Twitter4j, I imagine)
"I don't have any import statements as the jar libraries are placed in a code file beside the sketch"
Even then, you need the import statements!
Its cool guys velvetkevorkian was right. Twitter4j also had a json class...duh. my bad cheers.
You can use Processing.data.JSONObject to specify the source library.
Gah, I am having a similar problem:
code is here (it's a lot of code): https://www.dropbox.com/sh/2riljmpqfywibaa/AADseXkl0ms0WOYcjPSjKow7a?dl=0
I change this (in the save and load tab):
JSONObject json = new JSONObject(); to this processing.data.JSONObject json = new JSONObject();
and it still says:
the type JSONObject is ambiguous.
Any ideas? (and thanks!)
never mind, needed an import statement (slaps hand to forehead)
@sirianth try
processing.data.JSONObject json = new processing.data.JSONObject();