Categories
discuss

What is the difference between JDBC API and PostgreSQL Driver?

I am creating a new Spring Boot project (https://start.spring.io/), and I am not understanding the different dependencies provided for interfacing with relational databases.

The point of confusion is “JDBC API” and “PostgresSQL Driver”. My app needs to connect to a PostgreSQL database. So, which of the following are true?

  1. JDBC API can be used to connect and operate with any relational (SQL) database (MySQL, PostgreSQL, etc.), and PostgresSQL Driver is not needed for JDBC API to work.

  2. Both JDBC API and PostgresSQL Driver are needed for an application to connect to a PostgreSQL database.

  3. The dependency PostgresSQL Driver includes JDBC API.

I have already googled about this, but there are only manuals of how to use them.

Answer

JDBC defines an API to connect and work with relational databases. The PostgreSQL driver is an implementation of this API for the PostgreSQL database.

You cannot use the JDBC API without an underlying driver to implement it. You could, however, use the driver directly, but drivers usually have very little guarantees about the stability of their APIs (other than what’s promised by JDBC, of course), so it would probably be a poor idea.

Categories
discuss

Ionic Allow location all the time API level 29

I’m trying to use API level 29 on my ionic app, and I require the app to ask the user for “Allow all the time” location. I’ve added all the suggested modifications.

My AndroidManifest.xml:

 <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION">
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
 <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION">
 <uses-permission android:name="android.permission.PERMISSION.FOREGROUND_SERVICE">

My app.component.ts:

initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      this.setProviders();
      this.checkBackgroundPermission();
    });
  }

private async checkBackgroundPermission() {
    try {
      const result = await this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.ACCESS_BACKGROUND_LOCATION);
      if (!result || result.hasPermission === false) {
        this.requestPermissions();
      }
    } catch (error) {
      this.requestPermissions();
    }
  }

 private async requestPermissions() {
    try {
      const data = await this.androidPermissions.requestPermissions([
        this.androidPermissions.PERMISSION.ACCESS_BACKGROUND_LOCATION,
        this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION,
        this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION,
        this.androidPermissions.PERMISSION.ACTIVITY_RECOGNITION,
        this.androidPermissions.PERMISSION.FOREGROUND_SERVICE
      ]);
      if (!data.hasPermission) {
        throw new Error('No permission');
      }
    } catch (error) {
      await this.alertService.showAlert(
        'Background location',
        'We need background location access in order to continue.'
      );
      this.signOut();
    }
  

However when I target Api Level 29 it doesn’t show the request for allow location all the time.

allthetime

But if I use Api Level 28 it shows the permission I need

Any suggestions to make this work?

Answer

I had exactly the same problem. The following worked for me.

  1. Use the AndroidPermissions to request the following permissions.
    const data = await this.androidPermissions.requestPermissions([
            "android.permission.ACCESS_BACKGROUND_LOCATION",
            "android.permission.ACCESS_COARSE_LOCATION",
            this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION,
    ]);
    

Note the string "android.permission.ACCESS_BACKGROUND_LOCATION", instead of this.androidPermissions.PERMISSION.ACCESS_BACKGROUND_LOCATION, which is actually undefined during runtime. My guess is that the ACCESS_BACKGROUND_LOCATION permission didn’t exist when the AndroidPermissions plugin was released.

  1. Call the requestPermissions method before Background Location. This is very important.

  2. Add <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> to platformsandroidappsrcmainAndroidManifest.xml

Categories
discuss

The verification ID used to create the phone auth credential is invalid in flutter

I’m trying to achieve Firebase phone auth in my flutter application, I’ve used packages : Firebase_core And Firebase_auth , here is the code i’ve use d:

          FirebaseAuth _auth = FirebaseAuth.instance;
          _auth.verifyPhoneNumber(
              phoneNumber: '+2$mobile',
              timeout: Duration(seconds: 60),
              verificationCompleted: (AuthCredential authCredential){
                var _credential = PhoneAuthProvider.credential(verificationId: actualCode, smsCode: smsCodeController.text);
                _auth.signInWithCredential(_credential).then((UserCredential result) async {
                  pr.hide();
                  setState(() {
                    status = 'Authentication successful';
                  });
//The rest of my success code
                }).catchError((e){
                  print(e);
                  Navigator.of(context).pushAndRemoveUntil(
                      MaterialPageRoute(
                          builder: (context) => Welcome()),
                          (Route<dynamic> route) => false);                
};
              },
              verificationFailed: (FirebaseAuthException  authException){
                print(authException.message);
              },
              codeSent: (String verificationId, [int forceResendingToken]){
                setState(() {
                  actualCode = verificationId;
                  status = 'Code sent';
                });
              },
              codeAutoRetrievalTimeout: (String verificationId){
                verificationId = verificationId;
                print(verificationId);
                setState(() {
                  status = 'Auto retrieval timeout';
                });
              },
              );

But i always get this error :

[firebase_auth/invalid-verification-id] The verification ID used to create the phone auth credential is invalid.

Any help ??

Answer

I’m not sure why this line exists:

var _credential = PhoneAuthProvider.credential(verificationId: actualCode, smsCode: smsCodeController.text);

You already get a credential from the parameter authCredential so just sign in with that since it’s valid.

The way the verifyPhoneNumber method works in FirebaseAuth is as follows:

  • verificationFailed: callback for any errors thrown while verifying
  • verificationCompleted: if no sms code was needed to verify (google knows it’s a valid number)
  • codeSent: triggered when a code was sent, this is the function that you would have the user enter the sms code and create an AuthCredential from it with the provided verificationId and the sms code.
Categories
discuss

ReferrenceError while trying to access variable inside of Function constructor

Reading the mdn documentation on the difference between Function constructor and function declaration. The example specified there works on the browser and also on the node.js repl, but on trying it through a file, the node.js process crashed with this error

ReferenceError: x is not defined

This is the program

var x = "bar";

function test() {
    var x = "baz";
    return new Function("return x;");
}

var t = test();
console.log(t());

What might be the possible reason for this example not work as expected when been executed from a file with node.js?

Answer

In the Node REPL, the lexical location of where you’re typing in code is the top level, equivalent to typing stuff into the top of a <script> tag in the browser.

Variables defined with var on the top level get assigned to the global object. So, in both Node’s REPL and the browser, your

var x = "bar";

results in x being assigned to the global object.

But, in contrast, when you run the code from a file, eg node bar.js, the code that is run is inside a module – it’s not on the top level, so variables declared at the top level of such a script do not get assigned to the global object.

The function that is created is global, at the top level, so it can only lexically “see” variables defined at the top level. So, when running the code as a file in Node, since the scope of the code being run is not the top level, the created function can’t see x anywhere, so a ReferenceError is the result.

Categories
discuss

Constraint layout not matching parent in Nested scrollview

Constraint layout not matching parent in Nestedscrollview and all in coordinate layout. So, i am including layout file in Coordinate layout . Below is the code and Image of Layout . Screenshot

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="true"

   >
<androidx.core.widget.NestedScrollView
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    >


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <TextView
           android:id="@+id/delivery_tv_label"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:text="Delivery"
           android:textSize="14sp"
           android:textAlignment="center"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintLeft_toLeftOf="parent"
           app:layout_constraintRight_toLeftOf="@id/rating_tv_label"
           app:layout_constraintHorizontal_weight="5"
           />
        <TextView
            android:id="@+id/rating_tv_label"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Overall Rating"
            android:textSize="14sp"
            android:textAlignment="center"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toRightOf="@id/delivery_tv_label"
            app:layout_constraintHorizontal_weight="5"
            />
        <TextView
            android:id="@+id/delivery_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="21 Mins"
            android:textSize="16sp"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            android:textAlignment="center"
            android:layout_marginTop="5dp"
           app:layout_constraintTop_toBottomOf="@id/delivery_tv_label"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/rating_tv_label"
            app:layout_constraintHorizontal_weight="5"
            />

        <LinearLayout
            android:id="@+id/linear_layout"
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            app:layout_constraintTop_toBottomOf="@id/rating_tv_label"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toRightOf="@id/delivery_tv"
            android:layout_marginTop="5dp"
            >
            <TextView
                android:id="@+id/rating_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="4.2"
                android:textSize="16sp"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:textAlignment="center"

                />

            <ImageView
                android:id="@+id/star_img"
                android:src="@drawable/ic_baseline_star_24"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginTop="2dp"

                />
        </LinearLayout>
        <TextView
            android:id="@+id/rets"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="@string/large_text"
            android:textSize="16sp"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_item_title"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@id/linear_layout"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"

            />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

This layout is one of the child of CoordinatorLayout

Answer

Edit : I’m going to elaborate more on why this is the solution.

The reason you need fillViewPort is you’ve a RecyclerView inside which means you want to fill the available space with it as you’ve also used 0dp. But, the problem is without fillViewPort, it won’t behave like fill available space (0dp) and will work as wrap_content. So, the moment you set fillViewPort, it fills all the available space even with less content. Check these images for its working.

You can also check similar question here.


Remember ScrollView child’s default height is wrap_content and hence its child layout ConstraintLayout is also taking wrap_content as the height as mentioned in below image. enter image description here This is not necessary in case of NestedScrollView but its child still acts as wrap_content.

You just have to set android:fillViewport="true" in NestedScrollView as

<androidx.core.widget.NestedScrollView
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    >
    ...
</androidx.core.widget.NestedScrollView>

Setting fillViewPort will do the trick as it will act like match_parent or in your case, 0dp.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..