Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

java connection with limesurvay

  • manavaahuja07
  • manavaahuja07's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
6 years 11 months ago - 6 years 11 months ago #150905 by manavaahuja07
java connection with limesurvay was created by manavaahuja07
HI,

I am trying to connect with java. using this guide line its give me error.

I also refer this
I added some line which is System.out.println(); which is for my reference what i am getting output.

Code:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
 
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
 
public class GetData {
 
    public static String parse(String jsonLine) {
     System.out.println(jsonLine);
   JsonElement jelement = new JsonParser().parse(jsonLine);
   JsonObject  jobject = jelement.getAsJsonObject();
   String result = jobject.get("result").getAsString();
   return result;
  }
 
 
    public static void main(String[] args) throws UnsupportedEncodingException {
      DefaultHttpClient client = new DefaultHttpClient();          
 
      HttpPost post = new HttpPost("http://localhost/index.php/admin/remotecontrol");
      post.setHeader("Content-type", "application/json");
      post.setEntity( new StringEntity("{\"method\": \"get_session_key\", \"params\": [\"admin\", \"password\" ], \"id\": 1}"));
      try {
        HttpResponse response = client.execute(post);
        if(response.getStatusLine().getStatusCode() == 200){
           System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
 
          String sessionKey = parse(EntityUtils.toString(entity));
            post.setEntity( new StringEntity("{\"method\": \"list_groups\", \"params\": [ \""+sessionKey+"\", \"557974\" ], \"id\": 1}"));
            response = client.execute(post);
            if(response.getStatusLine().getStatusCode() == 200){
                entity = response.getEntity();
                System.out.println(EntityUtils.toString(entity));
                }
           }
 
 
      } catch (IOException e) {
        e.printStackTrace();
      }
    }}
 


and my output is like this.
Code:
 
HTTP/1.1 200 OK
<br />
<b>Deprecated</b>:  Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in <b>Unknown</b> on line <b>0</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent in <b>Unknown</b> on line <b>0</b><br />
<!DOCTYPE html PUBLIC
  "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Bad Request</title>
<style type="text/css">
/*<![CDATA[*/
body {font-family:"Verdana";font-weight:normal;color:black;background-color:white;}
h1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
h2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
h3 {font-family:"Verdana";font-weight:bold;font-size:11pt}
p {font-family:"Verdana";font-weight:normal;color:black;font-size:9pt;margin-top: -5px}
.version {color: gray;font-size:8pt;border-top:1px solid #aaaaaa;}
/*]]>*/
</style>
</head>
<body>
<h1>Bad Request</h1>
<h2>The CSRF token could not be verified.</h2>
<p>
The request could not be understood by the server due to malformed syntax.
Please do not repeat the request without modifications.
</p>
<p>
If you think this is a server error, please contact the webmaster.
</p>
<div class="version">
2017-03-21 14:06:33 </div>
</body>
</html>
Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 6
  at com.google.gson.JsonParser.parse(JsonParser.java:65)
  at com.google.gson.JsonParser.parse(JsonParser.java:45)
  at GetData.parse(GetData.java:19)
  at GetData.main(GetData.java:38)
Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 6
  at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1310)
  at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:963)
  at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:910)
  at com.google.gson.stream.JsonReader.peek(JsonReader.java:384)
  at com.google.gson.JsonParser.parse(JsonParser.java:60)
  ... 3 more
 
 



Need some HELP to solve this pro.
Last edit: 6 years 11 months ago by manavaahuja07.
The topic has been locked.
More
6 years 11 months ago #150914 by jelo
Replied by jelo on topic java connection with limesurvay
What version/build of LimeSurvey is used?
What version of PHP is used?

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
  • manavaahuja07
  • manavaahuja07's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
6 years 11 months ago - 6 years 11 months ago #151024 by manavaahuja07
Replied by manavaahuja07 on topic java connection with limesurvay
i am using limesurvey2.64.1+170310-on-xampp-win32-setup.

limesurvery 2.64.1 and
PHP 5.6.8 (11 X86 32bit thread safe) + PEAR


Which is latest version available on website from here www.limesurvey.org/stable-release .
Last edit: 6 years 11 months ago by manavaahuja07.
The topic has been locked.
  • manavaahuja07
  • manavaahuja07's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
6 years 11 months ago #151075 by manavaahuja07
Replied by manavaahuja07 on topic java connection with limesurvay
OK Prob is solved thanx for you reply.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 11 months ago #151169 by DenisChenu
Replied by DenisChenu on topic java connection with limesurvay
What is the issue ?

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • manavaahuja07
  • manavaahuja07's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
6 years 11 months ago #151802 by manavaahuja07
Replied by manavaahuja07 on topic java connection with limesurvay
I just set this always_populate_raw_post_data to -1 so its started giving me proper json object then i am able to find the result. before its giving me xml that why its not converted in JSON obj.
The following user(s) said Thank You: DenisChenu
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose