【问题标题】:php not getting data from androidphp没有从android获取数据
【发布时间】:2015-04-23 10:53:52
【问题描述】:

我正在尝试将 json 格式的数据从 android 发送到 php webservice。 但是我的网络服务在它的响应中没有得到任何东西。 我尝试了什么:

public void postLatLong (double lat, double lon) throws MalformedURLException {
    try {
        URL url = new URL("http://server/path/index.php");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        OutputStream out = connection.getOutputStream();
        OutputStreamWriter outwriter = new OutputStreamWriter(out, "UTF-8");
        BufferedWriter writer = new BufferedWriter(outwriter);
        JSONObject json = new JSONObject();
        json.put("lat", lat);
        json.put("lon", lon);
        Log.d("JSONString",json.toString());
        writer.write(json.toString());
        writer.flush();
        writer.close();
        connection.connect();
        InputStream in = connection.getInputStream();
        InputStreamReader inreader = new InputStreamReader(in, "UTF-8");
        BufferedReader reader = new BufferedReader(inreader);
        Log.d("Response",reader.readLine());
        reader.close();
    } catch (IOException e) {
        Log.d("posting Error", e.getMessage());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d("JSONException", e.getMessage());
    }
}

我正在收到回复。 没有显示错误日志,json是好的,我可以在日志中看到它。但是,当我检查webservice日志时,POST请求中没有任何内容。

【问题讨论】:

    标签: php android httpurlconnection


    【解决方案1】:

    检查您的 PHP 代码/配置标头是否已设置,如果未设置则设置

    header('Content-Type: application/json;charset=UTF-8');
    

    对你有帮助。

    【讨论】:

      猜你喜欢
      • 2018-05-19
      • 1970-01-01
      • 2014-05-26
      • 2013-03-22
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多