java connection with limesurvay

More
8 months 3 weeks ago - 8 months 3 weeks ago #150905 by manavaahuja07
manavaahuja07 created the topic: java connection with limesurvay
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.

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.
 
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: 8 months 3 weeks ago by manavaahuja07.

Please Log in to join the conversation.

More
8 months 3 weeks ago #150914 by jelo
jelo replied the topic: java connection with limesurvay
What version/build of LimeSurvey is used?
What version of PHP is used?

Please Log in to join the conversation.

More
8 months 3 weeks ago - 8 months 3 weeks ago #151024 by manavaahuja07
manavaahuja07 replied the 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: 8 months 3 weeks ago by manavaahuja07.

Please Log in to join the conversation.

More
8 months 3 weeks ago #151075 by manavaahuja07
manavaahuja07 replied the topic: java connection with limesurvay
OK Prob is solved thanx for you reply.

Please Log in to join the conversation.

More
8 months 3 weeks ago #151169 by DenisChenu
DenisChenu replied the 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 (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

More
8 months 2 weeks ago #151802 by manavaahuja07
manavaahuja07 replied the 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

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!