简体   繁体   中英

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

Hello I shall get straight to the point, I am trying to use a EditText input to use in a php url i want the url to to be like thi when it runs.

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

SO I tried

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

but i get the error

在此处输入图片说明

Here is the main part of my code.

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

I'm not sure what are you trying to do.

If you want to add the content of the EditText to your URL try:

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

You can't append EditText with String . You have to append input from EditText .

To get input from EditText use -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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