我正在尝试使用从Android应用程序本地创建的Web服务。 我的问题是,在我的Android应用中,在某个时刻,我必须提供一个包含以下参数的网址:http://localhost:8080/CalculatorApp/CalculatorWSService/add?i=1&j=1
其中CalculatorWS
是我使用的网络服务,add
是其中的操作,i
和j
是add
操作的参数。目前我正在使用示例应用程序计算器(来自NetBeans)进行测试,我想检索正确的URL以提供给我的Web服务客户端(Android应用程序),以便它可以返回一个XML来解析。
我尝试使用上面提到的那个网址,但它不起作用。
有人知道要放置的正确网址是什么吗?
答案 0 :(得分:5)
您需要将网址设置为10.0.2.2:portNr
portNr = ASP.NET Development Server的给定端口 我当前的服务正在运行 本地主机:3229 / Service.svc
所以我的网址是10.0.2.2:3229
我以这种方式解决了我的问题
我希望它有所帮助...
答案 1 :(得分:4)
使用此网址:
http://10.0.2.2:8080/CalculatorApp/CalculatorWSService/add?i=1&j=1
由于Android模拟器在虚拟机上运行,因此我们必须使用此IP地址而不是localhost or 127.0.0.1
答案 2 :(得分:1)
如果您使用的是模拟器,请阅读以下段落:Referring to localhost from the emulated environment
如果您需要参考主机的本地主机,例如何时 您希望模拟器客户端联系在其上运行的服务器 host,使用别名10.0.2.2来引用主机的环回 接口。从模拟器的角度来看,localhost(127.0.0.1) 指的是它自己的环回接口。
答案 3 :(得分:0)
以及对HttpUtils的调用示例......
public static final String WS_BASE = "http://www.xxxxxx.com/dev/xxx/";
public static final String WS_STANDARD = WS_BASE + "webserviceoperations.php";
public static final String REQUEST_ENCODING = "iso-8859-1";
/**
* Send a request to the servers and retrieve InputStream
*
* @throws AppException
*/
public static Login logToServer(Login loginData) {
Login result = new Login();
try {
// 1. Build XML
byte[] xml = LoginDAO.generateXML(loginData);
// 2. Connect to server and retrieve data
InputStream is = HTTPUtils.readHTTPContents(WS_STANDARD, "POST", xml, REQUEST_ENCODING, null);
// 3. Parse and get Bean
result = LoginDAO.getFromXML(is, loginData);
} catch (Exception e) {
result.setStatus(new ConnectionStatus(GenericDAO.STATUS_ERROR, MessageConstants.MSG_ERROR_CONNECTION_UNKNOWN));
}
return result;
}
和我的HTTPUtils类的方法readHTTPContents
/**
* Get the InputStream contents for a specific URL request, with parameters.
* Uses POST. PLEASE NOTE: You should NOT use this method in the main
* thread.
*
* @param url
* is the URL to query
* @param parameters
* is a Vector with instances of String containing the parameters
*/
public static InputStream readHTTPContents(String url, String requestMethod, byte[] bodyData, String bodyEncoding, Map<String, String> parameters)
throws AppException {
HttpURLConnection connection = null;
InputStream is = null;
try {
URL urlObj = new URL(url);
if (urlObj.getProtocol().toLowerCase().equals("https")) {
trustAllHosts();
HttpsURLConnection https = (HttpsURLConnection) urlObj
.openConnection();
https.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
connection = https;
} else {
connection = (HttpURLConnection) urlObj.openConnection();
}
// Allow input
connection.setDoInput(true);
// If there's data, prepare to send.
if (bodyData != null) {
connection.setDoOutput(true);
}
// Write additional parameters if any
if (parameters != null) {
Iterator<String> i = parameters.keySet().iterator();
while (i.hasNext()) {
String key = i.next();
connection.addRequestProperty(key, parameters.get(key));
}
}
// Sets request method
connection.setRequestMethod(requestMethod);
// Establish connection
connection.connect();
// Send data if any
if (bodyData != null) {
OutputStream os = connection.getOutputStream();
os.write(bodyData);
}
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new AppException("Error HTTP code " + connection.getResponseCode());
}
is = connection.getInputStream();
int numBytes = is.available();
if (numBytes <= 0) {
closeInputStream(is);
connection.disconnect();
throw new AppException(MessageConstants.MSG_ERROR_CONNECTION_UNKNOWN);
}
ByteArrayOutputStream content = new ByteArrayOutputStream();
// Read response into a buffered stream
int readBytes = 0;
while ((readBytes = is.read(sBuffer)) != -1) {
content.write(sBuffer, 0, readBytes);
}
ByteArrayInputStream byteStream = new ByteArrayInputStream(content.toByteArray());
content.flush();
return byteStream;
} catch (Exception e) {
// Logger.logDebug(e.getMessage());
throw new AppException(e.getMessage());
} finally {
closeInputStream(is);
closeHttpConnection(connection);
}
}
希望这能帮到你......