2013-06-25 62 views
1

您好,我正在嘗試使用APACHE COMMONS LIBRARY爲Android創建應用程序。 此應用程序必須連接到服務器並使用Telnet發出一些命令。 這是我的應用程序... 清單:在Android中使用Apache Commons的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不返回任何錯誤。 請幫我:( 在那裏我錯了嗎?

+0

然後是什麼錯誤?會發生什麼/應該發生?請giva描述你的實際問題。 – AlexS

+0

沒有任何反應。沒有錯誤。我認爲問題出在printstream上,因爲連接開始但MKD​​IR失敗。 –

+0

任何想法?它不顯示任何錯誤,但它不起作用 –

回答

0

它永遠不會顯示任何錯誤,因爲你正在追趕每一個例外,不與它做任何事情。

在你抓塊你問該異常的消息,但是你既沒有存儲這個消息也不登錄,或以任何其他方式顯示它

要記錄異常使用您的捕獲塊是這樣的:

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

甚至:

Log.e("com.example.test.MainActivity", "MyFault", ex); 
+0

我使用其他庫解決了我的問題,非常感謝您的幫助。 –