I recently started working with Android and I have to write a code snippet in my android application that transfers a file via WiFi from a Raspberry pi connected to the same network as my android application. I am using the JSCH library and a code example given here.
I have tried to include this code in my android application. However, in the try/Except block, my application crashes at the session.connect() command. I can't seem to figure out the problem here. The code compiles and builds fine. I am running it directly on my android phone rather than an emulator. The application crashes on start-up, even though this activity is started with the push of a button.
Can someone help me out?
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;
}
});
}
Even if there is some error in the way I am fetching files, the code causing the error is in a try/Except block, so I believe the application should not crash and go to the except block. However, as mentioned above, the entire application crashes.
I have the following permissions in my 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" />