public class Server extends Activity implements OnClickListener {
// EditText editText;
// TextView text1;
Button button;
Button button1;
SQLiteDatabase db_server;
private ServerSocket serverSocket;
Handler updateConversationHandler;
Thread serverThread = null;
private TextView text;
public static final int SERVERPORT = 6000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text2);
// editText = (EditText) findViewById(R.id.editText1);
// button = (Button) findViewById(R.id.button1);
button1 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
// button.setOnClickListener(this);
db_server = openOrCreateDatabase("server_DB1", Context.MODE_PRIVATE,
null);
db_server
.execSQL("CREATE TABLE IF NOT EXISTS server_table1(name VARCHAR,image BLOB);");
updateConversationHandler = new Handler();
this.serverThread = new Thread(new ServerThread());
this.serverThread.start();
}
@Override
protected void onStop() {
super.onStop();
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class ServerThread implements Runnable {
public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
try {
socket = serverSocket.accept();
CommunicationThread commThread = new CommunicationThread(
socket);
new Thread(commThread).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class CommunicationThread implements Runnable {
private Socket clientSocket;
private BufferedReader input;
public CommunicationThread(Socket clientSocket) {
this.clientSocket = clientSocket;
try {
this.input = new BufferedReader(new InputStreamReader(
this.clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
String read = input.readLine();
updateConversationHandler.post(new updateUIThread(read));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class updateUIThread implements Runnable {
private String msg;
public updateUIThread(String str) {
this.msg = str;
}
@Override
public void run() {//showing error in this method
String data[] = msg.split(",");
String name = data[0];
String image = data[1];
for (int i = 0; i < data.length; i++) {
if (i == 0)
name = data[i];
else if (i == 1)
image = data[i];
}
Log.e("immmmmmmmaaaaaaaaaaaaggggee", name + "," + image);
byte[] decodedImage = null;
try {
decodedImage = Base64.decode(image);
} catch (IOException e) {
e.printStackTrace();
}
db_server.execSQL("insert into server_table1 values('" + name
+ "'," + decodedImage + ")");
text.setText(text.getText().toString() + "Client Name: " + msg
+ "\n");
}
}
@Override
public void onClick(View v) {
/*
* if (v == button) {
* db_server.execSQL("INSERT INTO server_table VALUES('" +
* editText.getText() + "');"); showMessage("Success", "Record added");
* clearText(); }
*/
if (v == button1) {
Cursor c = db_server.rawQuery("SELECT * FROM server_table1", null);
if (c.getCount() == 0) {
showMessage("Error", "No records found");
return;
}
if (c.moveToFirst()) {
StringBuffer buffer = new StringBuffer();
while (c.moveToNext()) {
buffer.append("name: " + c.getString(0) + "\n");
buffer.append("image: " + c.getBlob(1) + "\n");
}
showMessage(" Details", buffer.toString());
}
}
}
public void showMessage(String title, String message) {
Builder builder = new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}