title: Post value to server when no internet connection android studio
body: <p>I'm trying to send a value to the server when the connection doesn't exist, when it connects to the internet it runs fine and is saved to the server and sqlite. but the problem arises when switching state from offline to online. I send one value to the server when the connection is offline but when the state moves to online the value I sent earlier becomes multiple on the server while in the sqlite database only one value is stored.</p>
  <p>this is my network state checker class</p>
  <pre><code>public class NetworkStateCheckerNama extends BroadcastReceiver {
  
  //context and database helper object
  private Context context;
  private Database db;
  
  ApiRequestData apiRequestData;
  
  
  
  @Override
  public void onReceive(Context context, Intent intent) {
  
      this.context = context;
  
      apiRequestData = ServiceGenerator.createBaseService(this.context, ApiRequestData.class);
      db = new Database(context);
  
      ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
  
      //if there is a network
      if (activeNetwork != null) {
          //if connected to wifi or mobile data plan
          if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI || activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
  
              //getting all the unsynced user
              Cursor cursor2 = db.getUnsyncedNama();
              if (cursor2.moveToFirst()) {
                  do {
                      //calling the method to save the unsynced name to MySQL
                      Nama(
                              cursor2.getString(cursor2.getColumnIndexOrThrow(Database.KOLOM_NAMA_COBA)),
                              cursor2.getString(cursor2.getColumnIndexOrThrow(Database.KOLOM_NIP_COBA))
                      );
                  } while (cursor2.moveToNext());
              }
          }
      }
  }
  
  private void Nama(final String nama,final String nip) {
      //Call call = retrofit.create(APIInterface.class).saveName(name);
      Call call = apiRequestData.nama_query(nama,nip);
      call.enqueue(new Callback&lt;Responses&gt;() {
          @Override
          public void onResponse(Call&lt;Responses&gt; call, Response&lt;Responses&gt; response) {
  
              if (response.code() == 200){
                  db.updateNamaStatus(nip, MainActivity.NAME_SYNCED_WITH_SERVER);
                  //sending the broadcast to refresh the list
                  context.sendBroadcast(new Intent(MainActivity.DATA_SAVED_BROADCAST_MAIN));
              }
          }
  
          @Override
          public void onFailure(Call&lt;Responses&gt; call, Throwable t) {
  
          }
      });
  }
  </code></pre>
  <p>this is my MainActivity Class</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  Session session;
  Button buttonLogout,buttonKirim;
  TextView textViewNamaMain, textViewNipMain;
  
  EditText editTextNamaMain;
  
  Database database;
  
  
  private AdapterNama mAdapter;
  private RecyclerView listviewNama;
  ApiRequestData apiRequestData;
  //1 means data is synced and 0 means data is not synced
  public static final int NAME_SYNCED_WITH_SERVER = 1;
  public static final int NAME_NOT_SYNCED_WITH_SERVER = 0;
  
  //a broadcast to know weather the data is synced or not
  public static final String DATA_SAVED_BROADCAST_MAIN = &quot;com.example.myapplication.datasave2&quot;;
  ArrayList&lt;Nama&gt; namaArray;
  ArrayList&lt;User&gt; users;
  BroadcastReceiver broadcastReceiver;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      session = new Session(this);
  
      buttonLogout = findViewById(R.id.buttonLogout);
      buttonKirim = findViewById(R.id.buttonKirim);
  
      textViewNipMain = findViewById(R.id.textViewNipMain);
      textViewNamaMain = findViewById(R.id.textViewNamaMain);
  
      editTextNamaMain = findViewById(R.id.editTextTextNamaMain);
  
      String nip = getIntent().getStringExtra(&quot;nip&quot;);
  
      listviewNama =  findViewById(R.id.lv_nama);
  
      RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
      listviewNama.setLayoutManager(layoutManager);
      listviewNama.setItemAnimator(new DefaultItemAnimator());
  
      database = new Database(this);
      namaArray = new ArrayList&lt;&gt;();
      users = new ArrayList&lt;&gt;();
      apiRequestData = ServiceGenerator.createBaseService(this, ApiRequestData.class);
      textViewNipMain.setText(nip);
  
  
      
      registerReceiver(new NetworkStateCheckerNama(), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
      
      loadDaftarNama(nip);
  
      broadcastReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              //loading the names again
              loadDaftarNama(nip);
          }
      };
      registerReceiver(broadcastReceiver, new IntentFilter(DATA_SAVED_BROADCAST_MAIN));
  
      if(!session.isUserLogin()){
          logout();
      }
  
      buttonKirim.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              SimpanKeServer();
          }
      });
  
      buttonLogout.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              logout();
          }
      });
  
      users.clear();
      Cursor cursor = database.getUsersWhereNip(nip);
      if (cursor.moveToFirst()) {
          do {
              User user = new User(
                      cursor.getString(cursor.getColumnIndexOrThrow(Database.KOLOM_NIP)),
                      cursor.getString(cursor.getColumnIndexOrThrow(Database.KOLOM_NAMA)),
                      cursor.getString(cursor.getColumnIndexOrThrow(Database.KOLOM_PASSWORD)),
                      cursor.getInt(cursor.getColumnIndexOrThrow(Database.KOLOM_STATUS))
              );
              users.add(user);
              textViewNamaMain.setText(user.getNama());
          } while (cursor.moveToNext());
      }
  }
  
  
  
  private void SimpanKeServer(){
      final ProgressDialog progressDialog = new ProgressDialog(this);
      progressDialog.setMessage(&quot;Saving Name...&quot;);
      progressDialog.show();
  
      String nama = editTextNamaMain.getText().toString();
      String nip = textViewNipMain.getText().toString();
      
      Call call = apiRequestData.nama_query(nama,nip);
      call.enqueue(new Callback&lt;Responses&gt;() {
          @Override
          public void onResponse(Call&lt;Responses&gt; call, Response&lt;Responses&gt; response) {
              progressDialog.dismiss();
              if(response.code() == 200){
                  //if there is a success
                  //storing the name to sqlite with status synced
                  SaveNamaToLocal(nama,nip,NAME_SYNCED_WITH_SERVER);
                  Toast.makeText(MainActivity.this, &quot;Berhasil&quot;, Toast.LENGTH_SHORT).show();
              }else {
                  progressDialog.dismiss();
                  //if there is some error
                  //saving the name to sqlite with status unsynced
                  SaveNamaToLocal(nama,nip,NAME_NOT_SYNCED_WITH_SERVER);
                  Toast.makeText(MainActivity.this, &quot;Gagal&quot;, Toast.LENGTH_SHORT).show();
              }
          }
  
          @Override
          public void onFailure(Call&lt;Responses&gt; call, Throwable t) {
              progressDialog.dismiss();
              SaveNamaToLocal(nama,nip,NAME_NOT_SYNCED_WITH_SERVER);
              Toast.makeText(MainActivity.this, &quot;Gagal&quot;, Toast.LENGTH_SHORT).show();
          }
      });
  }
  
  
  private void SaveNamaToLocal(String nama, String nip, int status){
      database.Nama(nama,nip,status);
      Nama namas = new Nama(nama,nip,status);
      namaArray.add(namas);
      refreshList();
  }
  
  
  private void loadDaftarNama(String nip) {
      namaArray.clear();
      Cursor cursor = database.getNama(nip);
      if (cursor.moveToFirst()) {
          do {
              Nama nama = new Nama(
  
                      cursor.getString(cursor.getColumnIndexOrThrow(Database.KOLOM_NAMA_COBA)),
                      cursor.getString(cursor.getColumnIndexOrThrow(Database.KOLOM_NIP_COBA)),
                      cursor.getInt(cursor.getColumnIndexOrThrow(Database.KOLOM_STATUS_NAMA_COBA))
              );
              namaArray.add(nama);
          } while (cursor.moveToNext());
      }
  
      mAdapter = new AdapterNama(this, namaArray);
      listviewNama.setAdapter(mAdapter);
  }
  
  @SuppressLint(&quot;NotifyDataSetChanged&quot;)
  private void refreshList() {
      mAdapter.notifyDataSetChanged();
  }
  
  
  private void logout(){
      session.updateUserLoginStatus(false);
      finish();
      startActivity(new Intent(MainActivity.this,LoginActivity.class));
  }
  </code></pre>
  <p>}</p>
  <p>this when online</p>
  <p><a href="https://i.stack.imgur.com/D6wbS.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p><a href="https://i.stack.imgur.com/9c654.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>this the problem when state internet switch from offline to online</p>
  <p><a href="https://i.stack.imgur.com/e5QG5.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p><a href="https://i.stack.imgur.com/DELnv.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p><a href="https://i.stack.imgur.com/vvdGP.png" rel="nofollow noreferrer">Even in server send 3 same values</a></p>
  <p>I'm sorry for my english and my question structure im new in programmer, and thank you for the answer</p>