【问题标题】:Android_Passing parameter URLAndroid_Passing 参数地址
【发布时间】:2017-05-15 14:52:09
【问题描述】:

我想通过我的 Android 应用程序在网站上发送一个变量,所以我尝试了这个:

    OutputStreamWriter writer = null;

    URLConnection connexion = null;

    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            String data = msg.getData().getString("receivedData");

            long t = System.currentTimeMillis();
            if(t-lastTime > 100) {     // Pour eviter que les messages soit coupes
                ReceptionDetect.append("\n");
                lastTime = System.currentTimeMillis();
            }
            ReceptionDetect.append(data);

            try {

                // Encodage des paramètres de la requête
                String donnees = URLEncoder.encode("Variable="+data);

                // On a envoyé les données à une adresse distante
                URL url = new URL("http://MyWebsite/myPhp.php");
                connexion = url.openConnection();
                connexion.setDoOutput(true);
                //connexion.setChunkedStreamingMode(0);

                // On envoie la requête ici
                writer = new OutputStreamWriter(connexion.getOutputStream());
                // On insère les données dans notre flux
                writer.write(donnees);
                // Et on s'assure que le flux est vidé
                writer.flush();

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

            } finally {
                try{writer.close();}catch(Exception e){}


        }
    }
    };

在我的网站上使用这个 php:$_GET['Variable']; echo 'Le détecteur a répondu : '; echo ($Variable);

或者也可以在php中打开一个txt文件:

<?php

$_GET['Variable'];


 // Open the text file
	$f = fopen("TextRecevoir.txt", "w");
// Write text
	fwrite($f, $_GET["Variable"]);
// Close the text file
	fclose($f);
// Open file for reading, and read the line
	$f = fopen("TextRecevoir.txt", "r");
	

?>

但它不起作用,应用程序启动但php文件中没有写入任何内容。 如果有人可以帮助我,我不明白为什么它不起作用。 谢谢你

-> 是的,我将互联网权限放在清单中。

【问题讨论】:

  • 为什么不用凌空抽射?
  • 好吧,我已经看过开发人员 (developer.android.com/training/volley/simple.html) 上的教程,我目前正在尝试但没有成功,但我也想知道为什么我的代码不起作用。
  • 老实说,复制粘贴 volley 代码比调试代码要快得多。当涉及到您的代码时,您在哪里将参数附加到您的网址?
  • 最后我的代码是对的,错误在 php 文件中...

标签: javascript php android url


【解决方案1】:

我会给你一个例子,我是如何做到的。
也许有编译错误,因为我把它做短了,所以你可以更好地阅读它。
它是 Async Task 的类扩展。

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Check extends AsyncTask<String, String, String> {
    protected void onPreExecute() {
        super.onPreExecute();
    }

    protected String doInBackground(String... params) {
        String urlString="http://myserver.com/add.php"; // URL to call
        String resultToDisplay = "";

        InputStream in = null;
        try {
            URL url= new URL(urlString+"?hello="+params[0]);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(3000);
            urlConnection.setConnectTimeout(3000);
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            in = new BufferedInputStream(urlConnection.getInputStream());
        } catch (Exception e) {
            Log.e("marketdebug",e.getMessage());
            error=true;
            error_resp=e.getMessage();
            return e.getMessage();
        }
        try {
            resultToDisplay = IOUtils.toString(in, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultToDisplay;
    }
    protected void onPostExecute(String result) {
        Log.d("DEBUG", "RESULT:"+result);
    }

}

执行:

    Check saveData = new Check();

    String myVariable="TEST";
    saveData.execute(myVariable);

【讨论】:

  • 其实错误出在我的 php 中,所以你的代码可以工作!!
猜你喜欢
  • 2013-09-05
  • 1970-01-01
  • 2016-04-03
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
相关资源
最近更新 更多