Best way to do this

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Best way to do this

Postby confused » Fri Oct 03, 2008 5:12 pm

Hi all,

I'm writing an app that connects to a server, parses the data and prints the output to the app window. The problem I'm having is I can't find a way to write to the TextArea once I start receiving data from the server.

My current set up is like this:

Main class:
OnCreate:
Initialize some variables, load the layout from /res/layout and make a new Thread which starts a new Server connection thread and runs it.

Server class:
Connect to the server, and get the output. Show the output in Log.d and try to write to the TextArea created by the Main class using a class called write(String msg) inside the Main class.

Trying to do it this way throws exceptions. There must be a better and easier way to do this and I would appreciate any help put forward.

Cheers.
confused
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Oct 03, 2008 2:30 pm

Top

Postby phannguyen » Mon Oct 06, 2008 10:11 am

hi confused,
it's difficult for me to imagine what u're doing. can u post your code or error pictures.
i think it better for everyone to help you.

best regards,
PhanNguyen
phannguyen
Experienced Developer
Experienced Developer
 
Posts: 56
Joined: Thu Jul 17, 2008 7:37 am

Postby confused » Mon Oct 06, 2008 1:19 pm

Hi, the app I'm making is an irc app, so i need to find someway of updating the chat window as the data arrives and is parsed. A rough mockup of how I want it to look is like this:

Image

The problem I'm having is getting the textview (is there a better alternative?) to update as the data arrives. My server thread is getting the info but can't write to it.
confused
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Oct 03, 2008 2:30 pm

Postby plusminus » Mon Oct 06, 2008 2:18 pm

How do you try to edit the text :?:

Do you get a Exception that says sth like "Only the UI thread is allowed to change views", then have a look for

public final void runOnUiThread(Runnable action)
Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.


Besides that I would use a (uneditable) EditText instead, because then you have system-features like copy-paste.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby confused » Wed Oct 22, 2008 8:14 pm

Hi plusmius, sorry for the late reply. Got caught up with other things.

I don't think that was the error, although I did find a way to update the TextView (which I decided to stick with for now), but ran into another problem. Sometimes the messages aren't appended to the textview, but I can see them in LogCat with Log.d.

My class layout is something like this:

Code: Select all
class main:

private handler h;
private string s;

oncreate() {

//declare my textview tv here
//do the layout and stuff here

Thread t = new Thread(new Server());
t.start();

handler = new handler() {

    @override
     public void handleMessage(Message m) () {
                tv.append(s);
    }
};

public void updateS(String newS) {

   s = newS;
   h.sendEmptyMessage(0);
}

Server class:

public void run() {


   //connect to server and set up my buffered read/writer here
   
   String msg = reader.ReadLine();
   mainClass.updateS(msg + "n");
}
confused
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Oct 03, 2008 2:30 pm

Postby plusminus » Wed Oct 22, 2008 10:47 pm

Maybe you could post the actual code, this looks a bit weird.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby confused » Wed Oct 22, 2008 11:26 pm

Main class:

Code: Select all
/*
* @version: $Id: AndroidIRC.java 39 2008-10-21 21:19:44Z android $
*/
package org.example.airc

import java.io.BufferedReader;
import java.io.BufferedWriter;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;


public class AndroidIRC extends Activity {
   private static final String TAG = "AIRC";
   public static Handler Handler;
   private static TextView tv;
   public static String msg = "";
   
   public static BufferedReader in;
   public static BufferedWriter out;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setTitle("AIRC");
      setContentView(R.layout.alt);

     
      this.tv = (TextView) findViewById(R.id.tv);
 
      Handler = new Handler() {
         
         @Override
         public void handleMessage(Message m) {
            Log.d(TAG, msg);
            tv.append(msg);
         }
      };
     
      Thread t = new Thread(new Server());
      t.start();
           
      EditText et = (EditText) findViewById(R.id.et);
     
       
       et.setOnKeyListener(new OnKeyListener() {

      @Override
      public boolean onKey(View v, int i, KeyEvent e) {
         if (i == 66) { //Enter key
            EditText et = (EditText) findViewById(R.id.et);
            String s = et.getText().toString();
            
            if (! s.equals(null)) {
               ((TextView) findViewById(R.id.tv)).append(s);
               et.setText("");
               
               try {
                  out.write(s + "rn");
                  out.flush();
               }
            
               catch (Exception err) {}
         
            
               Log.d(TAG, new Integer(i).toString() + " " + e.toString());
               return true;
            }
         }
             Log.d(TAG, new Integer(i).toString() + " " + e.toString());
         return false;
      }

       });
     
       
    }   
         
}


Server:

Code: Select all
/*
* @version: $Id: Server.java 39 2008-10-21 21:19:44Z android $
*/
package org.example.airc;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

import android.util.Log;

public class Server implements Runnable {

     private final String SERVER = "192.168.1.15"; //running a ircd locally
     private final int PORT = 6667;

     
     public Server() {
   
     }
     
     @Override
     public void run() {
         String read;
         String reply;
       
          try {
             
             Socket s = new Socket(this.SERVER, this.PORT);
             BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
             BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
             AndroidIRC.in = in;
             AndroidIRC.out = out;
              out.write("NICK testrn");
              out.write("USER test . . :testuserrn");
              out.flush();
               
              while (s.isConnected()) {

                    if (in.ready()) {
                       read = in.readLine();
                       Log.d("AIRC_SERVER", read);
                   
                       if (read.startsWith("PING :")) {
                          reply = "PONG :" + read.split(":")[1] +"rn";
                          out.write(reply);
                          out.flush();
                          continue;
                       }
                       
                       AndroidIRC.msg = read + "n";
                       AndroidIRC.Handler.sendEmptyMessage(0);
                }
              }
               
          } catch (Exception e) {
               Log.e("AIRC_SERVER", "Error", e);
          }
     }
}


In LogCat, AIRC_SERVER (Server class) shows all the raw irc messages, but AIRC (main class) shows only a couple of the messages and always repeats a few of them.
confused
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Oct 03, 2008 2:30 pm

Postby confused » Thu Oct 23, 2008 6:20 pm

Got it working with Message.obtain(......).sendToTarget();
confused
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Oct 03, 2008 2:30 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 6 guests