-2

I know this has been asked several times before but I am facing an issue. When I am connecting the device to the system and running my app it is working fine. Even if I am generating a debug apk and run the app, it works fine.

However, on generating a signed apk for the release of the app I am getting the below error:

FATAL EXCEPTION: AsyncTask #5
Process: com.xxxx.xxxxx, PID: 8180
java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

I cannot figure out where this exception is occurring and why?

Please, can anyone help?

I have already gone through several links as:

I am calling my async task as,

new BannerAdAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

And my async task is,

// Checking banner ad and displaying the appropriate banner ad
class BannerAdAsyncTask extends AsyncTask<String, Boolean, Boolean> {

    String addUserResponseCode;
    String responseForAdduserRequest;
    String msg;

    /*
     * (non-Javadoc)
     *
     * @see android.os.AsyncTask#onPreExecute()
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    /*
     * (non-Javadoc)
     *
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected Boolean doInBackground(String... params) {

        try {

                String fetchBannerURL = context.getString(R.string.site_url)
                        + "fetchbannerad.cfc?method=fetchbannerad";

                responseForAdduserRequest = fetchContentFromServer(fetchBannerURL);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);

        try {

            if (responseForAdduserRequest != null) {



            // Parse JSON

            JSONObject pages = new JSONObject(responseForAdduserRequest);

            String d = pages.getString("DATA");

            JSONArray dd = new JSONArray(d);

            int len = dd.length();

            for (int i = 0; i < dd.length(); i++) {

                String p = dd.getString(i);

                JSONArray values = new JSONArray(p);

                adFileName = values.getString(6);
                adLinkLocation = values.getString(7);
            }


            adBanner.setVisibility(View.VISIBLE);

            adBanner.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    // If the link location contains the companyid field,
                    // fetch the company id and navigate to the app's
                    // companies page with this id
                    if (adLinkLocation.contains("?companyid=")) {

                        int temp = adLinkLocation.indexOf("companyid=");

                        temp = temp + 10;

                        String companyid = adLinkLocation.substring(temp);

                        boolean isNumber = true;

                        try {

                            int id = Integer.parseInt(companyid);

                        } catch (NumberFormatException e) {
                            isNumber = false;
                        }

                        if (isNumber) {

                            Intent intent = new Intent(context,
                                    CompanyProfile.class);
                            intent.putExtra("company_id",
                                    Integer.parseInt(companyid));

                            // Get company branches as a cursor
                            final DataAdapter mDbHelper = new DataAdapter(
                                    context);
                            mDbHelper.createDatabase();
                            mDbHelper.open();

                            intent.putExtra("branch_id", primaryBranchID);
                            intent.putExtra("com_name", name);
                            intent.putExtra("com_tradename", tradename);
                            intent.putExtra("com_desc", "");
                            intent.putExtra("main_branch_id",
                                    primaryBranchID);

                            mDbHelper.close();

                            context.startActivity(intent);
                        }

                    } else {

                        // Otherwise load the link in browser
                        Intent intent = new Intent(
                                "android.intent.action.VIEW", Uri
                                .parse(adLinkLocation));
                        startActivity(intent);
                    }
                }
            });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

This is my code for the method that fetched the contents from the server. I suspect that issue is getting the response here,

    private static String fetchContentFromServer(String request) {

    /* Converting required variables to convert the response */
    String line = "";
    String responseString = "";

    /* Initializing the HTTP client */
    HttpClient httpClient = new DefaultHttpClient();

    try {

        /* Read and write connection timeout */
        HttpParams httpParameters = new BasicHttpParams();

        int timeOutInMillis = 8 * 1000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeOutInMillis);

        int socketTimeOutinMillis = 8000;
        HttpConnectionParams.setSoTimeout(httpParameters,
                socketTimeOutinMillis);

        /*
         * Getting the response by giving request and adding it to the
         * buffer reader to read the content
         */
        HttpResponse httpResponse = httpClient
                .execute(new HttpGet(request));
        InputStream inputStream = httpResponse.getEntity().getContent();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(inputStream));

        /* For all the lines in the response */
        while ((line = bufferedReader.readLine()) != null) {

            /* Appending every line to the result */
            responseString += line;
        }

        /* Closing the input stream and displaying result in text view */
        inputStream.close();

    } catch (Exception e) {

        e.printStackTrace();
    } finally {

        /* Closing the http connection */
        httpClient.getConnectionManager().shutdown();
    }

    return responseString;
}
devgeek
  • 398
  • 2
  • 12

1 Answers1

1

The issue in my case was that I was not getting any kind of server response after generating the release APK whereas all of the server responses were fine in the case of debug APK.

I added the below line in my gradle file,

    buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
devgeek
  • 398
  • 2
  • 12