Answer the question
In order to leave comments, you need to log in
Android and sockets?
Well, in short: I wrote a server in java and wrote an android client, too, java.
There is an activity in which registration takes place, here is the code:
public class CheckActivity extends ActionBarActivity {
private String login = null;
private String password = null;
private ServerThread client = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check);
}
public void onClickCheck(View view) throws IOException, ClassNotFoundException {
EditText editLogin = (EditText) findViewById(R.id.editLogin);
EditText editPassword = (EditText) findViewById(R.id.editPassword);
login = editLogin.getText().toString();
password = editPassword.getText().toString();
client = new ServerThread(new Message(login,password,"R"));
if(client.check()==true){
TextView checkText = (TextView)findViewById(R.id.checkText);
checkText.setText("Регистрация успешно завершена");
}
}
}
public class ServerThread extends Thread {
private Message clientSend = null;
private Socket fromServer = null;
private ObjectOutputStream out = null;
private ObjectInputStream in = null;
//конструктор для регистрации
public ServerThread(Message clientSend){
this.clientSend = clientSend;
}
public Boolean check() throws IOException, ClassNotFoundException {
out.writeObject(clientSend);
Message serverSend = (Message) in.readObject();
if(serverSend.getMessage()=="true"){
return true;
}
else {
return false;
}
}
public void run(){
try {
fromServer = new Socket("localhost",5050);
out = new ObjectOutputStream(fromServer.getOutputStream());
in = new ObjectInputStream(fromServer.getInputStream());
if(clientSend.getSight()=="R") {
check();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Answer the question
In order to leave comments, you need to log in
If you are a beginner and there is no goal to figure out the sockets yourself from scratch, then it is better not to cut your bike.
I use socket.io in my projects .
And on the issue, advice, do not forget to publish the stack trace. Here the code is not complicated and it is not difficult to find the problem.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question