3

我试图让下面的 AutomatedTelnetClient 在使用 Eclipse 的 Android 应用程序中工作。如果我创建一个新的 Java 项目(不是 Android)并添加以下代码,它可以正常工作。

问题是当我将相同的代码添加到 Android 项目时,我收到以下错误消息:

05-20 14:07:25.991: E/AndroidRuntime(337): FATAL EXCEPTION: main
05-20 14:07:25.991: E/AndroidRuntime(337): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jbapp/com.example.jbapp.AutomatedTelnetClient}: java.lang.ClassCastException: com.example.jbapp.AutomatedTelnetClient
05-20 14:07:25.991: E/AndroidRuntime(337):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
05-20 14:07:25.991: E/AndroidRuntime(337):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-20 14:07:25.991: E/AndroidRuntime(337):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-20 14:07:25.991: E/AndroidRuntime(337):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-20 14:07:25.991: E/AndroidRuntime(337):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 14:07:25.991: E/AndroidRuntime(337):  at android.os.Looper.loop(Looper.java:123)
05-20 14:07:25.991: E/AndroidRuntime(337):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-20 14:07:25.991: E/AndroidRuntime(337):  at java.lang.reflect.Method.invokeNative(Native Method)
05-20 14:07:25.991: E/AndroidRuntime(337):  at java.lang.reflect.Method.invoke(Method.java:507)
05-20 14:07:25.991: E/AndroidRuntime(337):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-20 14:07:25.991: E/AndroidRuntime(337):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-20 14:07:25.991: E/AndroidRuntime(337):  at dalvik.system.NativeStart.main(Native Method)
05-20 14:07:25.991: E/AndroidRuntime(337): Caused by: java.lang.ClassCastException: com.example.jbapp.AutomatedTelnetClient
05-20 14:07:25.991: E/AndroidRuntime(337):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-20 14:07:25.991: E/AndroidRuntime(337):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
05-20 14:07:25.991: E/AndroidRuntime(337):  ... 11 more

这是 AutomatedTelnetClient.class

package com.example.jbapp;

import java.io.InputStream;
import java.io.PrintStream;
      import org.apache.commons.net.telnet.TelnetClient;


public class AutomatedTelnetClient {    
    private TelnetClient telnet = new TelnetClient(); 
    private InputStream in; 
    private PrintStream out; 
    private String prompt = "$"; 
          private String server = "my.ip.add.ress";
          private String user = "userName";
          private String password = "password";

    public AutomatedTelnetClient() { 
        try { 
                // Connect to the specified server 
                telnet.connect(server, 23); 

                // Get input and output stream references 
                in = telnet.getInputStream(); 
                out = new PrintStream(telnet.getOutputStream()); 

                // Log the user on 
                readUntil("login: "); 
                write(user); 
                readUntil("Password: "); 
                write(password); 

                // Advance to a prompt 
                readUntil(prompt + " "); 
        } catch (Exception e) { 
                e.printStackTrace(); 
        } 
    } 

    public void su(String password) { 
        try { 
                write("su"); 
                readUntil("Password: "); 
                write(password); 
                prompt = "$"; 
                readUntil(prompt + " "); 
        } catch (Exception e) { 
                e.printStackTrace(); 
        } 
    } 

    public String readUntil(String pattern) { 
        try { 
                char lastChar = pattern.charAt(pattern.length() - 1); 
                StringBuffer sb = new StringBuffer(); 
                // boolean found = false; 
                char ch = (char) in.read(); 
                while (true) { 
                        System.out.print(ch); 
                        sb.append(ch); 
                        if (ch == lastChar) { 
                                if (sb.toString().endsWith(pattern)) { 
                                        return sb.toString(); 
                                } 
                        } 
                        ch = (char) in.read(); 
                } 
        } catch (Exception e) { 
                e.printStackTrace(); 
        } 
        return null; 
    } 

    public void write(String value) { 
        try { 
                out.println(value); 
                out.flush(); 
                System.out.println(value); 
        } catch (Exception e) { 
                e.printStackTrace(); 
        } 
    } 

    public String sendCommand(String command) { 
        try { 
                write(command); 
                return readUntil(prompt + " "); 
        } catch (Exception e) { 
                e.printStackTrace(); 
        } 
        return null; 
    } 

    public void disconnect() { 
        try { 
                telnet.disconnect(); 
        } catch (Exception e) { 
                e.printStackTrace(); 
        } 
    } 

    public static void main(String[] args) { 
        try { 
                AutomatedTelnetClient telnet = new AutomatedTelnetClient(); 
                System.out.println("Got Connection..."); 
                telnet.sendCommand("ps -ef "); 
                System.out.println("run command"); 
                telnet.sendCommand("ls "); 
                System.out.println("run command 2"); 
                telnet.disconnect(); 
                System.out.println("DONE"); 
        } catch (Exception e) { 
                e.printStackTrace(); 
        } 
    } 
}

这是我的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jbapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" /> 
<uses-library android:name="org.apache.commons.net.telnet.TelnetClient" />
<uses-permission android:name="android.permission.INTERNET"/> 
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".JbAndroidAppActivity"
        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.example.jbapp.DisplayMessageActivity" />
    <activity android:name="com.example.jbapp.AutomatedTelnetClient" />
</application>

</manifest>

我在按下按钮后使用此代码调用它:

    public void sendTelnet(View view) {    
    // Do something in response to button}
    Intent intent = new Intent(this, AutomatedTelnetClient.class);
    startActivity(intent);
    }

我对 Java/Andrtoid/Eclipse 很陌生,但在其他语言方面有丰富的经验。任何帮助表示赞赏!:)

谢谢!


我现在有了读取 Telnet 并将结果显示到 TextView 的代码!:)

AutomatedTelnetClient.class

package com.example.jbapp; 

import java.io.InputStream; 
import java.io.PrintStream; 

import org.apache.commons.net.telnet.TelnetClient; 

import android.app.Activity; 
import android.content.Intent;
import android.os.Bundle; 
import android.view.View;
import android.widget.TextView; 

public class AutomatedTelnetClient extends Activity {    
    private TelnetClient telnet = new TelnetClient();  
    private InputStream in;  
    private PrintStream out;  
    private String server = "my.ip.add.ress"; 
    private String prompt = "#";  
    private String user = "user"; 
    private String password = "password";
    String tmpOutput;
    TextView outputView;

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        outputView = (TextView)findViewById(R.id.output);
    } 

    public void sendTelnet(View view) {    
        // Do something in response to button}
        try {  
            // Connect to the specified server  
            telnet.connect(server, 23);  

            // Get input and output stream references  
            in = telnet.getInputStream();  
            out = new PrintStream(telnet.getOutputStream());  

            // Log the user on  
            readUntil("Login: ");  
            write(user);  
            readUntil("Password: ");  
            write(password);  
            readUntil("ATP>");  
            write("shell"); 

            // Advance to a prompt  
            readUntil(prompt + " "); 
            write("ls"); 
            readUntil(prompt + " "); 
            telnet.disconnect();
            outputView.setText(tmpOutput);
            finish();
            }  
        catch (Exception e) {  
            e.printStackTrace();  
        }  
    }   

    public String readUntil(String pattern) {  
 //     outputView.setText("DOESN'T work #2");
        try {  
            char lastChar = pattern.charAt(pattern.length() - 1);  
            StringBuffer sb = new StringBuffer();  
            // boolean found = false;  
            char ch = (char) in.read();  
            while (true) {  
                System.out.print(ch);
                tmpOutput = tmpOutput + ch;
                sb.append(ch);  
                if (ch == lastChar) {  
                    if (sb.toString().endsWith(pattern)) {
                        return sb.toString();  
                    }  
                }  
                ch = (char) in.read();  
            }  
        }  
        catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  

    public void write(String value) {  
        try {  
            outputView.setText(value);
            out.println(value);  
            out.flush();  
            System.out.println(value); 
        }  
        catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  

} 

这是我的 main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >
            <Button
                android:id="@+id/button_telnet"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:onClick="sendTelnet"
                android:text="@string/button_telnet" />
        </TableRow>
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >
            <TextView android:id="@+id/output"       
                android:layout_weight="1"        
                android:layout_width="fill_parent"        
                android:layout_height="fill_parent"        
                android:hint="@string/output_message" />  
        </TableRow>
</TableLayout>

这是 AndroidManifext.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jbapp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="10" />
    <uses-library android:name="org.apache.commons.net.telnet.TelnetClient" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AutomatedTelnetClient"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

非常感谢所有帮助!:)

4

1 回答 1

0

假设您使用的是 Eclipse,res/layout您应该在文件夹下看到一个main.xml. 这包含一个基本布局。你想要做的是添加android:id="@+id/output"TextView. 然后,您可以在您的代码中通过TextView output = (TextView)findViewById(R.id.output);并使用output.setText(myText);.

你当然需要Activity.onCreateActivity使用的方法setContentView(R.layout.main);

于 2012-05-20T16:43:22.600 回答