drgwsx8405 2014-09-27 15:30
浏览 14
已采纳

如何在php中提取POST参数

From android app, I am calling my php script.

HttpPost httppost = new HttpPost("http://www.smth.net/some.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("pa", "555"));
nameValuePairs.add(new BasicNameValuePair("pb", "550"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}

How do I get the value of "pa" and "pb" in my php script.

I did $_POST['pa'] and urldecode($_POST['pa']) but both of these give me empty string.

  • 写回答

2条回答 默认 最新

  • douti19680318 2014-09-27 15:42
    关注

    You can use print_r($_POST) to debug what is being sent.

    If it helps, this is how I send info from Android using POST:

    JSONObject requestBody = new JSONObject();
    requestBody.put("pa", "550");
    requestBody.put("pb", "550");
    
    HttpPost requestBase = new HttpPost("url");
    StringEntity stringEntity = new StringEntity(requestBody.toString());
    stringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    requestBase.setEntity(stringEntity);
    
    HttpResponse response = httpclient.execute(requestBase);
    

    I'm working from memory here though - there are a couple of try/catches you'll need to insert.

    Using a JSONObject might be unnecessary, but I found it gave me better flexibility and reliability.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)