从Java

时间:2016-03-08 18:33:07

标签: java android multithreading sockets

我正在开发一个在Java上使用套接字通信的小代码,我正在尝试执行以下操作:

  1. 接收JSON对象并解析它[完成]
  2. 如果收到的命令是login,则开始登录[完成]
  3. 向套接字打印登录过程的结果 这是我遇到问题的最后一部分,我的代码如下:

    public class LoginActivity extends Activity {
    
    private Button btnLogin;
    private Button btnDemo;
    private EditText edtLogin;
    private EditText edtSenha;
    
    private ProgressDialog pd;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    
        btnLogin = (Button)findViewById(R.id.btnLogin);
        btnDemo = (Button)findViewById(R.id.btnDemo);
        edtLogin = (EditText)findViewById(R.id.edtLogin);
        edtSenha = (EditText)findViewById(R.id.edtSenha);
        pd = new ProgressDialog(this);
    
        new Thread(new Runnable(){
            public void run(){
                try {
                    String inMsg;
                    JSONObject fromClient;
                    String cmd;
                    JSONArray args;
                    ServerSocket server;
                    Socket client;
                    BufferedReader in;
                    PrintWriter out;
                    //TODO: Getting reconnects on a proper method
                    System.out.println("Waiting for client on port 8888");
                    server = new ServerSocket(8888);
                    client = server.accept();
                    System.out.println("Just connected to " + client.getRemoteSocketAddress());
                    in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    out = new PrintWriter(client.getOutputStream(), true);
                    while(true)
                    {
                        inMsg = in.readLine();       
                        if (inMsg == null)
                        {
                            System.out.println("Waiting for client on port 8888");
                            client = server.accept();
                            System.out.println("Just connected to " + client.getRemoteSocketAddress());
                            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                            out = new PrintWriter(client.getOutputStream(), true);
                            continue;
                        }
                        fromClient = new JSONObject(inMsg);
                        System.out.println("In: " + fromClient);
                        cmd = fromClient.getString("command");
                        args = new JSONArray(fromClient.getString("args"));
                        if(cmd.equals("login")){
                            final String login = args.getString(0);
                            final String pwd = args.getString(1);
                            AndroidUtil.mostrarProgressoAguarde(LoginActivity.this, pd);
    
                            new Thread(new Runnable() {
                                public void run() {
                                    PagPop.getInstance().logar(LoginActivity.this, login, pwd, new ServidorListener<Cliente>() {
                                        @Override
                                        public void recebeuResposta(String mensagem, RespostaServidor<Cliente> resposta) {
                                            handleResposta(mensagem, resposta);
                                        }
                                    });
                                }
                            }).start();
                            //out.println(mensagem);
                        }
    
                        else 
                        {
                            out.println("Received");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        btnLogin.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final String login = edtLogin.getText().toString();
                final String senha = edtSenha.getText().toString();
                System.out.print("Tentando Logar");
    
                AndroidUtil.mostrarProgressoAguarde(LoginActivity.this, pd);
    
                new Thread(new Runnable() {
                    public void run() {
                        PagPop.getInstance().logar(LoginActivity.this, login, senha, new ServidorListener<Cliente>() {
                            @Override
                            public void recebeuResposta(String mensagem, RespostaServidor<Cliente> resposta) {
                                handleResposta(mensagem, resposta);
                            }
                        });
                    }
                }).start();
            }
        });
    
  4. 在代码中间附近,我尝试在//out.println(mensagem);打印出所需的变量失败了。如果我尝试编译我的代码,则会发生以下情况:

      

    [javac] /android/demo/LoginActivity.java:107:错误:从内部类中访问局部变量out;需要被宣布为最终

         

    [javac] out.println(mensagem);

    如何将变量mensagem传输出去,这样我就可以从out.println级别if或从该方法访问out而无需{{1}最后?

1 个答案:

答案 0 :(得分:1)

PrintWriter out声明为activity / fragment

中的全局变量