android菜鸟 问一个关于socket执行时间的问题
new AsyncTask<String, Void, Void>(){
@Override
protected void onPostExecute(Void result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
if(a==1){
Toast.makeText(CalculatorActivity.this,R.string.success,Toast.LENGTH_SHORT).show();
rsText.setText("");
a=0;
}
else
{
Toast.makeText(CalculatorActivity.this,R.string.fail,Toast.LENGTH_SHORT).show();
rsText.setText("");
}
}
@Override
protected Void doInBackground(String... arg0) {
// TODO Auto-generated method stub
String exp = rsText.getText().toString();
Socket socket;
try {
socket = new Socket(bundle.getString("ip"), 51706);
//InetAddress serverAddr = InetAddress.getByName(bundle.getString("ip"));
Log.d("TCP", "C: Connecting..."+bundle.getString("ip"));
Log.d("TCP", "C: Sending: '" + exp + "'");
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
out.println(exp);
socket.close();
a=1;
}
catch(Exception v)
{
Log.e("TCP", "S: Error", v);
}
return null;
}
}.execute(new String[]{});
@Override
protected void onPostExecute(Void result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
if(a==1){
Toast.makeText(CalculatorActivity.this,R.string.success,Toast.LENGTH_SHORT).show();
rsText.setText("");
a=0;
}
else
{
Toast.makeText(CalculatorActivity.this,R.string.fail,Toast.LENGTH_SHORT).show();
rsText.setText("");
}
}
@Override
protected Void doInBackground(String... arg0) {
// TODO Auto-generated method stub
String exp = rsText.getText().toString();
Socket socket;
try {
socket = new Socket(bundle.getString("ip"), 51706);
//InetAddress serverAddr = InetAddress.getByName(bundle.getString("ip"));
Log.d("TCP", "C: Connecting..."+bundle.getString("ip"));
Log.d("TCP", "C: Sending: '" + exp + "'");
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
out.println(exp);
socket.close();
a=1;
}
catch(Exception v)
{
Log.e("TCP", "S: Error", v);
}
return null;
}
}.execute(new String[]{});
}
这是安卓客户端的程序,当服务器没有开启时,客户端会卡在try中,现在需要一个方法在try一定时间无果后,跳出toast提示。刚学android,请提点。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
socket.connect(remoteAddr, timeout); 不是可以设置超时时间么。超时的话会抛出异常,然后catch处理之