从android中的远程服务器获取文件

时间:2017-09-12 23:21:35

标签: java android

我最近开始使用Android,我必须在我的Android应用程序中编写一个代码片段,通过WiFi从连接到我的Android应用程序的同一网络的Raspberry pi传输文件。我正在使用JSCH库和给定here.

的代码示例

我试图在我的Android应用程序中包含此代码。但是,在try / Except块中,我的应用程序在session.connect()命令中崩溃。我似乎无法弄清楚这里的问题。代码编译和构建正常。我直接在我的Android手机而不是模拟器上运行它。应用程序在启动时崩溃,即使只需按一下按钮即可启动此活动。

有人可以帮帮我吗?

public class ft_mainpage extends AppCompatActivity{

Spinner spinner;
String[] paths = {"Red Tripple Band Pass","Water Level Index","Blue Tripple Band Pass", "RGB"};
TextView tv;

////////////////

////////////////
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ft_mainpage);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    tv = (TextView)findViewById(R.id.ft_mainpage_tv);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(ft_mainpage.this, android.R.layout.simple_spinner_item, paths);
    spinner = (Spinner)findViewById(R.id.ft_main_spinner);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            int position = spinner.getSelectedItemPosition();
            switch(position){
                case 1:
                    tv.setText("RTBP - Fetching Information");

                    Session session = null;
                    Channel channel = null;
                    ChannelSftp channelSftp = null;

                    try{
                        JSch ssh = new JSch();
                        session = ssh.getSession("pi","10.0.1.170",22);
                        session.setPassword("raspberry");
                        java.util.Properties config = new java.util.Properties();
                        config.put("StrictHostKeyChecking", "no");
                        session.setConfig(config);
                        session.connect();
                        channel = session.openChannel("sftp");
                        channel.connect();
                        channelSftp = (ChannelSftp) channel;
                        channelSftp.cd("/home/pi/Desktop/");
                        byte[] buffer = new byte[1024];
                        BufferedInputStream bis = new BufferedInputStream(channelSftp.get("Test.java"));
                        File newFile = new File(Environment.DIRECTORY_DOWNLOADS);
                        OutputStream os = new FileOutputStream(newFile);
                        BufferedOutputStream bos = new BufferedOutputStream(os);
                        int readCount;
                        while ((readCount = bis.read(buffer)) > 0) {
                            System.out.println("Writing: ");
                            bos.write(buffer, 0, readCount);
                        }
                        bis.close();
                        bos.close();

                    }catch (Exception ex){
                        ex.printStackTrace();
                    }


                    break;
                case 0:
                    tv.setText("WLI");
                    break;
                case 2:
                    tv.setText("RTBP");
                    break;
                case 3:
                    tv.setText("RGB");
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            int a =0;
        }
    });

}

即使我获取文件的方式有一些错误,导致错误的代码也在try / Except块中,所以我相信应用程序不应该崩溃并转到except块。但是,如上所述,整个应用程序崩溃。

我的Android Manifest中有以下权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />

1 个答案:

答案 0 :(得分:0)

“一个人”的评论是对的。

你必须在后台使用AsyncTask或类似的东西进行网络操作。