Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 37 of 37

Thread: Getting information from API Class into main body....

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Getting information from API Class into main body....

    Good luck.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Getting information from API Class into main body....

    class Ticker extends FXRateEvent
    		{
    			// No key set and no match implemented; this event matches all RateEventInfos
                            FXPair changedPair;
    			FXRateEventInfo REI;
     
                             public void handle(FXEventInfo EI, FXEventManager EM)
    			{
    				//Just print the tick
    				REI = (FXRateEventInfo) EI;
    				System.out.println(REI.getTick()+ ":"+ REI.getTimestamp());
    //				changedPair = REI.getPair();
    //				System.out.println(changedPair);
     
    			}
     
    			public FXPair changedPair()
                            {
    				return changedPair;	
    			}
     
    		}

    You need to put your variables into fields outside of the methods, then set them inside the methods only stating the variable, and not the type.

  3. #28
    Junior Member
    Join Date
    May 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Getting information from API Class into main body....

    Thank you micecd.

  4. #29
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Getting information from API Class into main body....

    No problem, would you mind clicking the thanks button under my username? ^^

    And did you get your project working?

  5. The Following User Says Thank You to micecd For This Useful Post:

    TorusMonkey (September 13th, 2014)

  6. #30
    Junior Member
    Join Date
    May 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Getting information from API Class into main body....

    It is working. I didn't get it going though. I'm working with someone whose trade is programming. So he fixed it. Doesn't help me much in terms of learning. I need to spend more time on JAVA.

    I use code to trade forex, been working in MQL, but the broker I've been using is closing its retail devision. So now I have to make a move and the best alternative option is using Oanda's REST API, with JAVA. Just takes so damn long to make anything work in JAVA, just don't have the experience to hunt down problems in a hurry.

    --- Update ---

    In fact, maybe you can tell me why this isn't working, it's either a malformed link problem or I'm sending the header wrong:

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.Date;
     
    import org.apache.http.*;
    import org.apache.http.client.methods.*;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.message.BasicHeader;
    import org.apache.http.util.EntityUtils;
    import org.apache.http.client.HttpClient;
     
    import org.json.simple.JSONObject;
    import org.json.simple.JSONValue;
     
    public class OandaRestProjectMain {
        public static void main (String[]args) throws IOException {
     
        	CloseableHttpClient httpClient = HttpClientBuilder.create().build();
     
            try {
     
                // Set these variables to whatever personal ones are preferred
                String domain = "https://api-fxpractice.oanda.com";
                String access_token = "Token";
                String account_id = "AccountNR";
                String instruments = "EUR_USD";
     
     
                HttpUriRequest httpGet = new HttpGet(domain + "/v1/prices?accountId=" + account_id + "&instruments=" + instruments);
                httpGet.setHeader(new BasicHeader("Authorization: ", "Bearer " + access_token));
     
                System.out.println("Executing request: " + httpGet.getRequestLine());
                System.out.println("Header info: "+ "Authorization: " + "Bearer " + access_token);
     
                HttpResponse resp = httpClient.execute(httpGet);
                HttpEntity entity = resp.getEntity();
     
                if (resp.getStatusLine().getStatusCode() == 200 && entity != null) {
                    InputStream stream = entity.getContent();
                    String line;
                    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
     
                    while ((line = br.readLine()) != null) {
     
                        Object obj = JSONValue.parse(line);
                        JSONObject tick = (JSONObject) obj;
     
                        // unwrap if necessary
                        if (tick.containsKey("tick")) {
                            tick = (JSONObject) tick.get("tick");
                        }
     
                        // ignore heartbeats
                        if (tick.containsKey("instrument")) {
                            System.out.println("-------");
     
                            String instrument = tick.get("instrument").toString();
                            String time = tick.get("time").toString();
                            double bid = Double.parseDouble(tick.get("bid").toString());
                            double ask = Double.parseDouble(tick.get("ask").toString());
     
                            System.out.println(instrument);
                            System.out.println(time);
                            System.out.println(bid);
                            System.out.println(ask);
                        }
                    }
                } else {
                    // print error message
                    String responseString = EntityUtils.toString(entity, "UTF-8");
                    System.out.println(responseString);
                }
     
            } finally {
            	httpClient.close();
            }
        }
    }

    The error message is:

    {
    "code" : 4,
    "message" : "The access token provided does not allow this request to be made",
    "moreInfo" : "http:\/\/developer.oanda.com\/docs\/v1\/auth\/#overview"
    }
    And I'm very sure I am sending the right token. So it must be the header delivering it wrong.

  7. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Getting information from API Class into main body....

    Those do not look like java programming errors. If you are trying to interface to some site, perhaps someone at the site could help you.

    If you can identify a java programming problem, please ask about it.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #32
    Junior Member
    Join Date
    May 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Getting information from API Class into main body....

    The problem is in the code. I had someone else check out my token and it works. I suspect I am sending the header wrong or the token wouldn't have been rejected, sometimes I get a different error message as if the token is going through, but the link is malformed, so my JAVA question is, is there a problem with these lines, one of them appears not to be working properly:

    HttpUriRequest httpGet = new HttpGet(domain + "/v1/prices?accountId=" + account_id + "&instruments=" + instruments);
    httpGet.setHeader(new BasicHeader("Authorization: ", "Bearer " + access_token));

  9. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Getting information from API Class into main body....

    suspect I am sending the header wrong
    Do you know what the header should look like?
    There are test servers you can use that will show you what the program is sending.
    If you can see what is sent by the program and compare that to what the desired header should be you should be able to change the program to sent the correct header.

    is there a problem with these lines, one of them appears not to be working properly:
    Again compare what is correct with what the program generates so you can change the code to match what is right.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #34
    Junior Member
    Join Date
    May 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Getting information from API Class into main body....

    They've updated their code. There were deprecated classes in their old example, that I changed in my example. Probably used the wrong class.

    https://github.com/oanda/java-api-st...Streaming.java

    So I've been running, but they still have a deprecated class in their example. Not sure why they updated half the blood thing. Now this line doesn't work...

    } finally {
    // httpClient.getConnectionManager().shutdown();
    }

    Thanks for the attempts to help...

  11. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Getting information from API Class into main body....

    Usually deprecated classes will continue to work for a while.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #36
    Junior Member
    Join Date
    May 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Getting information from API Class into main body....

    Quote Originally Posted by Norm View Post
    Usually deprecated classes will continue to work for a while.
    Eclipse refuses to compile it.


    Screen Shot 2014-09-16 at 2.18.33 PM.png

  13. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Getting information from API Class into main body....

    Try asking what to do on an Eclipse users forum.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. information on main method
    By antoni in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 10th, 2013, 09:30 PM
  2. Body Systems Class
    By lah2015 in forum Object Oriented Programming
    Replies: 2
    Last Post: September 16th, 2013, 11:44 PM
  3. Replies: 2
    Last Post: November 18th, 2012, 01:09 PM
  4. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  5. Having trouble printing object information in main class
    By KingLane in forum Object Oriented Programming
    Replies: 1
    Last Post: October 11th, 2009, 06:53 PM