基本上......
我正在尝试通过 TCP/IP 连接连接到设备。
我有一个 TextEdit 字段来输入设备的 IP 地址,然后单击按钮通过意图将 IP 地址发送到另一个类。然后使用 IP 地址和固定端口 32 连接到设备。
连接本身不需要任何身份验证,但一旦连接,设备就会要求输入用户名和密码(在这种情况下,它们都是 root)。
所以一旦连接我需要自动发送“root”等待响应,再次发送“root”,当“SNX_COM>”的响应(这是我登录时得到的)等待进一步的命令。
进一步的命令将来自按钮点击。
我遇到的问题是,当我输入我的 IP 地址并按连接时,应用程序只是强制关闭。
请帮忙........
我的 IP 条目页面 (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>
我的 IP 条目页面(Java 类)
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);
}
}
我的 IP 连接和命令发送页面 (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>
我的 IP 连接和命令发送页面(Java 类)
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;
}
}
安卓清单
<?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>
......最后是字符串(以防万一)
<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>
日志猫..
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
请帮我..........