首先,我不能改变那个网址......那说...... 我有一个非常愚蠢的问题,我收到了这个错误:
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));
我已检查过网址是否有效:http://formvalidation.io/validators/uri/ 并且我发现URL中的 UNDERSCORE _ 正在弄乱我并且我无法更改URL,谁知道解决方案或解决方法呢?
答案 0 :(得分:0)
首先,您不应该使用HttpPost,因为它已被弃用,而是使用HttpURLConnection。
其次,为什么不直接使用String constructor?
String urlStr = "http://elbot_e.csoica.artificial-solutions.com/cgi-bin/elbot.cgi";
HttpPost httpPost = new HttpPost(urlStr);
看起来你正在做很多不需要的额外工作,并通过太多复杂化来引入一些错误。该URL看起来有效,因此应该有效。
修改强>
如果您遇到字符串问题,可以添加URL encoding:
HttpPost httpPost = new HttpPost(URLEncoding.encode(urlStr, "UTF-8"));
答案 1 :(得分: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);
}
}
}