Asked  7 Months ago    Answers:  5   Viewed   102 times

I have some EditTexts that a user enters an ftp address, username, password, port anda testConnection button. If a connection is successfully estabished it returns a boolean value of true.

boolean status = ftpConnect(_address, _username, _password,_port);

                if (status == true) {
                 Toast.makeText(SiteManager.this, "Connection Succesful",
                 } else {
                 "Connection Failed:" + status, Toast.LENGTH_LONG).show();


I'm reworking my code to use AsyncTasks to perform the various ftp operations, but how can I pass back a boolean value if a connection is successfully made?

testConnection.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                _name = etSitename.getText().toString();
                _address = etAddress.getText().toString();
                _username = etUsername.getText().toString();
                _password = etPassword.getText().toString();
                _port = Integer.parseInt(etPort.getText().toString());

                AsyncConnectTask task = new AsyncConnectTask(SiteManager.this,
                        _address, _username, _password, _port);
                // boolean status = ftpConnect(_address, _username, _password,
                // _port);
                // ftpDisconnect();

                // if (status == true) {
                // Toast.makeText(SiteManager.this, "Connection Succesful",
                // Toast.LENGTH_LONG).show();
                // savesite.setVisibility(0);
                // } else {
                // Toast.makeText(SiteManager.this,
                // "Connection Failed:" + status, Toast.LENGTH_LONG)
                // .show();

                // }

And my AsyncTask

public class AsyncConnectTask extends AsyncTask<Void, Void, Void> {
    private Context mContext;
    private FTPHelper ftpHelper = new FTPHelper();
    private String _address;
    private String _user;
    private String _pass;
    private int _port;
    ProgressDialog progressDialog;

    public AsyncConnectTask(Context context, String address, String user,
            String pass, int port) {
        mContext = context;
        _address = address;
        _user = user;
        _pass = pass;
        _port = port;

    // declare other objects as per your need
    protected void onPreExecute() {
        progressDialog =, "Please wait for ",
                "Process Description Text", true);

        // do initialization of required objects objects here

    protected Void doInBackground(Void... params) {

        boolean status = ftpHelper.ftpConnect(_address, _user, _pass, _port);
        return null;

    protected void onPostExecute(Void result) {


public interface MyInterface {
    public void myMethod(boolean result);

public class AsyncConnectTask extends AsyncTask<Void, Void, Boolean> {

    private MyInterface mListener;

    public AsyncConnectTask(Context context, String address, String user,
        String pass, int port, MyInterface mListener) {
        mContext = context;
        _address = address;
        _user = user;
        _pass = pass;
        _port = port;
        this.mListener  = mListener;

    protected Boolean doInBackground(Void... params) {
        return result;

    protected void onPostExecute(Boolean result) {
        if (mListener != null) 

AsyncConnectTask task = new AsyncConnectTask(SiteManager.this,
                        _address, _username, _password, _port,  new MyInterface() {
    public void myMethod(boolean result) {
        if (result == true) {
            Toast.makeText(SiteManager.this, "Connection Succesful",
        } else {
            Toast.makeText(SiteManager.this, "Connection Failed:" + status, Toast.LENGTH_LONG).show();


If you call myMethod from onPostExecute the code inside it will run on the UI Thread. Otherwise you need to post a Runnable through a Handler

Tuesday, June 1, 2021
answered 7 Months ago

If you can somehow pass the Activity class or its context to the AsyncTask that will solve your issue for showing dialog. You would need to include another parameter together with the URL you are sending and put that parameter in a Context variable. And then whenever you need the dialog you use that context variable to show it.

If the dialog does not have a Context from which to show it will definitely run into runtime errors.

Update (put my comment up here as well): here we go... found a good example that you can modify to make use for your case. It's at Scroll down to Source Code section and have a look at the code for WebServiceAsyncTask and WebServiceBackgroundActivity.

Tuesday, June 22, 2021
answered 6 Months ago

GetName should return a const char *, however it is otherwise fine as written. Note however that returning a string like this can only ever possibly work for literal string constants, as shown above. You cannot use this pattern to return a calculated string value (if you try that, it will likely crash or give corrupted data); thus getName is wrong.

Also note that while C is case sensitive, Pascal is not, so getName and GetName are the same function in the Inno script. You might be getting away with this in the above case because the parameters are different, but I wouldn't rely on that -- you should give them distinct names. (Don't use the same name on the C side either, as DLL exports are sometimes looked up case-insensitively too.)

To return a calculated string, you should use a pattern like this:

DLL code:

void __stdcall CalculateName(char *buffer, size_t size)
    strncpy(buffer, "whatever", size);
    buffer[size-1] = 0;

Inno code:

procedure CalculateName(Buffer: AnsiString; Max: Cardinal);
external 'CalculateName@files:my.dll stdcall';

Max := 16;
Buffer := StringOfChar(#0, Max);
CalculateName(Buffer, Max);
SetLength(Buffer, Pos(#0, Buffer) - 1);

A few acceptable variations exist, for example you can make the DLL function return the number of characters actually written to the buffer, and use that in the subsequent SetLength rather than calling Pos to find the null terminator.

But you must:

  • Ensure that both sides are using the same string types, either both ANSI or both Unicode.
    • ANSI Inno Setup supports only ANSI strings with its String type.
    • Unicode Inno Setup supports either ANSI strings with AnsiString or Unicode strings with String.
  • When using Unicode strings, ensure that both sides agree whether Max and/or the return value is specified in characters or bytes (the example code assumes it's in characters).
  • Prior to calling the function, use either SetLength or StringOfChar to ensure that the buffer has been sized to the required maximum possible result length.
  • Ensure the called function does not try to write past this maximum length (which is easier if this is provided as a parameter to the function).
  • Ensure that if you're using Pos, the called function must ensure the value is null-terminated (or you need to be more careful than shown in the example).
  • Ensure that after the call you truncate the string to the actual length, either by using a returned value or by finding the null terminator.

One of the constraints in play here is that memory allocated by one side must be freed by the same side. You cannot safely release memory allocated on the "wrong" side of the DLL boundary, in either direction.

Friday, August 6, 2021
answered 4 Months ago

Try this.

SELECT (CASE WHEN LEN(SomeLongTextColumn) = 0 THEN 1 ELSE 0 END) AS IsEmtpy

@gbn has good explanation about how to return boolean.

Thursday, August 12, 2021
answered 4 Months ago

I have a doubt you are trying to set text of TextView before completion of your Asynctask.

Yes, either make it, static public String gpu2dcurent = "1234567";

Or, set Text of TextView in onPostExecute()

protected void onPostExecute(String result) {
         // Pass the result data back to the main activity
        gpu2dcurent = result;
         //Toast.makeText(getBaseContext(), result,
                //Toast.LENGTH_SHORT).show(); = result;  
         if (gpu.this.pd != null) {


After update your code in question,

change this line,

new readgpu2d().execute("blablabla");


new readgpu2d().execute("blablabla").get();
Friday, September 24, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :