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("https://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"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://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.