无法从GET请求中获取JSON

时间:2020-11-29 20:14:49

New to Play! Framework and web development in general, I'm trying to do a simple REST GET to a web service and just get some straight-forward JSON in response. Typing the URL into a browser, I get a perfect response, with nicely formatted JSON. Calling it via code, it just blows up:

新玩!一般来说,框架和Web开发,我正在尝试对Web服务进行简单的REST GET,并且只是获得一些直接的JSON作为响应。在浏览器中输入URL,我得到了一个完美的响应,格式很好的JSON。通过代码调用它,它只是爆炸:

   WS.WSRequest wsRequest = WS.url( serviceURL );
   wsRequest.timeout( timeoutTime );
   wsRequest.setHeader("Accept", "application/json");
   wsRequest.headers.put( "Content-type","application/json" );
   wsRequest.mimeType = "application/json"; 

   WS.HttpResponse response = wsRequest.get();
   String graphServiceResponse = response.getJson().toString();

Everything executes fine, until the last line where it throws an exception and errors out. I know I have what looks like a lot of redundant code; those are my attempts to fix it. Like I said, typing the "serviceURL" into a browser, it works fine.

一切都很好,直到它引发异常和错误的最后一行。我知道我看起来有很多冗余代码;那些是我试图解决它。就像我说的那样,在浏览器中键入“serviceURL”,它运行正常。

Anyone know what I'm doing wrong? Thanks in advance!

谁知道我做错了什么?提前致谢!

2 个解决方案

#1


0  

Okay, solved this. Just omitted all the sets and such, added authentication and it worked perfectly. Weird.

好的,解决了这个问题。只是省略了所有的集合等,添加了身份验证,它完美地工作。奇怪的。

   String stringResponse = "";
   try {
      // execute GET to graph service
      WS.WSRequest wsRequest = WS.url( serviceURL ).authenticate( USERNAME, PASSWORD );
      WS.HttpResponse response = wsRequest.get();
      stringResponse = response.getString();

      ... more cool stuff ...

Thanks for looking!

谢谢你的期待!

#2


0  

I have tried and found this below code working. 10000 is the timeout parameter in ms.

我试过,发现下面的代码工作。 10000是以ms为单位的超时参数。

    String baseUrl = "your url";

    F.Promise<WSResponse> response = ws.url(baseUrl)
            .setQueryParameter(param, value)
            .get();

    return response.get(10000).asJson().toString();

#1


0  

Okay, solved this. Just omitted all the sets and such, added authentication and it worked perfectly. Weird.

好的,解决了这个问题。只是省略了所有的集合等,添加了身份验证,它完美地工作。奇怪的。

   String stringResponse = "";
   try {
      // execute GET to graph service
      WS.WSRequest wsRequest = WS.url( serviceURL ).authenticate( USERNAME, PASSWORD );
      WS.HttpResponse response = wsRequest.get();
      stringResponse = response.getString();

      ... more cool stuff ...

Thanks for looking!

谢谢你的期待!

#2


0  

I have tried and found this below code working. 10000 is the timeout parameter in ms.

我试过,发现下面的代码工作。 10000是以ms为单位的超时参数。

    String baseUrl = "your url";

    F.Promise<WSResponse> response = ws.url(baseUrl)
            .setQueryParameter(param, value)
            .get();

    return response.get(10000).asJson().toString();