Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何根据用户输入添加GET参数?_Android_Http_Get - Fatal编程技术网

Android 如何根据用户输入添加GET参数?

Android 如何根据用户输入添加GET参数?,android,http,get,Android,Http,Get,我正在创建一个接受用户输入的应用程序,并希望它将其作为GET参数传递给HTTP请求 我的问题是,如何将输入传递给GET请求 例如,假设输入是10,请求URL应该是: http://example.com/?no=10&otherparam=some_stuff_here 我知道如何发出GET请求,但不知道如何从用户输入添加参数查找编辑文本: EditText et = (EditText) findViewById(R.id.edittext_id); //replace with y

我正在创建一个接受用户输入的应用程序,并希望它将其作为GET参数传递给HTTP请求

我的问题是,如何将输入传递给GET请求

例如,假设输入是
10
,请求URL应该是:

http://example.com/?no=10&otherparam=some_stuff_here
我知道如何发出GET请求,但不知道如何从用户输入添加参数查找编辑文本:

EditText et = (EditText) findViewById(R.id.edittext_id); //replace with your ID
获取用户输入:

String input = et.getText().toString();

现在,用户输入了一个名为
input
的变量。HTTP GET参数只是添加到URL的参数

假设您有以下URL:

http://example.com/search.php?keywords=some_keywords
如果您想添加,比如,
language
参数,只需将其附加到URL中,如下所示:

http://example.com/search.php?keywords=some_keywords&language=English

现在使用所需参数打开与服务器的连接

URLConnection con = new URL("http://example.com/?exampleparam=" + input).openConnection();
并使用输入/输出流从/向服务器接收和发送:

con.getInputStream();
con.getOutputstream();

注意

由于您使用的是Android,因此无法在主UI线程上执行网络操作,请打开并启动新线程,如:

Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
        //Run your network operations here
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //Update UI here
            }
        });
    }
});
t.start();

您也可以使用AsyncTask

我不需要服务器响应,只要发出请求就足够了。谢谢你的快速回复,让我试试。