1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
// abiz after Elektor Android book october 2012
package netcom.example.com;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class NetComActivity extends Activity {
Button btnSend;
TextView textStatus;
NetworkTask networktask;
private EditText message;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
message = (EditText) findViewById(R.id.editMessage);
btnSend = (Button)findViewById(R.id.btnSend);
textStatus = (TextView)findViewById(R.id.textStatus);
btnSend.setOnClickListener(btnSendListener);
networktask = new NetworkTask();
networktask.execute();
}
private OnClickListener btnSendListener = new OnClickListener() {
public void onClick(View v){
textStatus.setText("Envoi d'un message à la tâche.");
// On envoi un message vers le serveur.
networktask.SendDataToNetwork(message.getText() + "\r\n\r\n");
}
};
public class NetworkTask extends AsyncTask<Void, byte[], Boolean> {
Socket nsocket; //Network Socket
InputStream nis; //Network Input Stream
OutputStream nos; //Network Output Stream
@Override
protected void onPreExecute() {
}
@Override
// >>> Partie executee dans un autre Thread >>>>
protected Boolean doInBackground(Void... params) {
boolean result = false;
try {
SocketAddress sockaddr = new InetSocketAddress("192.168.55.1", 5000);
nsocket = new Socket();
nsocket.connect(sockaddr, 10000); //10 second connection timeout
if (nsocket.isConnected()) {
nis = nsocket.getInputStream();
nos = nsocket.getOutputStream();
byte[] buffer = new byte[4096];
int read = nis.read(buffer, 0, 4096);
while(read != -1){
byte[] tempdata = new byte[read];
System.arraycopy(buffer, 0, tempdata, 0, read);
publishProgress(tempdata);
read = nis.read(buffer, 0, 4096);
}
}
} catch (IOException e) {
e.printStackTrace();
result = true;
} catch (Exception e) {
e.printStackTrace();
result = true;
} finally {
try {
nis.close();
nos.close();
nsocket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
} // <<< doInBackground <<<<<<<<<<<<<<<<<<<<<<<<<<<<
public void SendDataToNetwork(String cmd) {
try {
if (nsocket.isConnected()) {
textStatus.setText("Envoi d'un message au Socket.");
nos.write(cmd.getBytes());
} else {
textStatus.setText("Envoi impossible Socket fermé.");
}
} catch (Exception e) {
textStatus.setText("Echec d'envoi Erreur générale.");
}
}
@Override
protected void onProgressUpdate(byte[]... values) {
if (values.length > 0) {
textStatus.setText(new String(values[0]));
}
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
textStatus.setText("Erreur de connexion.");
}
}
} // <<< NetworkTask <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@Override
protected void onDestroy() {
super.onDestroy();
networktask.cancel(true);
}
} |
Partager