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);
                ftpDisconnect();

                if (status == true) {
                 Toast.makeText(SiteManager.this, "Connection Succesful",
                 Toast.LENGTH_LONG).show();
                 } else {
                 Toast.makeText(SiteManager.this,
                 "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() {

            @Override
            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);
                task.execute();
                // 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
    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(mContext, "Please wait for ",
                "Process Description Text", true);

        // do initialization of required objects objects here
    };

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

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

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        progressDialog.dismiss();
    };
}

 Answers

56
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;
    }

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


    @Override
    protected void onPostExecute(Boolean result) {
        if (mListener != null) 
            mListener.myMethod(result);
    }
}

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

task.execute();

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
 
JohnnyW
answered 7 Months ago
36

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 brighthub.com/mobile/google-android/articles/82805.aspx. Scroll down to Source Code section and have a look at the code for WebServiceAsyncTask and WebServiceBackgroundActivity.

Tuesday, June 22, 2021
 
pocketfullofcheese
answered 6 Months ago
21

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
 
vivek
answered 4 Months ago
47

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
 
hakre
answered 4 Months ago
73

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();
         gpu.this.data = result;  
         if (gpu.this.pd != null) {
             //gpu.this.pd.dismiss();
         }
      tx.setText(gpu2dcurent);
     }
    }

Update:

After update your code in question,

change this line,

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

to

new readgpu2d().execute("blablabla").get();
Friday, September 24, 2021
 
jxh
answered 2 Months ago
jxh
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 :  
Share