问题描述
- 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();
时间: 2024-11-27 07:30:45