Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.nishant.ftp1;
- import android.annotation.SuppressLint;
- import android.content.res.Configuration;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.app.Activity;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- import org.apache.commons.net.ftp.*;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends Activity {
- public FTPClient mFTPClient = null;
- Button but;
- private RecyclerView.LayoutManager layoutManager;
- private ArrayList<FileListObject> fileList = new ArrayList<>();
- private RecyclerAdapter adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // but = (Button) findViewById(R.id.button1);
- /*but.setOnClickListener(new View.OnClickListener() {
- @SuppressLint("StaticFieldLeak")
- @Override
- public void onClick(View v) {
- AsyncTask asyncTask = new AsyncTask<Object, Void, Object>() {
- String host = "test.rebex.net", username = "demo", password = "password";
- int port = 21;
- @Override
- protected Object doInBackground(Object[] objects) {
- try {
- mFTPClient = new FTPClient();
- // connecting to the host
- mFTPClient.connect(host,port);
- // Now check the reply code, if positive mean connection success
- if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
- Log.v("wwww","Connected");
- // Login using username & password
- boolean status = mFTPClient.login(username, password);
- mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
- mFTPClient.enterLocalPassiveMode();
- String toppath=null;
- FTPFile[] ftpDirs = mFTPClient.listFiles("/pub/example/");
- for (int i = 0; i < ftpDirs.length; i++) {
- toppath = ftpDirs[0].getName();
- Log.d("CONNECT", "Directories in the ftp server are "
- + ftpDirs[i].getName());
- }
- return status;
- }
- } catch (Exception e) {
- return e;
- }
- return null;
- }
- public void onPostExecute(Object res) {
- if (res instanceof Exception) {
- int d = Toast.LENGTH_SHORT;
- Toast toast = Toast.makeText(getApplicationContext(), "" + res, d);
- toast.show();
- }
- }
- }.execute();
- }
- });*/
- RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
- recyclerView.setHasFixedSize(true);
- layoutManager = new LinearLayoutManager(this);
- recyclerView.setLayoutManager(layoutManager);
- adapter = new RecyclerAdapter(fileList, this);
- // setUpList();
- recyclerView.setAdapter(adapter);
- new setUpList().execute();
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
- recyclerView.setHasFixedSize(true);
- layoutManager = new LinearLayoutManager(this);
- recyclerView.setLayoutManager(layoutManager);
- Log.v("rec","in recycler");
- adapter = new RecyclerAdapter(fileList, this);
- recyclerView.setAdapter(adapter);
- }
- /* private void setUpList() {
- @SuppressLint("StaticFieldLeak")
- AsyncTask asyncTask = new AsyncTask<Object, Void, Object>() {
- String host = "test.rebex.net", username = "demo", password = "password";
- int port = 21;
- @Override
- protected Object doInBackground(Object[] objects) {
- try {
- mFTPClient = new FTPClient();
- // connecting to the host
- mFTPClient.connect(host,port);
- // Now check the reply code, if positive mean connection success
- if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
- Log.v("wwww","Connected");
- // Login using username & password
- boolean status = mFTPClient.login(username, password);
- mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
- mFTPClient.enterLocalPassiveMode();
- String toppath=null;
- try {
- FTPFile[] ftpDirs = mFTPClient.listFiles("/pub/example/");
- for (int i = 0; i < ftpDirs.length; i++) {
- toppath = ftpDirs[0].getName();
- Log.d("CONNECT", "Directories in the ftp server are "
- + ftpDirs[i].getName());
- *//*FileListObject fileListObject = new FileListObject(ftpDirs[i].getName());
- fileList.add(fileListObject);*//*
- fileList.add(new FileListObject(ftpDirs[i].getName()));
- // adapter.notifyDataSetChanged();
- }
- adapter.notifyDataSetChanged();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return status;
- }
- } catch (Exception e) {
- return e;
- }
- return null;
- }
- public void onPostExecute(Object res) {
- if (res instanceof Exception) {
- int d = Toast.LENGTH_SHORT;
- Toast toast = Toast.makeText(getApplicationContext(), "" + res, d);
- toast.show();
- }
- }
- }.execute();
- }*/
- public class setUpList extends AsyncTask<Object, Void, Object> {
- String host = "test.rebex.net", username = "demo", password = "password";
- int port = 21;
- @Override
- protected Object doInBackground(Object[] objects) {
- try {
- mFTPClient = new FTPClient();
- // connecting to the host
- mFTPClient.connect(host,port);
- // Now check the reply code, if positive mean connection success
- if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
- Log.v("wwww","Connected");
- // Login using username & password
- boolean status = mFTPClient.login(username, password);
- mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
- mFTPClient.enterLocalPassiveMode();
- String toppath=null;
- try {
- FTPFile[] ftpDirs = mFTPClient.listFiles("/pub/example/");
- for (int i = 0; i < ftpDirs.length; i++) {
- toppath = ftpDirs[0].getName();
- Log.d("CONNECT", "Directories in the ftp server are "
- + ftpDirs[i].getName());
- /*FileListObject fileListObject = new FileListObject(ftpDirs[i].getName());
- fileList.add(fileListObject);*/
- fileList.add(new FileListObject(ftpDirs[i].getName()));
- // adapter.notifyDataSetChanged();
- }
- adapter.notifyDataSetChanged();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return status;
- }
- } catch (Exception e) {
- return e;
- }
- return null;
- }
- public void onPostExecute(Object res) {
- if (res instanceof Exception) {
- int d = Toast.LENGTH_SHORT;
- Toast toast = Toast.makeText(getApplicationContext(), "" + res, d);
- toast.show();
- }
- }
- }
- }
- package com.example.nishant.ftp1;
- /**
- * Created by Nishant on 1/24/2018.
- */
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.support.v7.widget.RecyclerView;
- import android.text.Html;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by Hitayu on 18-01-2018.
- */
- public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
- private ArrayList<FileListObject> fileList = new ArrayList<>();
- private Context context;
- public RecyclerAdapter(ArrayList<FileListObject> fileList, Context context) {
- this.fileList = fileList;
- this.context = context;
- }
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recycler_view,parent,false);
- return new ViewHolder(itemView);
- }
- @Override
- public void onBindViewHolder(ViewHolder holder, int position) {
- final FileListObject objec = fileList.get(position);
- Log.v("mmm",objec.getName());
- holder.fileText.setText(Html.fromHtml("<u>"+objec.getName()+"</u>"));
- /*holder.linearLayout.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(objec.getLocation()));
- context.startActivity(intent);
- }
- });*/
- }
- @Override
- public int getItemCount() {
- return fileList.size();
- }
- public class ViewHolder extends RecyclerView.ViewHolder{
- public TextView fileText;
- public LinearLayout linearLayout;
- public ViewHolder(View itemView) {
- super(itemView);
- fileText = (TextView) itemView.findViewById(R.id.file);
- linearLayout = (LinearLayout) itemView.findViewById(R.id.layout);
- }
- }
- }
- package com.example.nishant.ftp1;
- /**
- * Created by Nishant on 1/24/2018.
- */
- public class FileListObject {
- String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public FileListObject(String name) {
- this.name = name;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.nishant.ftp1">
- <uses-permission android:name="android.permission.INTERNET"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity"
- android:screenOrientation="portrait">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#F6F6F6"
- android:orientation="vertical"
- tools:context="com.example.nishant.ftp1.MainActivity">
- <android.support.v7.widget.RecyclerView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/recycler_view"/>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout android:id="@+id/layout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView
- android:id="@+id/file"
- android:layout_width="match_parent"
- android:textColor="@color/colorPrimaryDark"
- android:layout_height="wrap_content"
- android:textSize="18sp"/>
- </LinearLayout>
Add Comment
Please, Sign In to add comment