使用asynctask android连接到套接字 [英] connecting to socket using asynctask android
本文介绍了使用asynctask android连接到套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图制作一个启动画面,但它似乎正在尝试通过套接字连接服务器.
I'm trying to make a splashscreen while it appears I'm trying to connect the server through socket.
我刚接触Android业务,所以有可能我做错了,无论如何它还是行不通的.
I'm pretty new to Android business so there is a chance I made it wrong, anyway it does not work.
public class SplashScreen extends Activity {
private Socket socket; // the socket
private final int port = 1500; // the socket port - 1500
private boolean connectivity=false; // if connected to the socket or not
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ConnectToServer cts= new ConnectToServer();
cts.execute();}
private class ConnectToServer extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Log.d("socket", "trying to connect");
socket = new Socket("10.0.0.11", port);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (socket.isConnected())
connectivity=true;
return null;
}}
protected void onPostExecute() {
// execution of result of Long time consuming operation
Intent i = new Intent(SplashScreen.this, MainActivity.class);
i.putExtra("connectivity", connectivity);
startActivity(i);
finish();}
推荐答案
OMG!你放错了花括号!!!我还建议您保护那些属性而不是私有属性,这是一个很长的故事,以解释原因(它防止创建特殊的静态方法以能够获取私有字段.您看不到该过程是由编译器完成的).
OMG! you had misplaced a curly bracket!!! Also I suggest you to make those properties protected instead of private its a long story to explain why (its to prevent to create special static methods to be able to get your private fields. you cant see that process which is done by a compiler).
public class SplashScreen extends Activity {
protected Socket socket; // the socket
protected final int port = 1500; // the socket port - 1500
protected boolean connectivity=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ConnectToServer cts= new ConnectToServer();
cts.execute();
}
private class ConnectToServer extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Log.d("socket", "trying to connect");
try {
socket = new Socket("10.0.0.11", port);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (socket.isConnected())
connectivity=true;
return null;
} // here u have the second } and you finished the class declaration without the following method which is overriden in activity!!!
@Override
protected void onPostExecute(Void result) {
// execution of result of Long time consuming operation
Intent i = new Intent(getBaseContext(), PreviewActivity.class);
i.putExtra("connectivity", connectivity);
startActivity(i);
finish();
}
}
}
这篇关于使用asynctask android连接到套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文