M
M
Masteron2016-01-25 19:42:48
Java
Masteron, 2016-01-25 19:42:48

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("Регистрация успешно завершена");
        }
    }

    }

And then everything goes into a separate thread:
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();
        }
    }

}

But when you try to call the onClickCheck method through a button, i.e. Button, the application immediately closes. What is the problem and how to get rid of it? PS I'm still new to sockets.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
georgeci, 2016-01-25
@georgeci

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 question

Ask a Question

731 491 924 answers to any question