Sweetie Anang
Sweetie Anang

Reputation: 332

Trying to figure out why "AsyncTask" is not working like I expect and How to fix it

I have this AsynTask class that works perfectly on an activity. I used a similar method in a new app that uses fragments because of tabs and it's failing me.

Below is the context menu item where I instantiate the class (OverviewFragment.java):

@Override
public boolean onContextItemSelected(MenuItem item){
    //AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    //int position = info.position;

    super.onContextItemSelected(item);
    if(item.getTitle() == "Show Report"){

        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/"  + this.getPDFFileName() + ".pdf");
        intent.setDataAndType(Uri.fromFile(file), "application/pdf");
        startActivity(intent);
    }

    if(item.getTitle()=="Upload report via webservice")
    {
        itemClicked = "uploadMain_file";

        uploadToERP uploadToERP = new uploadToERP(this.getActivity());
        uploadToERP.execute();

    }


    if(item.getTitle()=="Send as attachment")
    {

        this.sendMail(this.getPDFFileName());

    }
    if(item.getTitle()=="Upload to ERP")
    {

        itemClicked = "uploadMain_data";
        uploadToERP uploadToERP = new uploadToERP(getActivity());
        uploadToERP.execute();

    }

    return true;
}

And here is the class:

package com.application.sweetiean.stlmaintenance;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import com.application.sweetiean.stlservicing.Serv_OverviewFragment;
import com.application.sweetiean.stlservicing.ServicingActivity;

/**
 * Created by sweetiean on 1/15/2016.
*/
public class uploadToERP extends AsyncTask<String, Integer, Void> {

Context context;
String file;
String Maintenance_Info;
String Task;
String Service_Info;
String Servicing;
String Replacement;

public uploadToERP(Context c){
    context = c;
}

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

    Boolean doNothing = false;

    try{


        if(OverviewFragment.itemClicked.equals("uploadMain_file")) {
            file = MaintenanceActivity.Main_axcall.SendMainFile(OverviewFragment.getBytesFromFile(), OverviewFragment.sysaidIdFileName + ".pdf");
            doNothing = true;
        }
        /*if(Serv_OverviewFragment.itemClicked.equals("uploadServ_file")) {
            file = ServicingActivity.Serv_axcall.SendServFile(Serv_OverviewFragment.getBytesFromFile(),Serv_OverviewFragment.sysaidIdFileName+".pdf");
            doNothing = true;
        }*/



        if(!doNothing) {
            //check info because you will be receiving from both info data
            MaintenanceAppDB getRecord = new MaintenanceAppDB(context);

            //since there are two modules using this class I will have to set itemclicked for all the context
            //menu items and use that check
            if(OverviewFragment.itemClicked.equals("uploadMain_data")) {
                Maintenance_Info = getRecord.getMaintenanceInfoRecord(OverviewFragment.sysaidIdFileName);

                Task = getRecord.getTaskRecord(OverviewFragment.sysaidIdFileName);
            }

            /*if(Serv_OverviewFragment.itemClicked.equals("uploadServ_data")) {
                Service_Info = getRecord.getServiceInfoRecord(Serv_OverviewFragment.sysaidIdFileName);
                Servicing = getRecord.getServicingRecord(Serv_OverviewFragment.sysaidIdFileName);
                Replacement = getRecord.getReplacementRecord(Serv_OverviewFragment.sysaidIdFileName);

            }*/
        }//end insert



    }catch (Exception e){


        e.printStackTrace();


    }

    return null;
}



@Override
protected void onPostExecute(Void aVoid) {

    if(OverviewFragment.itemClicked.equals("uploadMain_file")) {

        Toast.makeText(context, file, Toast.LENGTH_SHORT).show();

    }/*else if(Serv_OverviewFragment.itemClicked.equals("uploadServ_file")) {

        Toast.makeText(context, file, Toast.LENGTH_SHORT).show();

    }*/else if(OverviewFragment.itemClicked.equals("uploadMain_data")) {

        Toast.makeText(context, Maintenance_Info, Toast.LENGTH_SHORT).show();
        Toast.makeText(context, Task, Toast.LENGTH_SHORT).show();

    }/*else if(Serv_OverviewFragment.itemClicked.equals("uploadServ_data")) {

        Toast.makeText(context, Service_Info, Toast.LENGTH_SHORT).show();
        Toast.makeText(context, Servicing, Toast.LENGTH_SHORT).show();
        Toast.makeText(context, Replacement, Toast.LENGTH_SHORT).show();

    }*/

    Toast.makeText(context, "Upload to ERP complete!!!", Toast.LENGTH_LONG).show();

}



}

Any help will be deeply appreciated. I have read many other answers since last Friday and I still can't find a solution to my problem. The onPostExecute method works alright but the doInBackground method does not get called at all. I realised that when I was debugging.


After some much-needed motivation from Gavriel and David, I finally got the debugger to run through the doInbackground method. I commented out the second If statement as that particular class is not loaded at the time when the AsyncTask class is called(makes sense) Thank you David for making me look harder anyway so Gavriel, I finally got the debugger to run through that method and here is the error code I am getting now SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Value cannot be null. Parameter name: This method cannot be invoked because a required argument has not been supplied.' faultactor: 'null' detail: org.kxml2.kdom.Node@41e9cc30 on the line Maintenance_Info = getRecord.getMaintenanceInfoRecord(OverviewFragment.sysaidIdFileName); it's totally unrelated to my initial problem. I have to look to the methods in my db class and the webservice.java to figure out which required argument is returning null

Upvotes: 0

Views: 219

Answers (2)

Sweetie Anang
Sweetie Anang

Reputation: 332

The doInBackground method seemed not to be working because I was referencing a class which was not loaded at the time doInBackground is called:

if(Serv_OverviewFragment.itemClicked.equals("uploadServ_file")) {
        file = ServicingActivity.Serv_axcall.SendServFile(Serv_OverviewFragment.getBytesFromFile(),Serv_OverviewFragment.sysaidIdFileName+".pdf");
        doNothing = true;
    }

This makes perfect sense. I was trying to accomplish two tasks with one class but I have since deleted all references to the Serv_OverviewFragment from this class and created a new asyncTask for that purpose.

Now to my second error I was getting after this was fixed was caused as a result of the difference in parameter names in the android code and the c# code on the server side. So I have since compared and ensured the parameter names are all spelled the same and any case-sensitive issues resolved. Once again I want to show my appreciation to Gavriel and David for their comments which nudged me into the right direction :-)

Upvotes: 0

Gavriel
Gavriel

Reputation: 19237

Update: I'm glad you found the line which throws the exception in doInBackground. I think fixing that will probably solve the problem.

General thoughts:

According to your code uploadToERP has to get a String as input:

uploadToERP extends AsyncTask<String, Integer, Void> 

but you haven't pass any:

uploadToERP uploadToERP = new uploadToERP(getActivity());
uploadToERP.execute(/* String is missing from here !!! */);

The 1st type of the triplet is the input type of doInBackground(), as you got it right. However someone has to pass the input to the AsyncTask. When you call execute, you have to pass the input value(s):

uploadToERP.execute("http://example.com");
uploadToERP.execute("http://example.com/1", "http://example.com/2", ...);

If you do not intend to use an input, then I suggest you to change it to:

uploadToERP extends AsyncTask<Void, Integer, Void> 

protected Void doInBackground(Void p...)

Upvotes: 2

Related Questions