Guest User

Untitled

a guest
Jan 29th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.29 KB | None | 0 0
  1. package com.example.nishant.ftp1;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.content.res.Configuration;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.app.Activity;
  8. import android.support.v7.widget.LinearLayoutManager;
  9. import android.support.v7.widget.RecyclerView;
  10. import android.util.Log;
  11. import android.view.Menu;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.Toast;
  15.  
  16. import org.apache.commons.net.ftp.*;
  17.  
  18. import java.util.ArrayList;
  19. import java.util.List;
  20.  
  21. public class MainActivity extends Activity {
  22. public FTPClient mFTPClient = null;
  23. Button but;
  24.  
  25.  
  26.  
  27. private RecyclerView.LayoutManager layoutManager;
  28. private ArrayList<FileListObject> fileList = new ArrayList<>();
  29. private RecyclerAdapter adapter;
  30.  
  31.  
  32.  
  33.  
  34.  
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_main);
  39. // but = (Button) findViewById(R.id.button1);
  40. /*but.setOnClickListener(new View.OnClickListener() {
  41.  
  42. @SuppressLint("StaticFieldLeak")
  43. @Override
  44. public void onClick(View v) {
  45. AsyncTask asyncTask = new AsyncTask<Object, Void, Object>() {
  46. String host = "test.rebex.net", username = "demo", password = "password";
  47. int port = 21;
  48.  
  49. @Override
  50. protected Object doInBackground(Object[] objects) {
  51. try {
  52. mFTPClient = new FTPClient();
  53. // connecting to the host
  54. mFTPClient.connect(host,port);
  55.  
  56. // Now check the reply code, if positive mean connection success
  57. if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
  58.  
  59. Log.v("wwww","Connected");
  60. // Login using username & password
  61. boolean status = mFTPClient.login(username, password);
  62. mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
  63. mFTPClient.enterLocalPassiveMode();
  64.  
  65.  
  66. String toppath=null;
  67.  
  68. FTPFile[] ftpDirs = mFTPClient.listFiles("/pub/example/");
  69. for (int i = 0; i < ftpDirs.length; i++) {
  70. toppath = ftpDirs[0].getName();
  71. Log.d("CONNECT", "Directories in the ftp server are "
  72. + ftpDirs[i].getName());
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. return status;
  83. }
  84. } catch (Exception e) {
  85. return e;
  86. }
  87. return null;
  88. }
  89.  
  90.  
  91. public void onPostExecute(Object res) {
  92. if (res instanceof Exception) {
  93. int d = Toast.LENGTH_SHORT;
  94. Toast toast = Toast.makeText(getApplicationContext(), "" + res, d);
  95. toast.show();
  96. }
  97. }
  98.  
  99. }.execute();
  100. }
  101. });*/
  102.  
  103.  
  104.  
  105. RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  106. recyclerView.setHasFixedSize(true);
  107.  
  108. layoutManager = new LinearLayoutManager(this);
  109. recyclerView.setLayoutManager(layoutManager);
  110.  
  111.  
  112. adapter = new RecyclerAdapter(fileList, this);
  113. // setUpList();
  114.  
  115. recyclerView.setAdapter(adapter);
  116. new setUpList().execute();
  117. }
  118.  
  119. @Override
  120. public void onConfigurationChanged(Configuration newConfig) {
  121. super.onConfigurationChanged(newConfig);
  122.  
  123. RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  124. recyclerView.setHasFixedSize(true);
  125.  
  126. layoutManager = new LinearLayoutManager(this);
  127. recyclerView.setLayoutManager(layoutManager);
  128.  
  129. Log.v("rec","in recycler");
  130. adapter = new RecyclerAdapter(fileList, this);
  131. recyclerView.setAdapter(adapter);
  132. }
  133.  
  134. /* private void setUpList() {
  135. @SuppressLint("StaticFieldLeak")
  136. AsyncTask asyncTask = new AsyncTask<Object, Void, Object>() {
  137. String host = "test.rebex.net", username = "demo", password = "password";
  138. int port = 21;
  139.  
  140. @Override
  141. protected Object doInBackground(Object[] objects) {
  142. try {
  143. mFTPClient = new FTPClient();
  144. // connecting to the host
  145. mFTPClient.connect(host,port);
  146.  
  147. // Now check the reply code, if positive mean connection success
  148. if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
  149.  
  150. Log.v("wwww","Connected");
  151. // Login using username & password
  152. boolean status = mFTPClient.login(username, password);
  153. mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
  154. mFTPClient.enterLocalPassiveMode();
  155.  
  156.  
  157. String toppath=null;
  158.  
  159. try {
  160. FTPFile[] ftpDirs = mFTPClient.listFiles("/pub/example/");
  161. for (int i = 0; i < ftpDirs.length; i++) {
  162. toppath = ftpDirs[0].getName();
  163. Log.d("CONNECT", "Directories in the ftp server are "
  164. + ftpDirs[i].getName());
  165. *//*FileListObject fileListObject = new FileListObject(ftpDirs[i].getName());
  166. fileList.add(fileListObject);*//*
  167. fileList.add(new FileListObject(ftpDirs[i].getName()));
  168. // adapter.notifyDataSetChanged();
  169. }
  170. adapter.notifyDataSetChanged();
  171.  
  172. } catch (Exception e) {
  173. e.printStackTrace();
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. return status;
  183. }
  184. } catch (Exception e) {
  185. return e;
  186. }
  187. return null;
  188. }
  189.  
  190.  
  191. public void onPostExecute(Object res) {
  192. if (res instanceof Exception) {
  193. int d = Toast.LENGTH_SHORT;
  194. Toast toast = Toast.makeText(getApplicationContext(), "" + res, d);
  195. toast.show();
  196. }
  197. }
  198.  
  199. }.execute();
  200. }*/
  201. public class setUpList extends AsyncTask<Object, Void, Object> {
  202.  
  203. String host = "test.rebex.net", username = "demo", password = "password";
  204. int port = 21;
  205.  
  206. @Override
  207. protected Object doInBackground(Object[] objects) {
  208. try {
  209. mFTPClient = new FTPClient();
  210. // connecting to the host
  211. mFTPClient.connect(host,port);
  212.  
  213. // Now check the reply code, if positive mean connection success
  214. if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
  215.  
  216. Log.v("wwww","Connected");
  217. // Login using username & password
  218. boolean status = mFTPClient.login(username, password);
  219. mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
  220. mFTPClient.enterLocalPassiveMode();
  221.  
  222.  
  223. String toppath=null;
  224.  
  225. try {
  226. FTPFile[] ftpDirs = mFTPClient.listFiles("/pub/example/");
  227. for (int i = 0; i < ftpDirs.length; i++) {
  228. toppath = ftpDirs[0].getName();
  229. Log.d("CONNECT", "Directories in the ftp server are "
  230. + ftpDirs[i].getName());
  231. /*FileListObject fileListObject = new FileListObject(ftpDirs[i].getName());
  232. fileList.add(fileListObject);*/
  233. fileList.add(new FileListObject(ftpDirs[i].getName()));
  234. // adapter.notifyDataSetChanged();
  235. }
  236. adapter.notifyDataSetChanged();
  237.  
  238. } catch (Exception e) {
  239. e.printStackTrace();
  240. }
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. return status;
  249. }
  250. } catch (Exception e) {
  251. return e;
  252. }
  253. return null;
  254. }
  255.  
  256.  
  257. public void onPostExecute(Object res) {
  258. if (res instanceof Exception) {
  259. int d = Toast.LENGTH_SHORT;
  260. Toast toast = Toast.makeText(getApplicationContext(), "" + res, d);
  261. toast.show();
  262. }
  263. }
  264. }
  265. }
  266.  
  267. package com.example.nishant.ftp1;
  268.  
  269. /**
  270. * Created by Nishant on 1/24/2018.
  271. */
  272.  
  273.  
  274. import android.content.Context;
  275. import android.content.Intent;
  276. import android.net.Uri;
  277. import android.support.v7.widget.RecyclerView;
  278. import android.text.Html;
  279. import android.util.Log;
  280. import android.view.LayoutInflater;
  281. import android.view.View;
  282. import android.view.ViewGroup;
  283. import android.widget.LinearLayout;
  284. import android.widget.TextView;
  285.  
  286. import java.util.ArrayList;
  287. import java.util.List;
  288.  
  289. /**
  290. * Created by Hitayu on 18-01-2018.
  291. */
  292.  
  293.  
  294. public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
  295.  
  296. private ArrayList<FileListObject> fileList = new ArrayList<>();
  297. private Context context;
  298.  
  299. public RecyclerAdapter(ArrayList<FileListObject> fileList, Context context) {
  300. this.fileList = fileList;
  301. this.context = context;
  302. }
  303.  
  304. @Override
  305. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  306. View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recycler_view,parent,false);
  307.  
  308. return new ViewHolder(itemView);
  309. }
  310.  
  311. @Override
  312. public void onBindViewHolder(ViewHolder holder, int position) {
  313.  
  314. final FileListObject objec = fileList.get(position);
  315. Log.v("mmm",objec.getName());
  316. holder.fileText.setText(Html.fromHtml("<u>"+objec.getName()+"</u>"));
  317.  
  318. /*holder.linearLayout.setOnClickListener(new View.OnClickListener() {
  319. @Override
  320. public void onClick(View v) {
  321. Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(objec.getLocation()));
  322. context.startActivity(intent);
  323. }
  324. });*/
  325. }
  326.  
  327. @Override
  328. public int getItemCount() {
  329. return fileList.size();
  330. }
  331.  
  332. public class ViewHolder extends RecyclerView.ViewHolder{
  333.  
  334. public TextView fileText;
  335. public LinearLayout linearLayout;
  336.  
  337. public ViewHolder(View itemView) {
  338. super(itemView);
  339.  
  340. fileText = (TextView) itemView.findViewById(R.id.file);
  341. linearLayout = (LinearLayout) itemView.findViewById(R.id.layout);
  342. }
  343. }
  344. }
  345.  
  346. package com.example.nishant.ftp1;
  347.  
  348. /**
  349. * Created by Nishant on 1/24/2018.
  350. */
  351.  
  352. public class FileListObject {
  353. String name;
  354.  
  355. public String getName() {
  356. return name;
  357. }
  358.  
  359. public void setName(String name) {
  360. this.name = name;
  361. }
  362.  
  363. public FileListObject(String name) {
  364. this.name = name;
  365. }
  366. }
  367.  
  368. <?xml version="1.0" encoding="utf-8"?>
  369. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  370. package="com.example.nishant.ftp1">
  371.  
  372. <uses-permission android:name="android.permission.INTERNET"/>
  373.  
  374. <application
  375. android:allowBackup="true"
  376. android:icon="@mipmap/ic_launcher"
  377. android:label="@string/app_name"
  378. android:roundIcon="@mipmap/ic_launcher_round"
  379. android:supportsRtl="true"
  380. android:theme="@style/AppTheme">
  381. <activity android:name=".MainActivity"
  382. android:screenOrientation="portrait">
  383.  
  384. <intent-filter>
  385. <action android:name="android.intent.action.MAIN" />
  386.  
  387. <category android:name="android.intent.category.LAUNCHER" />
  388. </intent-filter>
  389. </activity>
  390. </application>
  391.  
  392. </manifest>
  393.  
  394. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  395. xmlns:tools="http://schemas.android.com/tools"
  396. android:layout_width="match_parent"
  397. android:layout_height="match_parent"
  398. android:background="#F6F6F6"
  399. android:orientation="vertical"
  400. tools:context="com.example.nishant.ftp1.MainActivity">
  401.  
  402.  
  403. <android.support.v7.widget.RecyclerView
  404. android:layout_width="match_parent"
  405. android:layout_height="wrap_content"
  406. android:id="@+id/recycler_view"/>
  407. </LinearLayout>
  408.  
  409. <?xml version="1.0" encoding="utf-8"?>
  410.  
  411.  
  412. <LinearLayout android:id="@+id/layout"
  413. android:layout_width="match_parent"
  414. android:layout_height="wrap_content"
  415. xmlns:android="http://schemas.android.com/apk/res/android">
  416.  
  417. <TextView
  418. android:id="@+id/file"
  419. android:layout_width="match_parent"
  420. android:textColor="@color/colorPrimaryDark"
  421. android:layout_height="wrap_content"
  422. android:textSize="18sp"/>
  423.  
  424. </LinearLayout>
Add Comment
Please, Sign In to add comment