BalaB
BalaB

Reputation: 3871

WS play framework connection issue

Env: Java 7, Playframework

Hi guys,

I got an issue.. I created new play project on intelliJ idea. I wrote a WS and called the following URL

http://finance.yahoo.com/connection/currency-converter-cache?date=20151006

as follows

public static F.Promise<Result> getAllCurrencies(){

        SimpleDateFormat formater = new SimpleDateFormat("YYYYMMdd");
        String curl =  "http://finance.yahoo.com/connection/currency-converter-cache";

            F.Promise<Result> holder = WS.url(curl).setTimeout(2000).setQueryParameter("date", formater.format(new Date())).get().map(new F.Function<WSResponse, Result>() {
            @Override
            public Result apply(WSResponse wsResponse) throws Throwable {

                return ok(wsResponse.asJson());
            }
        });

        return holder;
    }

on calling I got connection exception..

[success] Compiled in 527ms
[info] play - Application started (Dev)
[error] play - Cannot invoke the action, eventually got an error: java.net.ConnectException: Connection timed out: no further information: finance.yahoo.com/106.10.199.10:80 to http://finance.yahoo.com/connection/currency-converter-cache?date=20150212
[error] application - 

! @6l6kefip0 - Internal server error, for (GET) [/currencies] ->

play.api.Application$$anon$1: Execution exception[[ConnectException: Connection timed out: no further information: finance.yahoo.com/106.10.xxx.xx:80 to http://finance.yahoo.com/connection/currency-converter-cache?date=20150212]]
    at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.7.jar:2.3.7]
    at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.7.jar:2.3.7]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.7.jar:2.3.7]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.7.jar:2.3.7]
    at scala.Option.map(Option.scala:145) [scala-library-2.11.1.jar:na]
Caused by: java.net.ConnectException: Connection timed out: no further information: finance.yahoo.com/106.10.xxx.xx:80 to http://finance.yahoo.com/connection/currency-converter-cache?date=20150212
    at com.ning.http.client.providers.netty.NettyConnectListener.operationComplete(NettyConnectListener.java:104) ~[async-http-client-1.8.14.jar:na]
    at org.jboss.netty.channel.DefaultChannelFuture.notifyListener(DefaultChannelFuture.java:431) ~[netty-3.9.3.Final.jar:na]
    at org.jboss.netty.channel.DefaultChannelFuture.notifyListeners(DefaultChannelFuture.java:422) ~[netty-3.9.3.Final.jar:na]
    at org.jboss.netty.channel.DefaultChannelFuture.setFailure(DefaultChannelFuture.java:384) ~[netty-3.9.3.Final.jar:na]
    at org.jboss.netty.channel.socket.nio.NioClientBoss.processSelectedKeys(NioClientBoss.java:109) ~[netty-3.9.3.Final.jar:na]
Caused by: java.net.ConnectException: Connection timed out: no further information: finance.yahoo.com/106.10.xxx.xx:80
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.7.0_71]
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:739) ~[na:1.7.0_71]

Wondering where I am missing the piece..

Same when I hit the URL in browser I am able to see output. Do i need to update application.secret in application.conf to get it work....

Upvotes: 0

Views: 1133

Answers (1)

Santiago Ferrer Deheza
Santiago Ferrer Deheza

Reputation: 312

Think the problem is that you get the Json from the response but then you ask for the "message" field wich doesn't exists in the yahoo finance response.

wsResponse.asJson().get("message");

What exactly do you want to return? if you want to return the same as yahoo sends you just need to

public static F.Promise<Result> getAllCurrencies(){

        SimpleDateFormat formater = new SimpleDateFormat("YYYYMMdd");
        String curl =  "http://finance.yahoo.com/connection/currency-converter-cache";
        F.Promise<WSResponse> holder1 = WS.url(curl).get();


            F.Promise<Result> holder = WS.url(curl).setQueryParameter("date", formater.format(new Date())).get().map(new F.Function<WSResponse, Result>() {
                @Override
                public Result apply(WSResponse wsResponse) throws Throwable {
                    return ok(wsResponse.asJson());
                }
            });

        return holder;

    }

Upvotes: 1

Related Questions