在android中使用ftpclient从php文件发送和获取数据

pxq42qpu  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(185)

我做了一个带有登录和注册的应用程序,它应该使用ftpclient通过ftp与服务器连接,并将电子邮件和密码发送到服务器a中的php脚本以获取响应,但是当我尝试搜索php文件时,它返回null
在ftpclient.connect中,我使用了文件login.class.php所在的文件夹的远程地址
作为登录我使用的用户从用户连接到filezilla的密码相同;

public class BackgroundTask extends AsyncTask<String,Void,String> {

SharedPreferences preferences;
SharedPreferences.Editor editor;

Context context;

BackgroundTask(Context ctx){
    this.context = ctx;
}

@Override
protected String doInBackground(String... params) {

    preferences = context.getSharedPreferences("MYPREFS", Context.MODE_PRIVATE);
    editor = preferences.edit();
    editor.putString("flag","0");
    editor.commit();

    String task = params[0];

       if(task.equals("login")){
        String loginEmail = params[1];
        String loginPassword = params[2];
        try {
            FTPClient ftpclient = new FTPClient();
            ftpclient.connect("ftp://portalpaconstrushop%255C%2524portalpaconstrushop@waws-prod-sn1-123.ftp.azurewebsites.windows.net/site/wwwroot/classes");
            ftpclient.login("portalpaconstrushop\\$portalpaconstrushop","kSqygjfGRGP6zgBRGo1ihEzesmGlaC48RH3nn8Bk27Fay5B62q9EaZ5a4r9g");

            //send the email and password to the database
            OutputStream outputStream = ftpclient.storeFileStream("Login.class.php");
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            String myData = URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(loginEmail,"UTF-8")+"&"
                    +URLEncoder.encode("senha","UTF-8")+"="+URLEncoder.encode(loginPassword,"UTF-8");
            bufferedWriter.write(myData);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            //get response from the database
            InputStream inputStream = ftpclient.retrieveFileStream("Login.class.php");
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String dataResponse = "";
            String inputLine = "";
            while((inputLine = bufferedReader.readLine()) != null){
                dataResponse += inputLine;
            }
            bufferedReader.close();
            inputStream.close();
            ftpclient.disconnect();

            System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            System.out.println(dataResponse);

            editor.putString("flag","login");
            editor.commit();
            return  dataResponse;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return null;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

//This method willbe called when doInBackground completes... and it will return the completion string which
//will display this toast.
@Override
protected void onPostExecute(String s) {
    String flag = preferences.getString("flag","0");

    if(flag.equals("register")) {
        Toast.makeText(context,s,Toast.LENGTH_LONG).show();
    }
    if(flag.equals("login")){
        String test = "false";
        String name = "";
        String email = "";
        String[] serverResponse = s.split("[,]");
        test = serverResponse[0];
        name = serverResponse[1];
        email = serverResponse[2];

        if(test.equals("true")){
            editor.putString("name",name);
            editor.commit();
            editor.putString("email",email);
            editor.commit();
            Intent intent = new Intent(context,LogginIn.class);
            context.startActivity(intent);
        }else{
            display("Login Failed...", "That email and password do not match our records :(.");
        }
    }else{
        display("Login Failed...","Something weird happened :(.");
    }
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

public void display(String title, String message){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题