I am trying to post a JSON string to a PHP server of mine, but nothing is happening at all on the server side.
Here is the server code:
<?php
$json = $_POST['json'];
$decoded = json_decode($json);
echo($decoded);
?>
And here is the JAVA code:
public void sendError(Error e)
{
String json = e.toJSONString();
try
{
if(url == null)
{
url = new URL("http://www.rstougaard.dk/errorHandler");
}
if(conn == null)
{
conn = (HttpURLConnection) url.openConnection();
}
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
conn.setRequestProperty("json", URLEncoder.encode(json));
output = new DataOutputStream(conn.getOutputStream());
conn.connect();
output.writeBytes("json=" + URLEncoder.encode(json, "UTF-8"));
output.flush();
BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while(input.ready())
{
System.out.println(input.readLine());
}
System.out.println(conn.getResponseCode() + " " + conn.getResponseMessage());
} catch (MalformedURLException ex)
{
Logger.getLogger(ConnectionController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException exc)
{
Logger.getLogger(ConnectionController.class.getName()).log(Level.SEVERE, null, exc);
}
}
The .getResponseCode() and .getResponseMessage() returns '200' and 'OK' respectively. And the inputStream just prints the php code from the site followed by the JSON string, which seems odd to me.
What am i not getting here?
EDIT
I have changed the "GET to "POST" in .setResponseMethod(). But still didnt work.
EDIT
I have tried using curl to check the server response, but it is the same thing as the inputStream in my java code does. It prints the html/php code from the website instead of giving me a response.