• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps My first android program

I am writing my first android program.Messaging between two emulators.The server side executes fine but I have some problem with the client side.Here is the code :

Code:
package com.app.ServerClient;
import java.io.*;
import java.net.*;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
public class SocketClient extends Activity {
	
   private Button bt;
   private TextView tv;
   private Socket socket;
   private String serverIpAddress = "192.168.0.5";
   
   private static final int REDIRECTED_SERVERPORT = 5000;
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      bt = (Button) findViewById(R.id.myButton);
      tv = (TextView) findViewById(R.id.myTextView);
      try {
         InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
         tv.setText((CharSequence) serverAddr);
         socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
      } catch (UnknownHostException e1) {
         e1.printStackTrace();
         System.out.println("Here");
      } catch (IOException e1) {
         e1.printStackTrace();
         System.out.println("Here too");
      }
      bt.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
            try {
               EditText et = (EditText) findViewById(R.id.EditText01);
               String str = et.getText().toString();
               PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
               out.println(str);
               Log.d("Client", "Client sent message");
            } catch (UnknownHostException e) {
               tv.setText("Error1");
               e.printStackTrace();
            } catch (IOException e) {
               tv.setText("Error2");
               e.printStackTrace();
            } catch (Exception e) {
               tv.setText("Error3");
               e.printStackTrace();
            }
         }
      });
   }
}

This is my program for a client emulator.This gives me errors.When I went through the net I found out the error was because the network connection is done is UI thread itself and android>3 does not allow it.Can anybody say how to overcome this?
 
Here is the error :

[error]


01-30 07:42:41.451: E/AndroidRuntime(535): FATAL EXCEPTION: main
01-30 07:42:41.451: E/AndroidRuntime(535): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.ServerClient/com.app.ServerClient.SocketClient}: java.lang.ClassCastException: java.net.Inet4Address cannot be cast to java.lang.CharSequence
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.os.Looper.loop(Looper.java:137)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-30 07:42:41.451: E/AndroidRuntime(535): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 07:42:41.451: E/AndroidRuntime(535): at java.lang.reflect.Method.invoke(Method.java:511)
01-30 07:42:41.451: E/AndroidRuntime(535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-30 07:42:41.451: E/AndroidRuntime(535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-30 07:42:41.451: E/AndroidRuntime(535): at dalvik.system.NativeStart.main(Native Method)
01-30 07:42:41.451: E/AndroidRuntime(535): Caused by: java.lang.ClassCastException: java.net.Inet4Address cannot be cast to java.lang.CharSequence
01-30 07:42:41.451: E/AndroidRuntime(535): at com.app.ServerClient.SocketClient.onCreate(SocketClient.java:34)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.Activity.performCreate(Activity.java:4465)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-30 07:42:41.451: E/AndroidRuntime(535): ... 11 more
[/error]
 
The error is on line :

Code:
tv.setText((CharSequence) serverAddr);

serverAddr is of type InetAddress and cannot be casted directly into a CharSequence.
You can try convert to string first as in :

Code:
tv.setText((CharSequence) serverAddr.toString());
 
Back
Top Bottom