douhuang1973 2017-05-18 21:55
浏览 80

通过Android连接到PHP的问题

I am trying to send a JSON object from my android application to my site's database. However, I seem to not get any type of connection and I don't get any exception errors. I am currently trying to get a connection just with java in eclipse but still no luck.

submitAlbum.php:

<?php
if(isset($_POST["albumId"])){
    $albumId = json_decode($_POST["albumId"]);
}

The java Code:

String albumId = "qjlix";

    try{
        //Creates JSON object
        JSONObject tempObj = new JSONObject();
        tempObj.put("albumId", albumId);

        //Creates connection to URL
        URL targetUrl = new URL("http://example.com/submitAlbum.php");
        HttpURLConnection conn = (HttpURLConnection) targetUrl.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setConnectTimeout(500);
        conn.setRequestProperty("Content_Type", "application/json; charset=UTF-8");

        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(tempObj.toString());
        writer.flush();
        writer.close();
    } catch (Exception e){
        System.out.println(e.toString());;
    }

Is their something I am missing or doing wrong?

  • 写回答

2条回答 默认 最新

  • dongsuoxi1790 2017-05-19 04:09
    关注

    You are not sending a POST parameter/key 'albumId' of which the value is a json text. Instead you send a json text containing a json parameter 'albumId'.

    Php script and client code do not match.

    First decide if you want to POST key=value pairs. Or want to POST json text. Then handle accordingly.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?