Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an application (Java) that needs to send json to a php web service.

This is my method to send User in JSON :

public void login(User user) throws IOException {
    Gson gson = new Gson();
     String json = gson.toJson(user);
     System.out.println(json);
      String url = "http://localhost/testserveur/index.php";
     URL obj = new URL(url);
     HttpURLConnection con = (HttpURLConnection)obj.openConnection();

     con.setRequestMethod("POST");
     con.setRequestProperty("json", json);

     con.setDoOutput(true);
     try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
         wr.flush();
     }

     int responseCode = con.getResponseCode();
     System.out.println(responseCode);

 }

And my php code :

$string=$_POST['json'];

I tried to insert in my database but $_POST['json'] does not exist.

share|improve this question
    
check your received information using debug syntax print_r($_POST); and verify your post is success –  Sundar Feb 1 '14 at 14:23

3 Answers 3

I didn't see you to posting anything. Add this to your code:

String param = "json=" + URLEncoder.encode(json, "UTF-8");
wr.write(param.getBytes());
share|improve this answer

This is not right:

con.setRequestProperty("json", json);

setRequestProperty is not used to set the HTTP payload. It is used to set the HTTP headers. For example, you should set the content type accordingly anyway. Like this:

con.setContentType("application/json");

The actual data that you are going to post goes into the HTTP body. You just write it to the end of the stream (before flush):

Here it depends on your implementation on the web server if the data needs to be escaped. If you read the body of the post and interpret it as JSON straight away, it does not need to be escaped:

wr.write(json);

If you transmit one or more JSON strings through parameters (which it looks like, since you are parsing it on the server like $_POST['json']), then you need to url-escape the string:

wr.write("json=" + URLEncoder.encode(json, "UTF-8"));

Im not very familiar with php. You might need to url-decode the string on the server before processing the received json-string any further.

share|improve this answer

Thx for your help.

This works :

public void login(User user) throws IOException {

    Gson gson = new Gson();
    String json = gson.toJson(user);
    System.out.println(json);

    String url = "http://localhost/testserveur/index.php";
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setDoOutput(true);
    con.setRequestMethod("POST");
    con.setRequestProperty("json", json);


    OutputStream os = con.getOutputStream();
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    //wr.write(new String("json=" + json).getBytes());
    String param = "json=" + URLEncoder.encode(json, "UTF-8");
    wr.write(param.getBytes());

    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println(responseCode);

}

PHP :

$string=$_POST['json'];

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.