Having problem sending JSON via Httppost in Java, please help
<p>I'm working on a school project, which requires JAVA to send a series of barcodes to a php web service front end. I've looked at a couple of posts <a href="http://stackoverflow.com/questions/2540786/android-json-httpclient-to-send-data-to-php-server-with-httpresponse">here</a> and <a href="http://stackoverflow.com/questions/3027066/how-to-send-a-json-object-over-request-with-android">here</a>,and taken something from them, but my code doesn't seem to work.</p>

<p>So here's my JAVA code:</p>

<pre><code>import org.json.simple.JSONObject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.entity.StringEntity;
import org.apache.http.HttpResponse;
import java.io.InputStream;


public class jsontest2 {
    public static void main (String Args[]) {
        JSONObject obj=new JSONObject();
        JSONObject codes=new JSONObject();
        //Forms the name value pairs for the barcodes and quantity
        codes.put("598013", new Integer(10));
        codes.put("5849632927",new Integer(15));
        codes.put ("5849676037",new Integer(20));
        codes.put ("6634391931", new Integer(25));

        //Headers in the JSON array
        obj.put("LoginID", new Integer(1234));
        obj.put("Machine_ID", new Integer(123456789));
        obj.put("add", codes);
        System.out.print(obj);

        //httppost
        try {
            HttpClient httpclient= new DefaultHttpClient();
            HttpResponse response;
            HttpPost httppost= new HttpPost ("http://example/receive.php");
            StringEntity se=new StringEntity ("myjson: "+obj.toString());
            httppost.setEntity(se);
            System.out.print(se);
            httppost.setHeader("Accept", "application/json");
            httppost.setHeader("Content-type", "application/json");

            response=httpclient.execute(httppost);

        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.print("Cannot establish connection!");
        }
    }
}
</code></pre>

<p>And to test the httppost request, I've made this php file to log every request.</p>

<pre><code>&lt;?php
$input = stripslashes($_POST["myjson"]);
logToFile("post.txt", $input);

function logToFile($filename, $msg)
{
    $fd = fopen($filename, "a");
    $str ="[".date("Y/m/d h:i:s")."]".$msg;
    fwrite($fd, $str."\n");
    fclose($fd);
}
</code></pre>

<p>The problem is, in log file log.txt, every time I run the JAVA program, it only adds a new line consisting time and date. To me, this means that php is picking up that there was a httppost request, but where is my json string? </p>

<p>Can anyone tell me what am I doing wrong? I've been stuck here for a while. Thanks!</p>
