我开发了Android应用程序并在三星标签2中进行了测试。该代码从托管在服务器中的java服务器页面获取数据。它在前一天工作,但现在它显示urlConnection.getInputStream()
期间的网络超时异常.I d我的代码中没有任何改变。请帮助我克服错误。
try {
String tally_ipaddr="XXXXXXX";
URL url = new URL(tally_ipaddr+"/Iplogin.jsp");
urlConnection = (HttpURLConnection) url.openConnection();
String line = "";
InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null){
sb.append(line);
}
Toast.makeText(MyActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(MyActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
答案 0 :(得分:0)
class UrlFetch extends AsyncTask<Void,Void,Void>
{
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
String tally_ipaddr="XXXXXXX";
URL url = new URL(tally_ipaddr+"/Iplogin.jsp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000); // 5 seconds
conn.setRequestMethod("GET");
conn.connect();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
conn.disconnect();
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
并调用如下所示的asynctask
new UrlFetch().execute();
希望上面的代码适合您
答案 1 :(得分:0)
我注意到地址以“Iplogin.jsp”结尾。
确保服务器正在接受来自设备IP地址的连接。我认为您设备的IP地址已更改,服务器不再响应该设备的请求。
您可以从桌面浏览器访问该服务,但不能从设备浏览器访问该服务,这一点非常可疑。