Php 从应用程序向服务器发送字符串

Php 从应用程序向服务器发送字符串,php,android,mysql,database,android-studio,Php,Android,Mysql,Database,Android Studio,我一直在开发一个应用程序,它将向我服务器上的数据库发送字符串,但是由于某种原因,没有收到任何数据。也许你可以给我指出正确的方向,我一直在网上搜索,但找不到它不起作用的原因 我的android代码: public class HttpURLConnectionHandler { protected String urlG = "http://example.com/"; public String sendText(String text) {

我一直在开发一个应用程序,它将向我服务器上的数据库发送字符串,但是由于某种原因,没有收到任何数据。也许你可以给我指出正确的方向,我一直在网上搜索,但找不到它不起作用的原因

我的android代码:

    public class HttpURLConnectionHandler
    {
    protected String urlG = "http://example.com/";
    public String sendText(String text)
    {

        try {
            URL url = new URL(urlG+"phpcode.php");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");

            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.connect();
            DataOutputStream wr = new DataOutputStream(
                    conn.getOutputStream());

            wr.writeBytes("mydata:"+text);
            wr.flush();
            wr.close();

            InputStream is = conn.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            while((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            return response.toString();
        }
        catch(Exception e){ return "error";}
    }

}
我的php代码:

<?php
$servername = "here is my server";
$username = "my username";
$password = "my pass";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}



$image = $_POST['image'];


$sql = "INSERT INTO photos (image)
VALUES ('{$image}')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?> 

我让你四处看看

你试图在你的PHP中获取POST变量image的值,但是你发送变量mydata

Json在服务器到客户端的通信方面会是一个更好的选择我不确定你在这里想做什么,但首先你的android代码和PHP代码不匹配,它们都是为了不同的目的而写的。第二,尝试一些库,如volley或okhttp,用于网络请求。该应用程序的主要目的是向我服务器上的数据库发送字符串,我对此非常陌生,这就是为什么我有点为难的原因。所以,如果我想向数据库发送字符串,我应该使用什么来代替POST?$image=$\u POST['mydata'];我从未为android编写过代码,但我认为您的代码从未向POST发送“image”变量。您可以通过添加以下内容查看POST请求的所有值:var\u dump($\u POST);添加到PHP代码中。
HttpURLConnectionHandler handler= new HttpURLConnectionHandler();
String response = handler.sendText("this is a text");