Check out the LimeSurvey source code on GitHub!

Client Java for testing the API

  • nicola_86
  • nicola_86's Avatar Topic Author
  • Visitor
  • Visitor
3 years 6 months ago #97396 by nicola_86
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

Please Log in to join the conversation.

More
3 years 6 months ago #97398 by DenisChenu
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

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).

Please Log in to join the conversation.

  • nicola_86
  • nicola_86's Avatar Topic Author
  • Visitor
  • Visitor
3 years 6 months ago #97405 by nicola_86
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

Please Log in to join the conversation.

More
3 years 6 months ago #97407 by DenisChenu
"java dev experience" === NULL :)

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).

Please Log in to join the conversation.

  • nicola_86
  • nicola_86's Avatar Topic Author
  • Visitor
  • Visitor
3 years 6 months ago #97408 by nicola_86
:( anyone else?

Please Log in to join the conversation.

  • nicola_86
  • nicola_86's Avatar Topic Author
  • Visitor
  • Visitor
3 years 6 months ago #97409 by nicola_86
Do you think that PHPSESSID=gvr28gs8eihjbpm7hj7fnl8i76; is the session key?

Please Log in to join the conversation.

More
3 years 6 months ago #97416 by DenisChenu
Yes, i think :)

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).

Please Log in to join the conversation.

  • nicola_86
  • nicola_86's Avatar Topic Author
  • Visitor
  • Visitor
3 years 6 months ago #97451 by nicola_86
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.

Please Log in to join the conversation.

More
3 years 6 months ago #97452 by mdekker
The request needs to be a post, with content-type application/json otherwise there will be no response.

---
Menno Dekker

Please Log in to join the conversation.

  • nicola_86
  • nicola_86's Avatar Topic Author
  • Visitor
  • Visitor
3 years 6 months ago #97453 by nicola_86
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?

Please Log in to join the conversation.

More
3 years 6 months ago #97456 by mdekker
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

Please Log in to join the conversation.

More
3 years 6 months ago #97457 by mdekker
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 following user(s) said Thank You: nicola_86

Please Log in to join the conversation.

  • nicola_86
  • nicola_86's Avatar Topic Author
  • Visitor
  • Visitor
3 years 6 months ago #97459 by nicola_86
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.

Please Log in to join the conversation.

  • nicola_86
  • nicola_86's Avatar Topic Author
  • Visitor
  • Visitor
3 years 6 months ago #97466 by nicola_86
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?

Please Log in to join the conversation.

  • nicola_86
  • nicola_86's Avatar Topic Author
  • Visitor
  • Visitor
3 years 6 months ago - 3 years 6 months ago #97481 by nicola_86
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: 3 years 6 months ago by nicola_86. Reason: the message wasn't written well..sorry

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form