android通过php连接mysql,但是内容没有通过

brc7rcf0  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(151)

我有一个问题我一整天都在想办法解决。也许你们中有人知道我哪里出错了?
我试图让android活动通过php将一个值发布到mysql数据库中。当我将php脚本与服务器同一目录中的html文件一起使用时,它运行良好。因此,我非常肯定,android文件有问题。但是:android文件到达php文件,并在数据库中创建一个条目。但值总是空的。
我有一个像这样的android活动:

public class MainActivity extends AppCompatActivity {
    public static final MediaType MEDIA_TYPE =
            MediaType.parse("application/json");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final OkHttpClient client = new OkHttpClient();
        JSONObject postdata = new JSONObject();
        try {
            postdata.put("Mail", "Tim");
            postdata.put("Password", "44");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        RequestBody body = RequestBody.create(MEDIA_TYPE,
                postdata.toString());

        final Request request = new Request.Builder()
                .url("MYURL")
                .post(body)
                .addHeader("Content-Type", "application/json") // 
                .addHeader("Authorization", "Your Token") //               
                .addHeader("cache-control", "no-cache")
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String mMessage = e.getMessage().toString();
                Log.w("failure Response", mMessage);
                //call.cancel();
            }

            @Override
            public void onResponse(Call call, Response response)
                    throws IOException {

                String mMessage = response.body().string();
                if (response.isSuccessful()) {
                    try {
                        JSONObject json = new JSONObject(mMessage);
                        final String serverResponse = json.getString("Your Index");

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}

像这样的php文件:

<?php
echo 'PHP-File works<br>';
$link = mysqli_connect("MYHOSTNAME", "USERNAME", "PASSWORD",                             
"DBNAME");
if (!$link) {
echo 'Link doesnt work';
}
if (mysqli_connect_errno()) {
echo 'Connection Error';
}

$Mail = mysqli_real_escape_string($link, trim($_POST['Mail']));
$Password = mysqli_real_escape_string($link, trim($_POST['Password']));

if(!mysqli_query($link, 'INSERT INTO Volley_Sample_2 (email, password)         
 VALUES ("hardcode_test",4)')){
echo ("Error description: " . mysqli_error($link));
}

$query = "INSERT INTO Volley_Sample_2 (email, password) VALUES('$Mail',     
'$Password')";
mysqli_query($link,$query) or trigger_error(mysqli_error($link)." in 
".$query);
mysqli_close($link);

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题