Символ подчеркивания в URL делает имя хоста = нулевой ошибкой в ​​Android

Прежде всего, я НЕ МОГУ изменить этот URL-адрес.... при этом... У меня действительно тупая проблема, я получаю эту ошибку:

Caused by: java.lang.IllegalArgumentException: Host name may not be null

в коде ниже:

            URL url = null;
            try{
               //  encodedURL = URLEncoder.encode("elbot_e.csoica.artificial-solutions.com/cgi-bin/elbot.cgi", "UTF-8");

                String urlStr = "http://elbot_e.csoica.artificial-solutions.com/cgi-bin/elbot.cgi";
                url = new URL(urlStr);
                URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
                url = uri.toURL();

            } catch(Exception e) {
            }

            HttpPost httpPost = new HttpPost(String.valueOf(url));

Я проверил, действительно ли URL-адрес в: http://formvalidation.io/validators/uri/ и я обнаружил, что UNDERSCORE_ в URL-адресе что-то напутало, и я не могу изменить URL-адрес, который знает решение или обходной путь для этот?

2 ответа

Во-первых, вы не должны использовать HttpPost, поскольку он устарел, вместо этого используйте HttpURLConnection.

Во-вторых, почему бы не использовать непосредственно конструктор String?

        String urlStr = "http://elbot_e.csoica.artificial-solutions.com/cgi-bin/elbot.cgi";
        HttpPost httpPost = new HttpPost(urlStr);

Похоже, вы выполняете много ненужной дополнительной работы и вносите некоторые ошибки, усложняя вещи. URL выглядит правильно, так что должно работать.

РЕДАКТИРОВАТЬ:

Вы можете добавить кодировку URL, если у вас есть проблемы со строкой:

HttpPost httpPost = new HttpPost(URLEncoding.encode(urlStr, "UTF-8"));
Try to get this code.


public class UHttpClient {

    private HttpClient httpClient = null;

    public static final int OK = 200;
    public static final int NO_CONTENT = 204;

    private static final int CONNECTION_TIMEOUT = 5000;

    /* timeout for waiting for data, since once connection is established then waiting for response*/
    private static final int SOCKET_TIMEOUT = 20000;    

    public UHttpClient(UAppInstance appInstance, String loginURL, String deviceID) 
    throws UBaseException {


        HttpParams httpParameters = new BasicHttpParams();

        // Set the timeout in milliseconds until a connection is established.
        HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);

        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIMEOUT);

        //httpClient = new DefaultHttpClient(httpParameters);
        httpClient = new DefaultHttpClient();

        login(loginURL, deviceID);
    }

    /**
     * Makes the GET call
     * @param urlStr - Url string
     * @param paramNames - parameter names
     * @param paramValues - parameter values
     * @return - response body string
     * @throws UBaseException
     */
    public String executeGet(String urlStr, String[] paramNames, String[] paramValues)
    throws UBaseException {

        HttpGet httpget = null;

        try {
            int length = paramNames != null ? paramNames.length : 0;

            urlStr = length > 0 ? (urlStr + "?") : urlStr;
            for (int k = 0; k < length; k++) {
                urlStr = k > 0 ? (urlStr + "&") : urlStr;
                urlStr = urlStr + paramNames[k] + "=" + paramValues[k];
            }

            httpget = new HttpGet(urlStr);

            // Execute HTTP Get Request
            ResponseHandler<String> respHandler = new BasicResponseHandler();
            String response = httpClient.execute(httpget, respHandler);

            return response;

        } catch (SocketTimeoutException e) {
                throw new UBaseException("Connection Timeout", e);
        } catch (Exception e) {
            if(null == e.getMessage() || "".equals(e.getMessage()))
                throw new UBaseException("Unable to connect to server", e);
            else
                throw new UBaseException(e.getMessage(), e);
        }
    }

    /**
     * Makes the POST call
     * @param urlStr - Url string
     * @param paramNames - parameter names
     * @param paramValues - parameter values
     * @return - response body string
     * @throws UBaseException
     */
    public String executePost(String urlStr, String[] paramNames, String[] paramValues)
    throws UBaseException {

        HttpPost httppost = new HttpPost(urlStr);

        try {
            int length = paramNames != null ? paramNames.length : 0;
            ArrayList<NameValuePair> paramsList = new ArrayList<NameValuePair>();

            for (int k = 0; k < length; k++) {
                paramsList.add(new BasicNameValuePair(paramNames[k], paramValues[k]));
            }

            List<NameValuePair> nameValuePairs = paramsList;
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            ResponseHandler<String> respHandler = new BasicResponseHandler();
            String response = httpClient.execute(httppost, respHandler);

            return response;

        } catch (SocketTimeoutException e) {
                throw new UBaseException("Connection Timeout", e);
        } catch (Exception e) {
            if(null == e.getMessage() || "".equals(e.getMessage()))
                throw new UBaseException("Unable to connect to server", e);
            else
                throw new UBaseException(e.getMessage(), e);
        }
    }


    protected void login(String url, String deviceID)
    throws UBaseException {
        // modified by Avishek as in desktop dev instead of distrCode it is entCode
        String[] argNames = new String[] {"userDeviceID", "loginMode", "command"};
        String[] argValues = new String[] {deviceID, "handheldLogin", "login"};

        String response = executePost(url, argNames, argValues);

        if(null != response){
            response = response.trim();
        }

        // if response message is not success that means it's a failure so raising exception 
        if("success".equals(response) == false) {
            if(!"".equals(response))
                throw new UBaseException(response, null);
            else
                throw new UBaseException("Remote login failed to '" + url + "'", null);
        }
    }
}
Другие вопросы по тегам