Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old January 30th, 2012, 07:20 AM   #1 (permalink)
New Member
 
Join Date: Jan 2012
Posts: 2
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?

ur2cdanger is offline  
Reply With Quote
Sponsors
Old January 30th, 2012, 07:23 AM   #2 (permalink)
New Member
 
Join Date: Jan 2012
Posts: 2
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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(A ctivityThread.java:1956)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:1981)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.ActivityThread.access$600(ActivityThre ad.java:123)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.ActivityThread$H.handleMessage(Activit yThread.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.jav a: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$MethodAndArgsCa ller.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(SocketC lient.java:34)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.Activity.performCreate(Activity.java:4 465)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1049)
01-30 07:42:41.451: E/AndroidRuntime(535): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:1920)
01-30 07:42:41.451: E/AndroidRuntime(535): ... 11 more
[/error]
ur2cdanger is offline  
Reply With Quote
Old February 7th, 2012, 02:55 AM   #3 (permalink)
New Member
 
Join Date: Feb 2012
Location: Italy
Posts: 12
 
Device(s): Acer Liquid Metal
Thanks: 2
Thanked 3 Times in 3 Posts
Default

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());
aciampal is offline  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 12:11 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo