我正在尝试将音频文件从 sdcard 上传到 ftp 服务器。logcat 显示以下消息..
09-04 18:47:10.767: W/System.err(12584): java.net.ConnectException: failed to connect to /127.0.0.1 (port 21): connect failed: ECONNREFUSED (Connection refused)
09-04 18:47:10.777: W/System.err(12584): at libcore.io.IoBridge.connect(IoBridge.java:120)
09-04 18:47:10.777: W/System.err(12584): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-04 18:47:10.777: W/System.err(12584): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
09-04 18:47:10.777: W/System.err(12584): at java.net.Socket.connect(Socket.java:849)
09-04 18:47:10.777: W/System.err(12584): at org.apache.commons.net.SocketClient.connect(SocketClient.java:176)
09-04 18:47:10.777: W/System.err(12584): at org.apache.commons.net.SocketClient.connect(SocketClient.java:268)
09-04 18:47:10.777: W/System.err(12584): at com.example.andro.MainActivity$AsyncUpload.ftpUpload(MainActivity.java:56)
09-04 18:47:10.777: W/System.err(12584): at com.example.andro.MainActivity$AsyncUpload.doInBackground(MainActivity.java:91)
09-04 18:47:10.777: W/System.err(12584): at com.example.andro.MainActivity$AsyncUpload.doInBackground(MainActivity.java:1)
09-04 18:47:10.777: W/System.err(12584): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-04 18:47:10.777: W/System.err(12584): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-04 18:47:10.777: W/System.err(12584): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-04 18:47:10.777: W/System.err(12584): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-04 18:47:10.777: W/System.err(12584): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-04 18:47:10.777: W/System.err(12584): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-04 18:47:10.777: W/System.err(12584): at java.lang.Thread.run(Thread.java:856)
09-04 18:47:10.777: W/System.err(12584): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
09-04 18:47:10.787: W/System.err(12584): at libcore.io.Posix.connect(Native Method)
09-04 18:47:10.787: W/System.err(12584): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
09-04 18:47:10.787: W/System.err(12584): at libcore.io.IoBridge.connectErrno(IoBridge.java:133)
09-04 18:47:10.787: W/System.err(12584): at libcore.io.IoBridge.connect(IoBridge.java:118)
09-04 18:47:10.787: W/System.err(12584): ... 15 more
09-04 18:47:10.787: W/dalvikvm(12584): threadid=11: thread exiting with uncaught exception (group=0x2b542210)
09-04 18:47:10.797: E/AndroidRuntime(12584): FATAL EXCEPTION: AsyncTask #1
09-04 18:47:10.797: E/AndroidRuntime(12584): java.lang.RuntimeException: An error occured while executing doInBackground()
09-04 18:47:10.797: E/AndroidRuntime(12584): at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-04 18:47:10.797: E/AndroidRuntime(12584): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.lang.Thread.run(Thread.java:856)
09-04 18:47:10.797: E/AndroidRuntime(12584): Caused by: java.lang.NullPointerException
09-04 18:47:10.797: E/AndroidRuntime(12584): at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:471)
09-04 18:47:10.797: E/AndroidRuntime(12584): at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:534)
09-04 18:47:10.797: E/AndroidRuntime(12584): at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:583)
09-04 18:47:10.797: E/AndroidRuntime(12584): at org.apache.commons.net.ftp.FTP.quit(FTP.java:794)
09-04 18:47:10.797: E/AndroidRuntime(12584): at org.apache.commons.net.ftp.FTPClient.logout(FTPClient.java:697)
09-04 18:47:10.797: E/AndroidRuntime(12584): at com.example.andro.MainActivity$AsyncUpload.ftpUpload(MainActivity.java:79)
09-04 18:47:10.797: E/AndroidRuntime(12584): at com.example.andro.MainActivity$AsyncUpload.doInBackground(MainActivity.java:91)
09-04 18:47:10.797: E/AndroidRuntime(12584): at com.example.andro.MainActivity$AsyncUpload.doInBackground(MainActivity.java:1)
09-04 18:47:10.797: E/AndroidRuntime(12584): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-04 18:47:10.797: E/AndroidRuntime(12584): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-04 18:47:10.797: E/AndroidRuntime(12584): ... 5 more
我的代码是:
public class MainActivity extends Activity {
TextView tv1;
Button bt1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textView1);
bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(new AsyncUpload().execute() != null)
tv1.setText("uploaded");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class AsyncUpload extends AsyncTask<String, Void, Void>{
public boolean ftpUpload(){
FTPClient con = new FTPClient();
try
{
con.connect("127.0.0.1"); //here i receive exception
//the exception is java.unknownhostexception
//java.net.UnknownHostException: Unable to resolve host "ftp.194.90.81.149": No address associated with hostname
if (con.login("android", "123"))
{
con.enterLocalPassiveMode();
String data = Environment.getExternalStorageDirectory().getPath() + "audiorecordtest.3gp";
ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
boolean result = con.storeFile("/test.3gp", in);
in.close();
if (result) Log.v("upload result", "succeeded");
return true;
}
}
catch (Exception e)
{
e.printStackTrace();
}
try
{
con.logout();
con.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
@Override
protected Void doInBackground(String... params) {
ftpUpload();
return null;
}
}
}