Search code examples
androidsubdomain

Problems Posting XML to a URL in which the subdomain has a hyphen


I'm having real problems posting to a webservice, and it seems that the problem lies in the url - the subdomain has a hyphen in it. The URL below is NOT the real one, but you should get the idea.

I get this error when passing the URL with the hyphen in:

02-27 10:33:45.992: E/AndroidRuntime(2226): java.lang.IllegalArgumentException: Host name may not be null

And if the hyphen is ommitted, then at least it will look for the URL.

Please help!

    HttpClient httpclient = new DefaultHttpClient();

    try {
        HttpPost httppost = new HttpPost("http://mbhh.one-dev.co.uk/HandsetService.asmx?op=Opp");

        StringEntity se = new StringEntity( getUploadXml ().toString(), HTTP.UTF_8);
        se.setContentType("text/xml");
        httppost.setEntity(se);

        HttpResponse httpresponse = httpclient.execute(httppost);
        HttpEntity resEntity = httpresponse.getEntity();
        String result = EntityUtils.toString(resEntity);
        Log.d(TAG, "writer = "+result);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Solution

  • You can use the URI constructor, which automatically escapes invalid characters:

    URL url = new URI("http", "//mbhh.one-dev.co.uk/HandsetService.asmx?op=Opp", null).toURL();