I try post json from Android client to PHP server but failed, server cannot receive any data, I don't see what's wrong with it, so I need help, thanks a lot.
this is Android client kernel code:
String result;
String encoding="UTF-8";
String params="{\"name\":\"123\",\"pass\":\"456\"}";
try {
byte[] data=params.getBytes(encoding);
URL urls=new URL(url);
HttpURLConnection conn=(HttpURLConnection) urls.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-javascript; charset="+ encoding);
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.setReadTimeout(4000);
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
BufferedReader bfread=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String str;
StringBuffer resb=new StringBuffer();
while ((str=bfread.readLine())!=null)
{
resb.append(str);
}
result=resb.toString();
and this is server code:
<?php
$data = file_get_contents('php://input');
if(empty($data))
{
echo "no post data";
}
else
{
print_r($data);
}
?>
android client get the message from server and print on screen, they always show "no post data", obviously client visit server successful, but server not received any data. Then I write a test HTML page to post the data with form, it works. I don't know why.