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.onCreate方法を必要としActivity、使用しますsetContentView(R.layout.main);

于 2012-05-20T16:43:22.600 に答える