在android应用程序中将http转换为https后的SocketTimeoutException
尝试将我的android应用程序转换为使用SSL在我的android应用程序和Web服务器之间传输信息后,出现一些问题。 (SocketTimeOutException)在android应用程序中将http转换为https后的SocketTimeoutException
我已从证书颁发机构(CA)购买了Positive SSL证书,并配置了我的服务器以正确使用它。我已经在我的网络浏览器中测试过它,并且它的工作正常。
现在我试图修改我的android应用程序使用https而不是http,但由于这是我第一次使用https自己,我有点困惑,我需要在java代码中实现哪些步骤。
目前我正在设置我的设置页面,在该页面中我将应用程序配置为使用正确的URL。例如,我在一个活动中有2个文本字段,我在其中输入网站URL(http://www.mydomain.com)和相关页面存储在的应用程序文件夹(myfolfolder)
然后我使用以下代码连接和测试连接变量配置正确,其中validateurl.aspx是,如果存在的页面,返回一个JSON字符串的网页:
protected boolean validateConnectionSettings() {
String result = "";
boolean validated = false;
try {
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(tvWebsiteURLValue.getText() + File.separator + tvApplicationMiddlewareValue.getText() + File.separator + "validateurl.aspx");
URL url = new URL(urlBuilder.toString());
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
URLConnection urlConn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String inputLine;
while((inputLine = in.readLine()) != null){
result += inputLine;
}
in.close();
if(result.equals("exists")) {
validated = true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
return validated;
}
现在,当我尝试转换(HTTP://www.mydomain .com)变量来使用https我得到了上面提到的java.net.SocketTimeoutException。
我用Google搜索这个问题,并看到了,我要在上面的代码来实现的,而不是HttpsURLConnection的URLConnection的所以我相应的修改我的代码如下:
protected boolean validateConnectionSettings() {
String result = "";
boolean validated = false;
try {
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(tvWebsiteURLValue.getText() + File.separator + tvApplicationMiddlewareValue.getText() + File.separator + "validateurl.aspx");
URL url = new URL(urlBuilder.toString());
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
// URLConnection urlConn = url.openConnection();
HttpsURLConnection urlConn = (HttpsURLConnection)url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String inputLine;
while((inputLine = in.readLine()) != null){
result += inputLine;
}
in.close();
if(result.equals("exists")) {
validated = true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
return validated;
}
不过我还是收到了java.net .SocketTimeoutException。任何想法我做错了什么?
好吧,我发现导致SocketTimeOutException的问题。
看来问题在于我正在家中工作,手机正在使用与服务器所在的网络相同的网络。在这种情况下,似乎无法使用https访问服务器。我关闭了手机无线网络,并使用手机3G连接进行连接,并且嘿pre the应用连接并验证了我的网址。
我有类似的问题,直接从服务器和我的工作笔记本电脑在Web浏览器中测试https连接。在每种情况下,我通过将www.mydomain.com地址添加到主机文件中来解决问题。
我现在要看看编辑我的Android手机主机文件,看看这是否会解决它,因为我不想用手机G3连接进行测试,因为我期望病得不偿失......
生病后评论下方后来说是怎么一回事呢
我希望这可以帮助其他人也有类似的问题,使用下面的链接生根我的手机后http://www.androidauthority.com/how
OK -to-root-your-samsung-galaxy-ace-37385 /然后从google play上下载并使用应用主机编辑器。我能够编辑我的主机文件并添加我的网络服务器的IP和别名。 这工作很好,现在我可以在我的内部网络上使用https连接到网络服务器 – 2012-07-06 14:59:48