我试图制作一个启动画面,而我试图通过套接字连接服务器。
我对Android业务很陌生,所以有可能我做错了,无论如何它都行不通。
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();}
答案 0 :(得分:0)
OMG!你错放了一个大括号!另外我建议你让这些属性受保护而不是私有它的长故事来解释原因(它阻止创建特殊的静态方法以便能够获得你的私有字段。你无法看到由编译器完成的过程)。
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();
}
}
}