简体   繁体   English

POST请求不适用于所有android

[英]POST request does not work at all android

So I have seen many tutorials to do the POST Request in Android and I have found so many codes but none of them really works and it drives me crazy. 因此,我看了很多教程来在Android中执行POST请求,并且发现了很多代码,但没有一个真正起作用,这使我发疯。

The code does not look false, but I don't know Android is pretty bizarre. 该代码看起来并不虚假,但是我不知道Android是多么奇怪。

Here is my Post request in Android: 这是我在Android中的发布请求:

thread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            String data = URLEncoder.encode("pseudo","UTF-8")+"="+URLEncoder.encode(edit.getText().toString(),"UTF-8");
            data += "&"+"score"+"="+URLEncoder.encode(String.valueOf(score),"UTF-8");
            URL url = new URL("http://mywebsite/serveur.php?"+data);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setReadTimeout(10000);
            connection.setConnectTimeout(15000);
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            OutputStream os = connection.getOutputStream();
            BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
            wr.write(data);
            wr.flush();
            wr.close();
            os.close();
            android.util.Log.d("my Tag",data);
            connection.connect();
            android.content.Intent Npage = new android.content.Intent(SubmiturScore.this, ListOscore.class);
            startActivity(Npage);

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

And the server code in PHP to store data in MySQL database: 然后使用PHP中的服务器代码将数据存储在MySQL数据库中:

<?php

$reponse = array();

$pseudo = urldecode($_POST['pseudo']);
$score = intval(urldecode($_POST['score']));

$mysqli = new MySQLi("myhost","myuser","mypassword","mydb");

$result = $mysqli->query("INSERT INTO SIDB (pseudo,score) VALUES('$pseudo',$score)");
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
        . $mysqli->connect_error);
}
if ($result) {
    $response["success"] = 1;
    $response["message"] = "SUCCESS!";

    echo json_encode($response);
} else {
    $response["success"] = 0;
    $response["message"] = "FAILURE!";

    echo json_encode($response);
} ?>

I have already posted a message in stack overflow but I thought that the code will work but no... Can you please help me ! 我已经在堆栈溢出中发布了一条消息,但是我认为该代码可以正常工作,但不能...您能帮帮我吗!

So i don't know what the f*** happened with the POST on Android. 所以我不知道f ***在Android上的POST发生了什么。 For resolving my problem I just called a webview that will charge the URL with data so the php script can recuperate data and finally store it in the database... 为了解决我的问题,我只是调用了一个webview,它将向URL收取数据,以便php脚本可以恢复数据并将其最终存储在数据库中。

It was tough but I've finally suceeded YES ! 很难,但我终于得出了YES!

    wb.getSettings().setJavaScriptEnabled(true);
    wb.getSettings().setDomStorageEnabled(true);
    wb.loadUrl("http://yourscript.php?"+data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM