3

我正在尝试使用 jsch 连接到我的开放式 ssh ubuntu 服务器我只是想看看我是否可以使用 SSH 从我的 android 手机连接到 linux 服务器,但它不起作用。当我在 android 手机上启动应用程序时应用程序崩溃我已附加代码和 LogCat 文件我是 java 和 eclipse 的新手

这是我的代码

package com.example.please;

import java.util.Properties;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    Button button;
    EditText usr_nm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("working");
        button=(Button) findViewById(R.id.btn);
        //button.setOnClickListener(handle);
        usr_nm = (EditText)findViewById(R.id.editText1);
        String host="x.x.x.x";

        new loadsomestuff().execute(host);
    }

    @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;
    }

    public class loadsomestuff extends AsyncTask<String, Integer, String>{

        String asd;
        @Override
        protected String doInBackground(String... arg0) {

            JSch jsch=new JSch();
            Properties props = new Properties(); 
            props.put("StrictHostKeyChecking", "no");

            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            config.put("compression.s2c", "zlib,none");
            config.put("compression.c2s", "zlib,none");

            Session session;
            try {
                session = jsch.getSession("user", "x.x.x.x",22);
                session.setConfig(config);
                session.setPassword("xxxxxxx");
                session.connect();
            } catch (JSchException e) {
                asd = "NOT_Executed";
                System.out.println("NOT_executed");
                e.printStackTrace();
                return "NOT_Executed";
            }          


            Context context = getApplicationContext();
            CharSequence text = "Connected to Pi";
            int duration = android.widget.Toast.LENGTH_SHORT;
            android.widget.Toast toast = android.widget.Toast.makeText(context, text, duration);
            toast.show();
            System.out.println("executed");
            asd = "executed";
            return "Executed";

            //return null;
        }

        @Override
        protected void onPostExecute(String abc){
            usr_nm.setText(asd);
        }

    }

}

LogCat 显示以下内容

07-31 16:21:53.298: E/Trace(781): error opening trace file: No such file or directory (2)
07-31 16:21:53.858: I/System.out(781): working
07-31 16:21:54.028: I/Choreographer(781): Skipped 38 frames!  The application may be doing too much work on its main thread.
07-31 16:21:54.078: D/gralloc_goldfish(781): Emulator without GPU emulation detected.
07-31 16:21:54.177: I/Choreographer(781): Skipped 46 frames!  The application may be doing too much work on its main thread.
07-31 16:21:54.547: I/System.out(781): NOT_executed
07-31 16:21:54.547: W/System.err(781): com.jcraft.jsch.JSchException: java.net.SocketException: socket failed: EACCES (Permission denied)
07-31 16:21:54.557: W/System.err(781):  at com.jcraft.jsch.Util.createSocket(Util.java:344)
07-31 16:21:54.557: W/System.err(781):  at com.jcraft.jsch.Session.connect(Session.java:215)
07-31 16:21:54.557: W/System.err(781):  at com.jcraft.jsch.Session.connect(Session.java:183)
07-31 16:21:54.557: W/System.err(781):  at com.example.please.MainActivity$loadsomestuff.doInBackground(MainActivity.java:64)
07-31 16:21:54.557: W/System.err(781):  at com.example.please.MainActivity$loadsomestuff.doInBackground(MainActivity.java:1)
07-31 16:21:54.557: W/System.err(781):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-31 16:21:54.557: W/System.err(781):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-31 16:21:54.567: W/System.err(781):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-31 16:21:54.567: W/System.err(781):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-31 16:21:54.577: W/System.err(781):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-31 16:21:54.577: W/System.err(781):  at java.lang.Thread.run(Thread.java:856)
07-31 16:21:54.577: W/System.err(781): Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)
07-31 16:21:54.577: W/System.err(781):  at libcore.io.IoBridge.socket(IoBridge.java:583)
07-31 16:21:54.588: W/System.err(781):  at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
07-31 16:21:54.588: W/System.err(781):  at java.net.Socket.startupSocket(Socket.java:559)
07-31 16:21:54.588: W/System.err(781):  at java.net.Socket.tryAllAddresses(Socket.java:127)
07-31 16:21:54.588: W/System.err(781):  at java.net.Socket.<init>(Socket.java:177)
07-31 16:21:54.588: W/System.err(781):  at java.net.Socket.<init>(Socket.java:149)
07-31 16:21:54.598: W/System.err(781):  at com.jcraft.jsch.Util.createSocket(Util.java:338)
07-31 16:21:54.608: W/System.err(781):  ... 10 more
07-31 16:21:54.608: W/System.err(781): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
07-31 16:21:54.618: W/System.err(781):  at libcore.io.Posix.socket(Native Method)
07-31 16:21:54.618: W/System.err(781):  at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
07-31 16:21:54.618: W/System.err(781):  at libcore.io.IoBridge.socket(IoBridge.java:568)
07-31 16:21:54.618: W/System.err(781):  ... 16 more

请帮忙

4

1 回答 1

7

您的应用程序必须具有以下权限AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
于 2013-07-31T20:26:41.697 回答