【问题标题】:php could not receive data sent by Android using HttpURLConnectionphp 无法接收 Android 使用 HttpURLConnection 发送的数据
【发布时间】:2015-07-06 14:32:43
【问题描述】:

我正在尝试使用 OutputStreamWriter 将一些数据从 Android 发送回服务器(数据库),但我在数据库中看不到任何结果,并且 Logcat 很好。我尝试了在 stackoverflow 上发布的解决方案,但没有运气。这是我的代码:-

private class sentFeedback extends AsyncTask<String,Void,String>{
    @Override
    protected String doInBackground(String... params) {
        if(isNetworkAvailable()){
            try{
                URL feedback = new URL("http:.../setFeedback.php");
                HttpURLConnection conn = (HttpURLConnection) feedback.openConnection();
                conn.setDoOutput(true);
                conn.setRequestMethod("POST");
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                String data = getData(params);
                wr.write(data);
                wr.flush();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            return "No internet connection";
        }
        return null;
    }

    public String getData(String... params) throws UnsupportedEncodingException {

        String data = "id" + "=" + URLEncoder.encode(params[0], "UTF-8");
        data += "&" + "rating" + "=" + URLEncoder.encode(params[1], "UTF-8");
        data += "&" + "message" + "=" + URLEncoder.encode(params[2], "UTF-8");
        return data;
    }
}

php代码:-

$id = $_POST['id'];
$rating = (double)$_POST['rating'];
$message = $_POST['message'];

$feedbackQuery = "INSERT INTO feedback(message, id, rating) VALUES ('$message', '$id', '$rating')";
$feedbackQueryResult = $mysqli->query($feedbackQuery);

$mysqli->close();

目前我正在使用免费的 000webhost 帐户。这就是我可以发回数据的原因吗?

提前致谢!!

【问题讨论】:

  • 您好,您找到这个问题的答案了吗?我已经被这个问题困扰了一个多星期了。如果您找到了解决方案,请您使用 android 和 php 代码发布答案:)

标签: php android database


【解决方案1】:

在 php 文件中将 tablenName 替换为查询字符串中的 dbName.tableName,例如,如果数据库名称为 mydb

$feedbackQuery = "INSERT INTO mydb.feedback(message, id, rating) VALUES ('$message', '$id', '$rating')";

再次测试,在一个项目中我的问题解决了这个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 2012-02-16
    相关资源
    最近更新 更多