Basically……
I am trying to connect to a device over a TCP/IP connection.
I have a TextEdit field to enter the IP Address of the device and then on button click send the IP address through an intent to another class. Then connect to the device using the IP address and a fixed port of 32.
The connection itself does not require any authentication, but once connected the device asks for a username and password (in this case they are both root).
So once the connected I need to automatically send “root” wait for a response, send “root” again, and when a response of “SNX_COM>” (This is what I get when logged on) wait for further commands.
The further commands will come from button clicks.
The problem I have is that when I enter my IP address and press connect the app just force closes.
Please help……..
My IP Entry page (XML)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IPEntry" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:ems="10"
android:ellipsize="start"
android:gravity="center_horizontal"
android:hint="@string/enterip" />
<Button
android:id="@+id/connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="@string/ipconbut" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/connect"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp" />
</RelativeLayout>
My IP Entry page (Java Class)
package com.smarte.smartipcontrol;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class IPEntry extends Activity {
public final static String ACTUALSMARTIP = "com.smarte.smartipcontrol.ACTU_IP";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_ipentry);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.act_ipentry, menu);
return true;
}
/** Called when the user clicks the SendIP button */
public void sendip (View view) {
Intent intent = new Intent(this, IPControl.class);
EditText editText = (EditText) findViewById(R.id.smartipaddress);
String actu_ip = editText.getText().toString();
intent.putExtra(ACTUALSMARTIP, actu_ip);
startActivity(intent);
}
}
My IP Connection and Command send page (XML)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IPControl" >
<TextView
android:id="@+id/textStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/getModel"
android:text="@string/status"
android:textSize="24sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="@string/command" >
<requestFocus />
</EditText>
<Button
android:id="@+id/getModel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/editText1"
android:text="@string/Modelbutton" />
</RelativeLayout>
My IP Connection and Command send page (Java class)
package com.smarte.smartipcontrol;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class IPControl extends Activity {
private Socket socket;
private String serverIpAddress;
private static final int REDIRECTED_SERVERPORT = 32;
public PrintWriter out;
public BufferedReader in;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
serverIpAddress = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
createConnection();
}
public void getModel(View view) {
try {
out.println("[c,l#,o13,i13crlf\r\n");
//System.out.print("root\r\n");
while(!in.ready());
readBuffer();
} catch(IOException e) {}
}
public void createConnection() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(!in.ready());
readBuffer();
out.println("root\r\n");
//System.out.print("root\r\n");
while(!in.ready());
readBuffer();
out.println("root\r\n");
//System.out.print("root\r\n");
while(!in.ready());
readBuffer();
} catch(IOException e) {}
}
private String readBuffer() throws IOException {
String msg = "";
while(in.ready()) {
msg = msg + (char)in.read();
}
//System.out.print(msg);
if(msg.indexOf("SCX_COM> ") != -1) return msg.substring(0, msg.indexOf("SCX_COM> "));
else return msg;
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smarte.smartipcontrol"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"> </uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> </uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.smarte.smartipcontrol.IPEntry"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.smarte.smartipcontrol.IPControl" />
</application>
</manifest>
……finally the strings (just incase)
<string name="app_name">SmartiP Control</string>
<string name="enterip">Enter IP Address</string>
<string name="menu_settings">Settings</string>
<string name="ipconbut">Connect</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_ipcontrol">IPControl</string>
<string name="Modelbutton">Model</string>
<string name="command">Enter Command</string>
<string name="status">Status</string>
LogCat..
12-01 03:19:50.977: E/AndroidRuntime(1537): FATAL EXCEPTION: main
12-01 03:19:50.977: E/AndroidRuntime(1537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smarte.smartipcontrol/com.smarte.smartipcontrol.IPControl}: android.os.NetworkOnMainThreadException
12-01 03:19:50.977: E/AndroidRuntime(1537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-01 03:19:50.977: E/AndroidRuntime(1537): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-01 03:19:50.977: E/AndroidRuntime(1537): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-01 03:19:50.977: E/AndroidRuntime(1537): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-01 03:19:50.977: E/AndroidRuntime(1537): at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 03:19:50.977: E/AndroidRuntime(1537): at android.os.Looper.loop(Looper.java:137)
12-01 03:19:50.977: E/AndroidRuntime(1537): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-01 03:19:50.977: E/AndroidRuntime(1537): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 03:19:50.977: E/AndroidRuntime(1537): at java.lang.reflect.Method.invoke(Method.java:511)
12-01 03:19:50.977: E/AndroidRuntime(1537): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-01 03:19:50.977: E/AndroidRuntime(1537): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-01 03:19:50.977: E/AndroidRuntime(1537): at dalvik.system.NativeStart.main(Native Method)
12-01 03:19:50.977: E/AndroidRuntime(1537): Caused by: android.os.NetworkOnMainThreadException
12-01 03:19:50.977: E/AndroidRuntime(1537): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-01 03:19:50.977: E/AndroidRuntime(1537): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
12-01 03:19:50.977: E/AndroidRuntime(1537): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
12-01 03:19:50.977: E/AndroidRuntime(1537): at libcore.io.IoBridge.connect(IoBridge.java:112)
12-01 03:19:50.977: E/AndroidRuntime(1537): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
12-01 03:19:50.977: E/AndroidRuntime(1537): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
12-01 03:19:50.977: E/AndroidRuntime(1537): at java.net.Socket.startupSocket(Socket.java:566)
12-01 03:19:50.977: E/AndroidRuntime(1537): at java.net.Socket.<init>(Socket.java:225)
12-01 03:19:50.977: E/AndroidRuntime(1537): at com.smarte.smartipcontrol.IPControl.createConnection(IPControl.java:50)
12-01 03:19:50.977: E/AndroidRuntime(1537): at com.smarte.smartipcontrol.IPControl.onCreate(IPControl.java:33)
12-01 03:19:50.977: E/AndroidRuntime(1537): at android.app.Activity.performCreate(Activity.java:5104)
12-01 03:19:50.977: E/AndroidRuntime(1537): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-01 03:19:50.977: E/AndroidRuntime(1537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-01 03:19:50.977: E/AndroidRuntime(1537): ... 11 more
Please help me……….
Chris Stratton in comments above.
Thanks Chris….
“Caused by: android.os.NetworkOnMainThreadException” is your problem. There is ample documentation of that issue online, so please do some research and then restructure your program to be compatible with Android’s UI responsiveness goals.