【问题标题】:Httpconnection won't open a link properlyHttpconnection 无法正确打开链接
【发布时间】:2018-09-28 21:17:29
【问题描述】:

我有一个应用程序,非常简单。在准备字典时,它应该获取单词并将其输入数据库。输入部分由 php 脚本完成,效果很好。生成的链接也可以,我手动尝试了。请问,我在这段代码中监督了什么,因为我在 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);
 ?>

【问题讨论】:

    标签: android httpconnection


    【解决方案1】:

    对您的代码进行一点改进并进行更多声明

            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 文件的返回响应,就可以确定插入是否成功。

    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);
    

    【讨论】:

    • 我提前一步输入了参数:
    • 是的,我已经激活了互联网权限
    • 我做到了。它工作了一次,没有第二次。它打印的代码是 301
    • 301 是重定向响应码。这说明应用程序正在成功地与 URL 通信horray。现在这取决于你如何处理这个服务器端。共享 PHP 脚本?
    • 好吧,我想有人在尝试链接。它出现在数据库中。不是应用程序起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    相关资源
    最近更新 更多