繁体   English   中英

Java:将字符串转换为在get中使用的url

[英]Java : Convert string to use in url as part of a get

您好,我会直截了当地说,我正在尝试使用EditText输入在php url中使用,我希望该URL在运行时像thi一样。

http://www.free-map.org.uk/course/mad/ws/hits.php?artist=Oasis

所以我尝试了

 URL url = new URL("http://www.free-map.org.uk/course/mad/ws/hits.php?artist=",et1);

但是我得到了错误

在此处输入图片说明

这是我的代码的主要部分。

 class MyTask extends AsyncTask<Void,Void,String>
{
    EditText et1 = (EditText)findViewById(R.id.et1);

    public String doInBackground(Void... unused)
    {
        HttpURLConnection conn = null;
        try
        {
            URL url = new URL("http://www.free-map.org.uk/course/mad/ws/hits.php?artist=",et1);
            conn = (HttpURLConnection) url.openConnection();
            InputStream in = conn.getInputStream();
            if(conn.getResponseCode() == 200)
            {
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String result = "", line;
                while((line = br.readLine()) !=null)
                    result += line;
                return result;
            }
            else
                return "HTTP ERROR: " + conn.getResponseCode();
        }
        catch(IOException e)
        {
            return e.toString();
        }
        finally
        {
            if(conn!=null)
                conn.disconnect();
        }
    }

我不确定您要做什么。

如果要将EditText的内容添加到URL,请尝试:

URL url = new URL("http://www.free-map.org.uk/course/mad/ws/hits.php?artist="+et1.getText().toString());

您不能在EditText后面加上String 您必须从EditText追加输入。

要从EditText获取输入,请使用-

EditText et1 = (EditText)findViewById(R.id.et1);
final String input = et1.getText().toString();
// then append it
URL url = new URL("http://www.free-map.org.uk/course/mad/ws/hits.php?artist="+input);

暂无
暂无

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

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