应用程序在调试模式下工作,但崩溃与发布apk显示java.lang.RuntimeException:Android

时间:2017-11-11 08:47:41

标签: android android-asynctask

我知道之前曾多次询问过这个问题,但我遇到了一个问题。 当我将设备连接到系统并运行我的应用程序时它工作正常。 即使我正在生成调试apk并运行应用程序,它也能正常工作。

但是,在为应用程序发布生成已签名的apk时,我收到以下错误:

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)

我无法弄清楚这个异常发生在哪里以及为什么?

拜托,有人可以帮忙吗?

我已经通过以下几个链接:

我将异步任务称为

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

我的异步任务是,

// 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();
        }

    }
}

这是我从服务器获取内容的方法的代码。我怀疑这个问题在这里得到了回应,

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

1 个答案:

答案 0 :(得分:2)

我的案例中的问题是,在生成发布APK后我没有得到任何类型的服务器响应,而在调试APK的情况下,所有服务器响应都很好。

我在gradle文件中添加了以下行,

    buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }