我正在尝试使用 HttpURLConnection API 创建 android 应用程序。我成功登录,现在尝试触发另一个查询说(http://someserver.com/myapp/getemployeebyid?userid。)在执行此语句时,我得到以下异常。
java.net.protocolexception:连接已经建立
我已经关闭了输入流。
我的代码是。
mymethod(urlString){
InputStream myis = null;
URL url = new URL(urlstring);
HttpURLConnection httpURLconnection = (HttpURLConnection)
url.openConnection();
httpURLconnection.setReadTimeout(10000);
httpURLconnection.setConnectTimeout(15000);
httpURLconnection.setRequestMethod("GET");
httpURLconnection.setDoInput(true);
httpURLconnection.connect();
int myresponse = httpURLconnection.getResponseCode();
if (myresponse == 200) {
ResponseFlag = true;
myis = httpURLconnection.getInputStream();
// Convert the InputStream into a string
contentAsString = convertIttostring(myis);
}
else {
ResponseFlag = false;
somemesg = "incorrect response ";
}
}
catch(Exception Ex){
System.out.println(Ex.toString());
somemesg="connection failed";
}
finally {
if (myis != null) {
myis.close();
}
}
}
现在我有 2 个查询。
- 我需要写connection.disconnect(); 在 myis.close() 之下,分别?
- 登录成功后,我正在尝试重新连接。我不能连接一次并触发查询。完成所有操作后关闭连接?