Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6893597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:40:43+00:00 2026-05-27T06:40:43+00:00

Consider this following code (that retrieves the response from a HTTP request and prints

  • 0

Consider this following code (that retrieves the response from a HTTP request and prints it). NOTE: This code works in a standard Java application. I only experience the problem listed below when using the code in an Android application.

public class RetrieveHTMLTest {

public static void main(String [] args) {
    getListing(args[0);
}

public static void getListing(String stringURL) {

    HttpURLConnection conn = null;
    String html = "";
    String line = null;
    BufferedReader reader = null;
    URL url = null;

    try {
        url = new URL(stringURL);

        conn = (HttpURLConnection) url.openConnection();

        conn.setConnectTimeout(6000);
        conn.setReadTimeout(6000);
        conn.setRequestMethod("GET");

        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        conn.connect();

        while ((line = reader.readLine()) != null) {
            html = html + line;
        }

        System.out.println(html);

        reader.close();
        conn.disconnect();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {

    }
}   
}

If I supply the URL:
http://somehost/somepath/

The following code works fine. But, if I change the URL to:
http://somehost/somepath [a comment]/
The code throws a timeout exception because of the “[” and “]” characters.

If I change the URL to:
http://somehost/somepath%20%5Ba%20comment%5D/
The code works fine. Again, because the “[” and “]” characters aren’t present.

My question is, how do I get the URL:

http://somehost/somepath [a comment]/

into the following format:

http://somehost/somepath%20%5Ba%20comment%5D/

Also, should I continue using HttpURLConnection in Android since it can’t accept a URL with special characters? If the standard to always convert the URL before using HttpURLConnection?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:40:44+00:00Added an answer on May 27, 2026 at 6:40 am

    Use the URLEncoder class :

    URLEncoder.encode(value, "utf-8");
    

    You can find more details here.

    Edit : You should use this method only to encode your parameter values. DO NOT encode the entire URL. For example if you have a url like : http://www.somesite.com?param1=value1&param2=value2
    then you should only encode value1 and value2 and then form the url using encoded versions of these values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code snippet in Java. I know that the statement temp[index] =
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider the following code: <?php $conn = mysql_connect('localhost', 'username', 'password'); mysql_select_db('database', $conn); ?> This
Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
Sometimes JavaScript doesn't make sense to me, consider the following code that generates a
Consider the following code, the first demonstrates that the cleanup executes when we're finished
Consider the following code: Dim sql = SELECT * FROM MyTable WHERE value1 =
Consider the following code: function Dog() { this.foo = function() {}; this.walk = function()
Consider the following code: from numpy import log2 import matplotlib.pyplot as plt xdata =
Consider the following code (you can just put this in the developer console in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.