0

我在发布到 Web 服务时遇到了真正的问题,似乎问题出在 url 上——子域中有一个连字符。下面的 URL 不是真实的,但您应该明白这一点。

传递带有连字符的 URL 时出现此错误:

02-27 10:33:45.992: E/AndroidRuntime(2226): java.lang.IllegalArgumentException: 主机名不能为空

如果连字符被省略,那么至少它会查找 URL。

请帮忙!

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

1 回答 1

1

您可以使用URI 构造函数,它会自动转义无效字符:

URL url = new URI("http", "//mbhh.one-dev.co.uk/HandsetService.asmx?op=Opp", null).toURL();
于 2012-02-27T11:11:50.697 回答