2 wjwjx123456 wjwjx123456 于 2013.09.10 17:28 提问

android client使用 apache 第三方远程访问包TelnetClient,一直出错请众大神帮帮忙

error:
dalvikvm Could not find class 'org.apache.commons.net.telnet.TelnetClient',

referenced from method com.test.telnet.TelnetUtil.
AndroidRuntime FATAL EXCEPTION: Thread-1263
AndroidRuntime java.lang.NoClassDefFoundError:

org.apache.commons.net.telnet.TelnetClient
AndroidRuntime at com.test.telnet.TelnetUtil.(TelnetUtil.java:9)
AndroidRuntime at com.test.main.MainActivity$2.run(MainActivity.java:46)
AndroidRuntime at java.lang.Thread.run(Thread.java:841)

source:
package com.test.telnet;

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

public class TelnetUtil {

private TelnetClient telnet = new TelnetClient();

private InputStream in;  

private PrintStream out; 

private String port;

private String ip;

public TelnetUtil(String ip, String port){

    this.ip=ip;

    this.port=port;

}

 /** 
 * @return boolean 连接成功返回true,否则返回false 
 */  

public boolean connect() {  

    boolean isConnect = true;  

    try { 

        telnet.connect(ip, Integer.parseInt(port));  

        in = telnet.getInputStream();  

        out = new PrintStream(telnet.getOutputStream());  

    } catch (Exception e) {  

        isConnect = false;  

        e.printStackTrace();  

        return isConnect;  

    }

    return isConnect;  
}  

protected void disconnect() { 

    try {  

        telnet.disconnect();  

    } catch (Exception e) {

        e.printStackTrace();  

    }  

}  

public String readUtil() { 

    try {  

        StringBuffer sb = new StringBuffer();  

        char ch=0;

        while (true) {  

            ch = (char) in.read();

            sb.append(ch);

            if(sb.length()>=512){  //----buffer size = 512*2 byte = 1 kB---
                                   //synchronized block  
                return sb.toString();

            }
        }  

    } catch (Exception e) {  

        e.printStackTrace();  

    } 

    return null;  

}  

private void write(String value) { 

    try {  

        out.println(value);

        out.flush();  

    } catch (Exception e) { 

        e.printStackTrace(); 

    }  

}  

/**
 * 向服务器发送命令
 * @param string
 */

public String sendCommand(String command) {  

    try {  

        write(command);

    } catch (Exception e) {

        e.printStackTrace();

    }  

    return null;  

}  

}

线程内调用:

TelnetUtil tu = new TelnetUtil("218.56.40.136","1602");
tu.connect();
tu.sendCommand("client");
res=tu.readUtil();

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!