I am trying to do a real-time chat application (where the Android
user's text shows up real-time in a web interface running in a browser
on someon'es PC). Right now I'm having it post the text in a textbox
using "onKeyUp" and running it in a background thread (using
"thread.join()" so it doesn't have to finish sending one character
before it sends the next one - to keep the app from freezing up) and
that seems to be running very inefficiently. I am posting the code I
am using, can someone help me clean it up and/or make it run more
efficiently?
===========================================================
String presponseBody;
String postdata;
public boolean onKeyUp(int keyCode, KeyEvent event)
{
final long mydate = System.currentTimeMillis();
String showtext = mytext.getText().toString();
try {
postdata =
URLEncoder.encode(mytext.getText().toString(), "UTF-8");
} catch (UnsupportedEncodingException e1) {
chatscreen.append("There was a problem sending your
text. Please
try again.\n");
}
// send text using background thread
final Handler phandler = new Handler();
Thread pthread = new Thread()
{
public void run()
{
// make http post
HttpClient chatclient = new DefaultHttpClient();
HttpPost chatpost = new
HttpPost("http://www.my-web-site.com/
postdata.php?data="+postdata);
// response handler
ResponseHandler<String> responseHandler = new
BasicResponseHandler
();
try {
// if user types exit, exit out of
program
if (postdata.compareTo("exit") == 0)
{
removeUpdates();
System.exit(0);
}
else
{
presponseBody =
chatclient.execute(chatpost, responseHandler);
}
} catch (ClientProtocolException e) {
// error sending message
presponseBody = "There was an error
retrieving the message.
Please try again.\n";
} catch (IOException e) {
// error sending message
presponseBody = "There was an error
retrieving the message.
Please try again.\n";
}
phandler.post(
new Runnable()
{
public void run()
{
// not doing anything
with this...
}
}
);
}
private void removeUpdates() {
// TODO Auto-generated method stub
}
};
pthread.start();
try {
// join the thread so we don't freeze the UI
pthread.join();
} catch (InterruptedException e) {
// do nothing
}
return true;
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---