Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting a url containing an underscore (_)

Tags:

android

I am posting a url with params containg an underscore (_).

sample: http://sdsdsds_asasasahjhd.com/dsdsdsd/login.json?

I am posting it like this:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://sdsdsds_asasasahjhd.com/dsdsdsd/login.json?");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("key1", "value1"));
        nameValuePairs.add(new BasicNameValuePair("key2", "value2"));
        nameValuePairs
                .add(new BasicNameValuePair("key3", "value3"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

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

When I am inspecting httpclient.execute(httppost) I am getting IllegalArgumentException and in catch in exception details it is telling Host name cannot be null. Please specify any solution.

I have gone through some other questions here:

  • java.lang.IllegalStateException: Target host must not be null, or set in parameters
  • Host name may not be null in HttpResponse execute for android

but no use as I am not encoding the whole url.

like image 262
zaiff Avatar asked Nov 12 '22 16:11

zaiff


1 Answers

I have an open-source library with network implementation mechanism. It has just receiver an workaround implementation. All you need it to set the host by reflection in case of troubles:

        final URI uriObj = new URI("https", host, path, null, null);
        if (uriObj.getHost() == null) {
            final Field hostField = URI.class.getDeclaredField("host");
            hostField.setAccessible(true);
            hostField.set(uriObj, host);
        }
        return uriObj;

The commit is here.

like image 198
Vladimir Ivanov Avatar answered Nov 15 '22 04:11

Vladimir Ivanov