我构建的 android 应用程序需要 http 连接到服务器才能访问数据。我编写了自动化测试(基于 Espresso)并在我的设备上进行了测试,它可以工作。在谷歌云测试实验室设备之一上进行测试时,相同的测试失败。我查看了日志,它显示它在尝试建立服务器连接时实际上失败了。
在我测试服务器连接之前,我必须先在谷歌云设备上进行任何特殊配置。
谢谢,K00k00
谢谢派桑科
这是我第一次在 StackOverFlow 上发帖。如果事情很乱,请原谅我!!!
这是应该连接到服务器 (www.servlet-location.com) 上的 Servlet (ServletName) 并传递两个参数(firstName 和 lastName)的代码。servlet 创建帐户以 xml 形式返回帐户的详细信息。
URL url = null;
HttpURLConnection conn = null;
InputStream is = null;
String xml = null;
String line = "";
StringBuilder builder = new StringBuilder();
BufferedReader reader = null;
try
{
url = new URL("http://www.servlet-location.com:8080/ServletName?firstName=Joe&lastName=Marvin");
conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setUseCaches(false);
conn.connect();
is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
while ((line = reader.readLine()) != null)
builder.append(line.trim());
if(is != null)
is.close();
if(conn != null)
conn.disconnect();
}
catch(MalformedURLException e)
{
e.printStackTrace();
try
{
if(is != null)
is.close();
if(conn != null)
conn.disconnect();
}
catch(IOException ioe)
{
ioe.printStackTrace();
return null;
}
return null;
}
catch(SocketTimeoutException e)
{
e.printStackTrace();
try
{
if(is != null)
is.close();
if(conn != null)
conn.disconnect();
}
catch(IOException ioe)
{
ioe.printStackTrace();
return null;
}
return null;
}
catch(IOException e)
{
e.printStackTrace();
try
{
if(is != null)
is.close();
if(conn != null)
conn.disconnect();
}
catch(IOException ioe)
{
ioe.printStackTrace();
return null;
}
return null;
}
return xml;
}
这是我从日志(谷歌云测试实验室日志文件)中得到的错误......
06-18 12:34:35.537: W/System.err(22125): java.net.SocketTimeoutException: failed to connect to www.servlet-location.com/xxx.xxx.xxx.xxx (port 8080) after 10000ms
06-18 12:34:35.538: W/System.err(22125): at libcore.io.IoBridge.connectErrno(IoBridge.java:169)
06-18 12:34:35.538: W/System.err(22125): at libcore.io.IoBridge.connect(IoBridge.java:122)
06-18 12:34:35.538: W/System.err(22125): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
06-18 12:34:35.539: W/System.err(22125): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
06-18 12:34:35.539: W/System.err(22125): at java.net.Socket.connect(Socket.java:882)
06-18 12:34:35.539: W/System.err(22125): at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)
06-18 12:34:35.539: W/System.err(22125): at com.android.okhttp.Connection.connect(Connection.java:152)
06-18 12:34:35.539: W/System.err(22125): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
06-18 12:34:35.539: W/System.err(22125): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
06-18 12:34:35.539: W/System.err(22125): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
06-18 12:34:35.540: W/System.err(22125): at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
06-18 12:34:35.540: W/System.err(22125): at com.yeha.mobilebirr.transport.DataTransportManager.request(DataTransportManager.java:135)
06-18 12:34:35.540: W/System.err(22125): at
再次感谢!!!
K00k00