1

您好,我正在尝试使用 APACHE COMMONS LIBRARY 为 android 制作应用程序。此应用程序必须连接到服务器并使用 Telnet 发出一些命令。这是我的应用程序...清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-library android:name="org.apache.commons.net.telnet.TelnetClient" /> 
    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />  
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test.MainActivity"
            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>

活动主要

package com.example.test;
import java.io.PrintStream;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.view.Menu;
import org.apache.commons.net.telnet.TelnetClient;
import android.widget.Button;
import android.view.View;
public class MainActivity extends Activity {
    private Button bottone1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottone1 = (Button) findViewById(R.id.bottone1);
        bottone1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) { 
                //
                new Connection().execute();                 
              }
            });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    private class Connection extends AsyncTask {
        @Override
        protected Object doInBackground(Object... arg0) {
            connessione();
            return null;
        }
    }
    private void connessione()
    {
        PrintStream output;
        TelnetClient tc = new TelnetClient();
        try
        {
        tc.connect("www.servertotelnet.it",23);
        }
        catch(Exception ex)
        {
                         ex.getMessage();
        }
        output = new PrintStream(tc.getOutputStream());  
        try
        {
            Thread.sleep(500);
            output.println("users");   
            output.flush();
            Thread.sleep(500);
            output.println("password");   
            output.flush();   
            Thread.sleep(500);
            output.println("mkdir TEST");   
            output.flush();   
            Thread.sleep(500);
        }
        catch(Exception ex)
        {
            ex.getMessage();
        }
        try
        {
        tc.disconnect();
        output.close();
        }
        catch(Exception ex)
        {
            ex.getMessage();
        }
    }
}

当我运行它时,Eclipse 不会返回任何错误。请帮助我:(我错在哪里?

4

1 回答 1

0

它永远不会显示任何错误,因为您正在捕获每个异常并且不对它做任何事情。

在您的捕获块中,您请求异常消息,但您既不存储此消息,也不记录它或以任何其他方式显示它。

要记录异常,请在您的 catch 块中使用类似的内容:

Log.e("com.example.test.MainActivity", ex.getMessage());

甚至:

Log.e("com.example.test.MainActivity", "MyFault", ex);
于 2013-06-26T10:57:02.807 回答