Httpconnection 无法正确打开 link

Httpconnection won't open a link properly

我有一个应用程序,非常简单。在准备字典时,它应该使用单词并将其输入数据库。输入部分由 php 脚本完成,效果很好。生成的 link 也有效,我手动尝试过。我可以问一下,我在这段代码中监督了什么,因为我还没有发现任何关于 Google:

的话题
    private class SendData extends AsyncTask<Void, Void, Void>{
    @Override
    protected Void doInBackground(Void... voids) {
        HttpURLConnection urlConnection = null;
        try {
            URL url = new URL(uri.toString());
            EditText text = (EditText) findViewById(R.id.oshikwanyamaEx);
            text.setText(url.toString());
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.connect();
        }catch (MalformedURLException m){
            m.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (urlConnection != null){
                urlConnection.disconnect();
            }
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        Toast.makeText(MainActivity.this, "Data was sent", Toast.LENGTH_LONG).show();
        super.onPostExecute(aVoid);
    }
}

提前致谢 这是 PHP 代码:

<?php 

    //Creating a connection
    $con = mysqli_connect("breidinga.lima-db.de:3306","USER373834","*********","db_373834_1");

    if (mysqli_connect_errno())
    {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    } 
    //INSERT INTO `ENGOKY` (`_id`, `eng`, `oky`, `type`, `engex`, `okyex`, `okypl`, `engpl`) VALUES (NULL, '', '', '', NULL, NULL, NULL, NULL)
    $sql = "INSERT INTO `ENGOKY` (`_id`, `eng`, `oky`, `type`, `engex`, `okyex`, `okypl`, `engpl`) VALUES (NULL,'".$_GET["eng"]."', '";
    //$sql = $sql.$_GET["oky"]."', '".$_GET["type"]."', '".$_GET["engex"]."', '".$_GET["okyex"]."', '".$_GET["okyex"]."', '".$_GET["ekypl"]."', '".$_GET["engpl"]."')";
$sql = $sql.$_GET["oky"]."', '".$_GET["type"]."', NULL, NULL, NULL, NULL)";
  $sql = str_replace("''","NULL", $sql);
    mysqli_query($con ,$sql);
    mysqli_free_result($result);
    mysqli_close($con);
 ?>

对您的代码稍作改进,并增加一些声明

        URL url = new URL("your URL here");

        JSONObject postDataParams = new JSONObject();
        postDataParams.put("word", "onomatopoeia"); //Change #1
        Log.e("params",postDataParams.toString());

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000); //Change #2
        conn.setConnectTimeout(15000); //Change #2
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

         OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(postDataParams));

            writer.flush();
            writer.close();
            os.close();

1 - 您需要将参数传递到您的 PHP 文件中,因为我看不到任何传递的证据。 假设变量类似于以下内容:-

$word= $_POST['word'];

2 - 设置连接超时和读取超时总是有用的,因为这将在一段时间后终止请求。在这种情况下,它是 15 秒。

3 - 作为指导,只需确保 PHP 文件有 return 响应,因为您想要确定插入是否成功。

4 - 添加以下权限,可能会有所不同

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

5 - 从连接中读取响应代码,这将确定它是否愿意做任何事情。

int response = conn.getResponseCode();
        Log.d(DEBUG_TAG, "Response code is: " + response);

        if ((response >= 200) && (response < 300)) {
            Log.d(DEBUG_TAG, "The response is: " + conn.getResponseMessage());
        }

6.用户收到响应代码 301(重定向)并添加此行以停止重定向

conn.setInstanceFollowRedirects(false);