I am a novice, do these days to do a college news client, but also found some problems, and sometimes will stop maintaining the school's server, network connection timed out sometimes, when that happens, I want the program prompt the user, rather than force quit, how to solve?
new Thread ()<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
{
public void run ()
{
String mytitle = null;
String mytime = null;
try
{
mytitle = new String ();
mytime = new String ();
Elements topnews;
Document doc = Jsoup.connect (getnewPage (id, page)) timeout (3000) get ();..
if ("4". equals (id))
{
topnews = doc.getElementsByClass ("list");
}
else
{
topnews = doc.getElementsByClass ("contList");
}
Elements links = topnews.select ("a [href]");
Elements time = topnews.select ("td [width = 80]");
if ("4". equals (id))
{
for (int i = 0; i <15; i + +) {
map = new HashMap();
mytitle = links.get (i +2) text ();.
Log.e ("aaaaaaa", mytitle);
pagelink [i +1] = links.get (i +2) attr. ("abs: href");
mytime = time.get (i) text ();.
map.put ("title", mytitle);
map.put ("time", mytime);
lists.set (i +1, map);
}
}
else
{
for (int i = 0; imap = new HashMap ();
. mytitle = links.get (i) text ();
Log.e ("aaaaaaa", mytitle);
pagelink [i +1] = links.get (i) attr ("abs: href");
.mytime = time.get (i) text ();.
map.put ("title", mytitle);
map.put ("time", mytime);
lists.set (i +1, map);
}
}
Message m = new Message ();
m.what = 1;
mainTab.this.handler.sendMessage (m);
}
catch (Exception e)
{
mytitle = e.getMessage ();
Toast.makeText (mainTab.this, "the network is not open or connection timeout", Toast.LENGTH_LONG) show ();.
e.printStackTrace ();
}
finally
{
dialog.dismiss ();
}
}
.} Start ();
Reply:
Network exception error when there are a lot of different requests; overtime is SocketTimeOutException;
before you catch this exception ExceptionCapture the timeout exception;
Depending on the exception that you can give a different toast message prompts the user;
I read your code timeout (3000) probably means 3000 milliseconds (I guess) you use this, I have not seen too lazy to look up; You can change this value
To set the timeout
Reply:
In general, setting a timeout is 5000 milliseconds general http will timeout after it prompts the user to reload or do other
Reply:
On the first floor that's good, in error where try, catch on the line, the wrong type SocketTimeOutException
Reply:
Set the timeout, catch an exception, the exception notification to the UI thread, like a
Reply:
(⊙ o ⊙) ... catch exception can not catch all exceptions do?
No comments:
Post a Comment