同时发生多个处理程序错误和处理程序错误

时间:2019-03-04 03:17:45

标签: android multithreading android-threading

我正在创建可以同时运行2个进程的应用程序(保存到数据库/发送到api),但是,我在踩踏时遇到了问题。这是我的线程代码:

这是我的代码:

public void timerToSaveSend() {
    Thread t1 = new Thread() {
        @Override
        public void run() {
            saving();

        }
    };
    t1.start();
    Thread t2 = new Thread(){
        @Override
        public void run() {
            sending();
        }
    };
    t2.start();
}


    private void sending() {
    //dataSendApi
    handler10 = new Handler();
    handler10.postDelayed(new Runnable() {
        @Override
        public void run() {
            try {
                Retrofit.Builder builder = new Retrofit.Builder()
                        .baseUrl("http://" + ADDRESS + ":" + PORT)
                        .addConverterFactory(GsonConverterFactory.create());

                Retrofit retrofit = builder.build();
                API locate = retrofit.create(API.class);

                Call<MapDetails> call = locate.mapDetailLocation(data);
                call.enqueue(new Callback<MapDetails>() {
                    @Override
                    public void onResponse(Call<MapDetails> call, 
                    Response<MapDetails> response) {
                        String portString = String.valueOf(portss);

                    }

                    @Override
                    public void onFailure(Call call, Throwable t) {
                        Log.d("Message: ", "Data not sent, please check your 
                        network connection.");

                    }

                });
            } catch (Exception e) {
                Toast.makeText(NavDrawerFleet.this, "Disconnected from Internet, Please Configure Settings", Toast.LENGTH_SHORT).show();
                restFailed();
            }


        }
    }, 10000);


}

private void saving() {
    //4SECOND
    handler2 = new Handler();
    handler2.postDelayed(new Runnable() {
        @Override
        public void run() {
            DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());
            SQLiteDatabase db = databaseHelper.getWritableDatabase();
            well2 = String.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.getDefault()).format(new java.util.Date()));
            boolean accepted = databaseHelper.saveLocationToLocalDatabase(gg, hidelat.getText().toString(), hidelon.getText().toString(), well2, "1", "9090", db);
            failedCount.setText(String.valueOf(retryList.size()));
            lat2 = hidelat.getText().toString();
            lon2 = hidelon.getText().toString();
            MapDetails mapDetails = new MapDetails(gg, hidelat.getText().toString(), hidelon.getText().toString(), well2, "1", 9090);
            data.add(mapDetails);
            retry2 = new NotSentModuleGetterSetter(hidelat.getText().toString(), hidelon.getText().toString(), well2);
            retryList.add(retry2);
            retrylist_adapter.notifyDataSetChanged();
            if (accepted == true)
                Log.w("Data Entered: ", "1st Copy");

        }
    }, 2000);
    saving();
        }

这是我遇到的错误:

enter image description here

PS。我想知道我的代码是否完全无法正常工作,尽管我还希望使用该代码实现其他可能的代码实现,例如asynctask可以与该多个进程一起使用+在单个肉类中进行线程化。

1 个答案:

答案 0 :(得分:1)

对于线程,请考虑使用RxJava。就这么简单

Observable.fromCallable(new Callable<Object>() {
      @Override public Object call() throws Exception {
        saving();
        return null;
      }
    })
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Observer<Object>() {
        @Override public void onSubscribe(Disposable d) {

        }

        @Override public void onNext(Object o) {

        }

        @Override public void onError(Throwable e) {

        }

        @Override public void onComplete() {

        }
    });

查看此链接,了解如何将RxJava集成到项目https://github.com/ReactiveX/RxAndroid