 |
Android - - ช่วยดูโค้ดให้ทีค่ะ อยากให้มันโชว์ไฟล์ของ ftp server แต่พอรันโค้ดแล้วมมัน error T^T |
|
 |
|
|
 |
 |
|
พอดีทำหน้าเกี่ยวกับการโชว์ file ของ server พอดีรันแล้วมันไม่ออก
อยากขอให้ช่วยดูโค้ดให้หน่อยค่ะ อยากทราบว่ามีที่ผิดตรงไหนรึป่าว
Code (Android-Java)
package com.example.android;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.TextView;
public class Listview extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
task getson = new task();
getson.execute();
}
FTPClient client;
public FTPClient connectWithFTP() throws IOException{
client = new FTPClient();
try {
client.connect("hostname", 21);
client.login("username", "password");
System.out.println("status :: " + client.getReplyString());
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
throw new Exception("Connect failed: " + client.getReplyString());
}
try {
client.enterLocalPassiveMode();
if (!client.setFileType(FTP.BINARY_FILE_TYPE)) {
Log.v(getClass().toString(), "Setting binary file type failed.");
}
} catch(Exception e) {
e.printStackTrace();
}
checkFiles(client);
} catch (Exception e) {
System.out.println("status :: " + client.getReplyString());
}
return client;
}
private class task extends AsyncTask <Void, Void, FTPClient> {
@Override
protected void onPreExecute() {
// Do stuff before the operation
}
@Override
protected FTPClient doInBackground(Void... params) {
FTPClient ftp = null;
try {
ftp = connectWithFTP();
} catch (IOException e) {
e.printStackTrace();
}
return ftp;
}
}
public void disconnectWithFTP() throws IOException{
client.logout();
client.disconnect();
}
private void checkFiles(FTPClient clients){
try {
FTPFile[] ftpFiles = clients.listFiles();
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
Calendar date = ftpFiles[i].getTimestamp();
Log.v("aasd", name+", "+date);
}
} catch(Exception e) {
e.printStackTrace();
}
}
@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;
}
}
Tag : Mobile, Android, JAVA
|
|
 |
 |
 |
 |
Date :
2014-01-21 23:06:57 |
By :
iamillusion |
View :
1121 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Error อะไรครับ มี LogCat ให้ดูหรือเปล่าครับ
|
 |
 |
 |
 |
Date :
2014-01-22 09:16:41 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณค่ะ แล้วถ้าอยากนำ file ที่เราได้มามาใส่ใน listview และ checkbox เราจะใช้ค่าตัวไหนมาโชว์ใน listview อ่าคะ
|
 |
 |
 |
 |
Date :
2014-01-22 23:55:45 |
By :
iamillusion |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (Android-Java)
private List <String> getSD()
{
List <String> it = new ArrayList <String>();
File f = new File ("/mnt/sdcard/picture");
File[] files = f.listFiles ();
for (int i = 0; i <files.length; i++)
{
File file = files[i];
Log.d("Count",file.getPath());
it.add (file.getPath());
}
return it;
}
Android Populate ListView ImageView get Image Resource from SD ...
|
 |
 |
 |
 |
Date :
2014-01-23 09:36:48 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองแก้แล้วอ่าค่ะ แต่มันไม่โชว์ใน listview
Code (Android-Java)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
task getson = new task();
getson.execute();
List<String> Listfile = null ;
Listfile = checkFiles(client);
System.out.println("showfile uuu :: "+ Listfile);
//String[] FileList = null; //new String[] {"1","2","3","4","5","6"};
// listView1
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
// System.out.println("show :: "+ Listfile);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Listfile);
lisView1.setAdapter(adapter);
}
FTPClient client;
public FTPClient connectWithFTP() throws IOException{
client = new FTPClient();
try {
client.connect("server", 21);
client.login("username", "password");
System.out.println("status :: " + client.getReplyString());
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
throw new Exception("Connect failed: " + client.getReplyString());
}
try {
client.enterLocalPassiveMode();
if (!client.setFileType(FTP.BINARY_FILE_TYPE)) {
Log.v(getClass().toString(), "Setting binary file type failed.");
}
} catch(Exception e) {
e.printStackTrace();
}
List<String> aa = checkFiles(client);
System.out.println("check :: " +aa);
} catch (Exception e) {
System.out.println("status :: " + client.getReplyString());
}
return client;
}
private class task extends AsyncTask <Void, Void, FTPClient> {
@Override
protected void onPreExecute() {
// Do stuff before the operation
}
@Override
protected FTPClient doInBackground(Void... params) {
FTPClient ftp = null;
try {
ftp = connectWithFTP();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public void disconnectWithFTP() throws IOException{
client.logout();
client.disconnect();
}
private List<String> checkFiles(FTPClient clients){
List<String> it = new ArrayList<String>();
try {
FTPFile[] ftpFiles = clients.listFiles();
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
it.add (name);
Log.v("aasd", name);
} // System.out.println("showfile :: "+ it);
} catch(Exception e) {
e.printStackTrace();
}
//System.out.println("showfile lol :: "+ it);
return it;
}
@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;
}
}
อันนี้เป็นไฟล์ xml
Code (Android-Java)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ListView and Checkbox : "
android:layout_span="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<RelativeLayout
android:layout_gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnCheckAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="49dp"
android:text="Check All" />
<Button
android:id="@+id/btnClearAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="36dp"
android:layout_toRightOf="@+id/btnCheckAll"
android:text="Clear All" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
<RelativeLayout
android:layout_gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnGetItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Get Item Checked" />
</RelativeLayout>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
</TableLayout>
|
ประวัติการแก้ไข 2014-01-23 12:26:01 2014-01-23 20:21:24 2014-01-23 20:23:08
 |
 |
 |
 |
Date :
2014-01-23 12:19:21 |
By :
iamillusion |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|