0

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.

Pogoe
  • 36
  • 7

2 Answers2

0

Might be that you are sending a GET request from your client code:

conn.setRequestMethod("GET");

while server checks the POST. Try changing it to "POST"

Also, as Evgeny Lebedev suggested, you might try to test your server response beforehand with something like CURL or the likes

micklesh
  • 417
  • 1
  • 4
  • 16
  • I have tried changing it to POST, but it is the same thing.. and how would i go about using CURL? – Pogoe Apr 10 '16 at 19:46
  • Pogoe curl is a tool that is able to send requests, including POST ones, to the server. In fact the same thing is possible using web developer tools in browser. The idea is just to check if for a correct request your server returns the response you want to get. And if the server works as expected - continue with the changes in Java code – micklesh Apr 10 '16 at 20:11
  • for CURL example see [link](http://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest) and for the browser - open web dev tools (I'm using FF so F12 works for mee), switch to Network tab and resubmit a post request to your server using correct headers and content – micklesh Apr 10 '16 at 20:14
0

I figured out what was wrong. I was basically a fool and thought that if I ran the code I could check if any output happened in chrome, but of course it didn't, because that is a different request and they don't collide.

The above code is working as intended in my case. But thank you guys for your help anyway and sorry for the trouble!

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
Pogoe
  • 36
  • 7