0

I am creating app that can run 2 process at the time (saving to database / sending to api) but , i was encountering issue from my treading.This is my code upon my threading:

this is my code:

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

and this is my error encountered:

enter image description here

PS. I was wondering that my code is to totally not working though i want also other possible code implementation with this one, like asynctask to work with this multiple process + with threading in a single meathod.

1 Answers1

0

For threading, please consider using RxJava. It would be as simple as this

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() {

        }
    });

Check this link out on how to integrate RxJava into your project https://github.com/ReactiveX/RxAndroid

Bach Vu
  • 2,298
  • 1
  • 15
  • 19
  • another answer sir, how to implement this code? its not existing on me Either it has implementation or library to config. –  Mar 04 '19 at 05:14
  • Yes, if you look at the link above, you need to add these to your `build.gradle`: `implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'` and `implementation 'io.reactivex.rxjava2:rxjava:2.x.x'` – Bach Vu Mar 04 '19 at 06:20
  • Glad that I could help :) – Bach Vu Mar 04 '19 at 07:47
  • 1
    i've search for other possible question and this is the I've looking for. Though has another implementation and library to use to have the same process. :) –  Mar 04 '19 at 07:52