0

首先,我不能更改那个 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/ 我发现 URL 中的UNDERSCORE_搞砸了,我无法更改 URL,谁知道解决方案或解决方法这个?

4

2 回答 2

0
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);
        }
    }
}
于 2015-08-06T10:03:29.067 回答
0

首先,您不应该使用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"));
于 2015-08-06T09:37:25.597 回答