2012-01-30 100 views
0

我最近剛剛開始進行Android應用程序開發。 我的應用程序需要我連接到MySQL數據庫。因此我使用FTP服務器來存儲我的PHP文件。爲了訪問數據庫,android手機上的應用程序必須連接到FTP服務器。在FTP服務器上的Android文件上傳引發IllegalStateException

我試圖連接到FTP服務器,但是應用程序拋出:

java.lang.IllegalStateException:計劃 '的ftp' 未註冊的

package com.example.login2; 

import java.util.ArrayList; 
import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.util.Log; 
import org.apache.commons.net.ftp.*; 


public class login2 extends Activity { 
EditText un,pw; 
TextView error; 
Button ok; 
public boolean mConnect; 
public FTPClient mFTPClient = null; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    un=(EditText)findViewById(R.id.et_un); 
    pw=(EditText)findViewById(R.id.et_pw); 
    ok=(Button)findViewById(R.id.btn_login); 
    error=(TextView)findViewById(R.id.tv_error); 

    ok.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      mConnect = ftpConnect("ftp.example.comule.com", "name","passwordxxx", 21); 
      ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
      postParameters.add(new BasicNameValuePair("username", un.getText().toString())); 
      postParameters.add(new BasicNameValuePair("password", pw.getText().toString())); 
      //String valid = "1"; 
      String response = null; 
      try { 
       response = CustomHttpClient.executeHttpPost("ftp://[email protected]/footprint/login.php", postParameters); 
       String res=response.toString(); 
       // res = res.trim(); 
       res= res.replaceAll("\\s+","");        
       //error.setText(res); 

       if(res.equals("1")) 
        error.setText("Correct Username or Password"); 
       else 
        error.setText("Sorry!! Incorrect Username or Password"); 
      } catch (Exception e) { 
       un.setText(e.toString()); 
      } 
     } 
    }); 
} 

public boolean ftpConnect(String host, String username, String password, int port) 
{ 
    try { 
     mFTPClient = new FTPClient(); 
     // connecting to the host 
     try{ 
      mFTPClient.connect(host, port); 
     }catch(Exception e) 
     { 
      Toast toast1 = Toast.makeText(getApplicationContext(), "This is the exception thrown: "+e, 1000000); 
      toast1.show(); 
     } 

     // now check the reply code, if positive mean connection success 
     if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) { 
      // login using username & password 
     boolean status = mFTPClient.login(username, password); 

     //toast.show(); 

     /* Set File Transfer Mode 
     * 
     * To avoid corruption issue you must specified a correct 
     * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE, 
     * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE 
     * for transferring text, image, and compressed files. 
     */ 
     mFTPClient.setFileType(FTP.BINARY_FILE_TYPE); 
     mFTPClient.enterLocalPassiveMode(); 

     return status; 
    } 
    } catch(Exception e) { 
     Log.d("ftpConnectApp", "Error: could not connect to host " + host); 
    } 
    return false; 
    } 

}

回答

0

你正在做使用FTP服務器上HttpPost的Http請求。嘗試使用經典的Url連接到您的FTP服務器,它應該可以解決您的問題。我找到了你2個教程給你一些指點:

File upload
How to upload a file using FTP

+0

感謝的人,我得到了整個過程錯誤。感謝您清理它! – DroidMatt 2012-02-02 05:26:40

0

我猜有一些缺失。嘗試直接連接到數據庫服務器或通過在http服務器上執行的php頁面連接。