I'm creating an android application in which I exchange data with a java socket server on the pc. And in the same application I will open some threads that run servers for a direct connection to the android, receiving intermittent data.
The problem is that the test server that I have on android receives only one string and then does not receive any more.
This is the test server that listens for connections coming from a client socket java: When the client sends the string for the first time, it appears in the apk android, but from the second it updates the more ui, until the client java it sends and does not appear any exception, in the application also does not generate exception but does not update it. Maybe it's some problem in the onPostExecute class, but I'm not sure how to parse it. It may also be that the connection is still open, I tried close () of the various forms but all gave exception and closed the apk. Could anyone analyze the code and see what is wrong?
package com.example.palioteste;
import android.os.AsyncTask;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
public class ServidorSocket extends AsyncTask<String, String, String> {
private static final int portaServidor = 9879;
private Socket conexao;
private BufferedReader br;
@Override
protected String doInBackground(String... params) {
try {
ServerSocket socket = new ServerSocket(portaServidor);
//dados enviados para o servidor
while(true)
{
conexao = socket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(conexao.getInputStream()));
//String retorno = "Dados Recebidos " + br.readLine(); //retornar ok
//result = br.readLine();
return br.readLine();
}
}
catch(IOException e) {
return e.getMessage();
}
finally{
try {
conexao.shutdownInput(); // da certo mas quando tenta denovo a ui nao atualiza
//conexao.close(); //da excessão informa que a conexao foi reinciada
//br.close() // da excessao e fecha a aplicacao
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void onPostExecute(String resultado) {
MainActivity.textorecebido2.setText("texto" + resultado);
}
}
Here I have MainActivity
package com.example.palioteste;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button button;
private TextView textView1;
static TextView textorecebido;
static TextView textorecebido2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ServidorSocket rodarServidor = new ServidorSocket();
rodarServidor.execute();
button = (Button) findViewById(R.id.button);
textView1 = (TextView) findViewById(R.id.textView1);
textorecebido = (TextView) findViewById(R.id.textorecebido);
textorecebido2 = (TextView) findViewById(R.id.textorecebido2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
textView1.setText("Botao Cliquado");
abrirSocket rodar = new abrirSocket();
rodar.execute();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}