我构建的Android应用程序需要http连接到服务器才能访问数据。我写了自动化测试(基于Espresso)并在我的设备上测试它,它的工作原理。在谷歌云测试实验室设备上测试时,相同的测试失败。我查看了日志,它显示它在尝试建立服务器连接时确实失败了。
在测试服务器连接之前,我是否需要先在Google云端设备上执行任何特殊配置。
谢谢, K00k00
谢谢Paisanco
这是我第一次发布的STVEROVERFLOW。如果有些话,请原谅我!!!
这是应该连接到服务器(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;
}
以下是我从Log(谷歌云测试实验室日志文件)获得的错误...
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
答案 0 :(得分:0)
我认为问题在于您使用的是端口8080.Test Lab仅支持出站HTTP(80)和HTTPS(443)连接。