0

我正在创建可以同时运行 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();
        }

这是我遇到的错误:

在此处输入图像描述

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

4

1 回答 1

0

对于线程,请考虑使用 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

于 2019-03-04T03:54:12.963 回答