无法在PHP中使用HttpUrlConnection读取android的请求

时间:2016-02-17 03:02:47

标签: php android httpurlconnection

以下是我的安卓代码

...

protected Void doInBackground(JSONObject...params){

...

//connecting and writing request to server
         mycon.connect();
         OutputStream out = mycon.getOutputStream();
         out.write(params[0].toString().getBytes(StandardCharsets.UTF_8));
         Log.d("jp2: Your Request :",params[0].toString());
         //out.flush();
         int status = mycon.getResponseCode();
         BufferedReader br=new BufferedReader(new InputStreamReader(mycon.getInputStream()));
         response=br.readLine();
         Log.d("jp2", "Your response from php "+response+" : " + status);
...

我的php代码向android返回相同的请求

...
$con=new mysqli($servername,$username,$password,$database);
$json = file_get_contents('php://stdin');
echo "Hello from PHP your request is :";
echo $json;
...

这是我在android monitor中的输出

02-17 08:11:58.515 7865-7880/com.srt.ujn.am D/jp2: Your Request :: {"mobile_no":"1234567890","cust_name":"jai","address":"surat","city":"Split AC","prod_cat":"Split AC","issue_type":"Repair"}

02-17 08:11:58.540 7865-7880/com.srt.ujn.am D/jp2: Your response from php Hello from PHP your reqest is :  : 200

你可以看到我从php输出为null。有人可以帮忙吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我更改了以下行

$json = file_get_contents('php://stdin');

$json = file_get_contents('php://input');

获得所需的输出

02-17 09:04:42.145 7865-7879/com.srt.ujn.am D/jp2: Your Request :: {"mobile_no":"1234567890","cust_name":"jai","address":"surat","city":"Split AC","prod_cat":"Split AC","issue_type":"Repair"}
02-17 09:04:42.159 7865-7879/com.srt.ujn.am D/jp2: Your response from php Hello from PHP your reqest is :{"mobile_no":"1234567890","cust_name":"jai","address":"surat","city":"Split AC","prod_cat":"Split AC","issue_type":"Repair"}  : 200