Welcome, Guest
Username: Password:
  • Page:
  • 1
  • 2

TOPIC: Client Java for testing the API

Client Java for testing the API 2 years 10 months ago #97396

  • nicola_86
  • nicola_86's Avatar
Hello!
I'm trying to develop a client in order to test the service to integrate in my system. I'd like to do that with Java. I'm trying using a standard httpclient of Apache.
I'm testing the method get_session_key insering my username and password of the server.
Here is the client: pastebin.com/6WPEMTuC
The answer is the following:
HTTP/1.1 200 OK [Date: Wed, 26 Jun 2013 06:53:16 GMT, Server: Apache/2.2.15 (CentOS), X-Powered-By: PHP/5.3.3, Set-Cookie: PHPSESSID=gvr28gs8eihjbpm7hj7fnl8i76; path=/; HttpOnly, Expires: Thu, 19 Nov 1981 08:52:00 GMT, Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, Pragma: no-cache, Content-Length: 0, Keep-Alive: timeout=15, max=100, Connection: Keep-Alive, Content-Type: text/html; charset=UTF-8]

I'm problably doing something wrong, can anyone help me?
Thanks
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97398

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • Moderator Lime
  • Posts: 8722
  • Thank you received: 1215
  • Karma: 360
Not sure,

But think you have the session , then it great ?

Maybe some library to help you:
- json-rpc.org/implementations
- code.google.com/p/jsonrpc4j/

Denis
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97405

  • nicola_86
  • nicola_86's Avatar
Thanks for the answer.
Do you have any experience with that library: jsonrpc4j
I'm trying to understand how to use it. In the examples I've just found it:

JsonRpcHttpClient client = new JsonRpcHttpClient(new URL("example.com/UserService.json"));
User user = client.invoke("createUser", new Object[] { "bob", "the builder" }, User.class);

How I can use it for getting the session key? The api says it is a string Object.

thanks
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97407

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • Moderator Lime
  • Posts: 8722
  • Thank you received: 1215
  • Karma: 360
"java dev experience" === NULL :)
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97408

  • nicola_86
  • nicola_86's Avatar
:( anyone else?
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97409

  • nicola_86
  • nicola_86's Avatar
Do you think that PHPSESSID=gvr28gs8eihjbpm7hj7fnl8i76; is the session key?
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97416

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • Moderator Lime
  • Posts: 8722
  • Thank you received: 1215
  • Karma: 360
Yes, i think :)
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97451

  • nicola_86
  • nicola_86's Avatar
I've just check on the server side. On the log I've got this line:
[27/Jun/2013:11:29:25 +0200] 131.175.38.99 - 131.175.187.153 80 <url_of_the_server> 4386583 "POST /index.php/admin/remotecontrol/get_session_key HTTP/1.1" 200 - "-" "Apache-HttpClient/4.2.5 (java 1.5)" "-"

What do you think it happened?It seems to me that my post request wasn't well done.
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97452

  • mdekker
  • mdekker's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 343
  • Thank you received: 68
  • Karma: 38
The request needs to be a post, with content-type application/json otherwise there will be no response.
---
Menno Dekker
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97453

  • nicola_86
  • nicola_86's Avatar
As you can see its a POST and the content type is application/json: pastebin.com/9c7W21dN
What's wrong with that code? Any suggestion?
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97456

  • mdekker
  • mdekker's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 343
  • Thank you received: 68
  • Karma: 38
Try this: pastebin.com/GsmWbTgy and take it from there

(btw i am no java programmer, but this got me a json response)
---
Menno Dekker
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97457

  • mdekker
  • mdekker's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 343
  • Thank you received: 68
  • Karma: 38
Actually this line:
post.setEntity( new StringEntity("{\"method\": \"get_session_key\", \"params\": [\"admin\",\"password\"], \"id\": 99}"));

should be able to give you a valid response
---
Menno Dekker
The administrator has disabled public write access.
The following user(s) said Thank You: nicola_86

Client Java for testing the API 2 years 10 months ago #97459

  • nicola_86
  • nicola_86's Avatar
thanks mdekker. Now I have finally a response.
{"id":99,"result":null,"error":null}

I was wondering what is "id". Anyway I use the username and password to do the login at the administration panel of limesurvey, but the result is null, what does it mean?
Thanks in advance.
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97466

  • nicola_86
  • nicola_86's Avatar
sorry the error was like that: {"id":1,"result":{"status":"Invalid user name or password"},"error":null}

I've also tried with the Demo version that you have with username:admin and password:test
and the message is the same. Any suggestion?
The administrator has disabled public write access.

Client Java for testing the API 2 years 10 months ago #97481

  • nicola_86
  • nicola_86's Avatar
Ok I've solved the problem.
The problem was also in the json string. The correct one is:
post.setEntity( new StringEntity("{\"method\": \"get_session_key\", \"params\": {\"admin\",\"password\"}, \"id\": 99}"));

Here a client to test the key session and a random function of the api, using gson for decoding the message(it's not very optimized, I'm still working on it, but it could be a good start for anyone else that will struggle with java like me)
pastebin.com/7jeGB6vA
Last Edit: 2 years 10 months ago by nicola_86. Reason: the message wasn't written well..sorry
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.200 seconds