I'm working on a small code that uses socket communications on Java and I'm trying to do the following:
- Receive a JSON object and parse it [Done]
- If the command received was
login
then start a login [Done] Print out to the socket the result of the login procedure It's the last portion I'm having trouble with, my code is as follows:
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(); } });
Near the middle of the code there is my failed attempt to print out the desired variable at //out.println(mensagem);
If I try to compile my code with that the following happens:
[javac] /android/demo/LoginActivity.java:107: error: local variable out is accessed from within inner class; needs to be declared final
[javac] out.println(mensagem);
How can I either transport that variable mensagem
out of there so I can out.println
it from the if
level or access out
from that method without making out
final?