doInBackground异常崩溃应用程序

时间:2014-04-17 04:37:26

标签: java android

此代码提出了很多问题,请注册http请求邮寄params。

这段代码让我的应用程序崩溃。

如何正确处理此异常?

我添加了logcat和register方法。谢谢

protected Void doInBackground(Void... params) {

     try {
         aController.register(getBaseContext(), regId);

    } catch (Exception e) {
        String error = e.getMessage();
        Log.v("Error", error);         
    } 

    return null;
}

Log Cat

04-17 06:40:03.662: D/CallbackProxy(10218): sendMessageToUiThreadSync Package=com.mytesting.app message=103
        04-17 06:40:06.034: E/AndroidRuntime(10218):    at com.mytesting.app.MainActivity$HttpAsyncTask.doInBackground(MainActivity.java:514)
        04-17 06:40:06.034: E/AndroidRuntime(10218):    at com.mytesting.app.MainActivity$HttpAsyncTask.doInBackground(MainActivity.java:1)
        04-17 06:40:06.484: D/CrashAnrDetector(727): processName: com.mytesting.app
        04-17 06:40:06.484: V/SmartFaceService - 3rd party pause(727): onReceive [android.intent.action.ACTIVITY_STATE/com.mytesting.app/pause]

将其注册为GCM

void register(final Context context, final String regId) {


        String serverUrl = Config.YOUR_SERVER_URL;

        Map<String, String> params = new HashMap<String, String>();
        params.put("RegId", regId);
        params.put("Lang", getString(R.string.R_Locale));


        long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);

        // Once GCM returns a registration id, we need to register on our server
        // As the server might be down, we will retry it a couple
        // times.
        for (int i = 1; i <= MAX_ATTEMPTS; i++) {

            Log.d(Config.TAG, "Attempt #" + i + " to register");

            try {
                post(serverUrl, params);

                GCMRegistrar.setRegisteredOnServer(context, true);

                //Send Broadcast to Show message on screen
                String message = context.getString(R.string.server_registered);

                return;

            } catch (IOException e) {

                // Here we are simplifying and retrying on any error; in a real
                // application, it should retry only on unrecoverable errors
                // (like HTTP error code 503).

                Log.e(Config.TAG, "Failed to register on attempt " + i + ":" + e);

                if (i == MAX_ATTEMPTS) {
                    break;
                }
                try {

                    Log.d(Config.TAG, "Sleeping for " + backoff + " ms before retry");
                    Thread.sleep(backoff);

                } catch (InterruptedException e1) {
                    // Activity finished before we complete - exit.
                    Log.d(Config.TAG, "Thread interrupted: abort remaining retries!");
                    Thread.currentThread().interrupt();
                    return;
                }

                // increase backoff exponentially
                backoff *= 2;
            }
        }

0 个答案:

没有答案