0

I have an issue with Android 6.0 = PERMISSION_NOT_GRANTED

P.S. I FOUND THAT PROBLEM IS WITH CONTACTS AND LOCATION PERMISSION, is anyone know how to fix it?

I can get DATA from JSON , BUT I can't send information to database because of PERMISSION_NOT_GRANTED exception.

public class DVLAresult extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback{

    private ProgressDialog progress;
    GPSTracker gps;

    RequestQueue requestQueue;
    String insertUrl = "http://zonec.net/project/insertDetails.php";

    double latitudeD;
    double longitudeD;

    String postCode;
    private DVLAInformation dvlaInfo = null;

    //DVLA Plate Number
    String dvlaPlateNumber;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dvlaresult);

        //Intent dvla from Index class
        Intent intent = getIntent();
        dvlaPlateNumber = intent.getStringExtra("dvlaNumber");

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_my_contacts, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        Intent intentURL = new Intent(Intent.ACTION_VIEW);
        //Set URL for similar car, based on retrieved information from current car
        String URL = ("http://www.autotrader.co.uk/search/used/cars/" + dvlaInfo.getMake() + "/postcode/" + postCode + "/radius/5/sort/default/onesearchad/used%2Cnearlynew%2Cnew/page/1/searchcontext/default/colour/" + dvlaInfo.getColour().toLowerCase() + "/fuel-type/" + dvlaInfo.getFuelType().toLowerCase() + "");
        intentURL.setData(Uri.parse(URL));

        switch (item.getItemId()) {
            //Panel button which redirect to website with similar car which yser currently was looking for.
            case R.id.action_check_similar:
                startActivity(intentURL);
                return true;

            //Panel button, redirect to report about this application or any other issue
            case R.id.action_report:
                startActivity(new Intent(this, Report.class));
                return true;

            //Panel button, redirect to subscribe web-page
            case R.id.action_subscribe:
                startActivity(new Intent(this, Subscribe.class));
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public void sendGetRequest(View v) {
        //EXECUTE JSON
        new GetClass(this).execute();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "DVLAresult Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://com.sandro.openalprsample/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "DVLAresult Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://com.sandro.openalprsample/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }


    private class GetClass extends AsyncTask<String, Void, DVLAInformation> {

        private final Context context;

        public GetClass(Context c) {
            this.context = c;
        }

        protected void onPreExecute() {
            progress = new ProgressDialog(this.context);
            progress.setMessage(getString(R.string.LoadMethod));
            progress.show();
        }

        //[START] Get DVLA information from website database by using url.
        @Override
        protected DVLAInformation doInBackground(String... params) {
            try {

                //URL from which information was retrieved.
                final URL url = new URL("https://dvlasearch.appspot.com/DvlaSearch?licencePlate=" + dvlaPlateNumber + "&apikey=d47OE2WB4nqgPQN6");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                //Set connection
                connection.setRequestMethod("GET");
                connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
                connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");

                BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder responseOutput = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    responseOutput.append(line);
                }
                br.close();

                //Read and parse JSON and responce to string
                DVLAInformation obj = new DVLAInformation(url.toString());
                obj.readAndParseJSON(responseOutput.toString());

                return obj;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;

        }
        //[END] Get DVLA information from website database by using url.




        @Override
        protected void onPostExecute(final DVLAInformation obj) {
            dvlaInfo = obj;

            final TextView outputView = (TextView) findViewById(R.id.showOutput);
            outputView.setText(obj.toString());
            progress.dismiss();

            //[START] Get DVLA information from JSON
            final StringBuilder output = new StringBuilder(obj.getUrl());
            output.append(System.getProperty("line.separator"));
            output.append("\n").append(System.getProperty("line.separator"));
            output.append("Make : ").append(obj.getMake());
            output.append("\nModel : ").append(obj.getModel());
            output.append("\nSix Month Rate  : ").append(obj.getSixMonthRate());
            output.append("\nTwelve Month Rate : ").append(obj.getTwelveMonthRate());
            output.append("\nDate of First Registration : ").append(obj.getDateofFirstRegistrationegistration());
            output.append("\nYear of Manufacture : ").append(obj.getYearOfManufacture());
            output.append("\nCylinder Capacity : ").append(obj.getCylinderCapacity());
            output.append("\nCO2 Emmissions : ").append(obj.getCo2Emissions());
            output.append("\nVIN number : ").append(obj.getVin());
            output.append("\nTransmission type : ").append(obj.getTransmission());
            output.append("\nFuel Type : ").append(obj.getFuelType());
            output.append("\nTax Status : ").append(obj.getTaxStatus());
            output.append("\nColour : ").append(obj.getColour());
            output.append("\nType Approval : ").append(obj.getTypeApproval());
            output.append("\nWheel Plan : ").append(obj.getWheelPlan());
            output.append("\nRevenue Weight : ").append(obj.getRevenueWeight());
            output.append("\nTax Details : ").append(obj.getTaxDetails());
            output.append("\nMOT Details : ").append(obj.getMotDetails());
            output.append("\nTaxed : ").append(obj.getTaxed());
            output.append("\nMOT : ").append(obj.getMot());
            //[END] Get DVLA information from JSON


            //[START] Get GPS Location.
            gps = new GPSTracker(DVLAresult.this);

            latitudeD = gps.getLatitude();
            longitudeD = gps.getLongitude();

            gps.stopUsingGPS();

            Geocoder geoCoder = new Geocoder(getApplicationContext(), Locale.getDefault());
            List<Address> address = null;

            try {
                address = geoCoder.getFromLocation(latitudeD, longitudeD, 1);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            assert address != null;
            if (address.size() > 0) {
                postCode = address.get(0).getPostalCode();
            }
            //[END] Get GPS Location


            //[START] SEND values to database
            requestQueue = Volley.newRequestQueue(getApplicationContext());

            StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {


                @Override
                public void onResponse(String response) {

                    System.out.println(response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {

                // [START] SEND values to database
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {

                    //Get values from Index activity
                    Intent intent = getIntent();
                    //Radio button choice
                    String radioChoice = intent.getStringExtra("radioChosen");

                    //Recognition confidence of the plate
                    String confidence = intent.getStringExtra("confidenceResult");
                    //Unchanged recognised plate number
                    String plateNumberUn = intent.getStringExtra("plateNumberResult");


                    //Google user details (ID, Name, Email)
                    Intent intentG = getIntent();
                    String userIdG = intentG.getStringExtra("userIdGresult");
                    String userNameG = intentG.getStringExtra("userNameGresult");
                    String userEmailG = intentG.getStringExtra("userEmailGresult");


                    //Values which will be used to insert to database
                    Map<String, String> parameters = new HashMap<>();

                    parameters.put("userIdG", userIdG);
                    parameters.put("userNameG", userNameG);
                    parameters.put("userEmailG", userEmailG);

                    parameters.put("latitude", String.valueOf(latitudeD));
                    parameters.put("longitude", String.valueOf(longitudeD));
                    parameters.put("postCode", postCode);

                    parameters.put("doWithCar", radioChoice);
                    parameters.put("plateNumber", dvlaPlateNumber);
                    parameters.put("confidencePlate", confidence);
                    parameters.put("plateNumberUn", plateNumberUn);

                    parameters.put("make", dvlaInfo.getMake());
                    parameters.put("model", dvlaInfo.getModel());

                    parameters.put("twelveMonthRate", dvlaInfo.getTwelveMonthRate());
                    parameters.put("yearOfManufacture", dvlaInfo.getYearOfManufacture());
                    parameters.put("cylinderCapacity", dvlaInfo.getCylinderCapacity());
                    parameters.put("dateofFirstRegistration", dvlaInfo.getDateofFirstRegistrationegistration());
                    parameters.put("co2Emissions", dvlaInfo.getCo2Emissions());
                    parameters.put("fuelType", dvlaInfo.getFuelType());
                    parameters.put("colour", dvlaInfo.getColour());
                    parameters.put("mot", dvlaInfo.getMot());
                    parameters.put("motDetails", dvlaInfo.getMotDetails());
                    parameters.put("vin", dvlaInfo.getVin());
                    parameters.put("transmission", dvlaInfo.getTransmission());

                    parameters.put("sixMonthRate", dvlaInfo.getSixMonthRate());
                    parameters.put("taxStatus", dvlaInfo.getTaxStatus());
                    parameters.put("wheelPlan", dvlaInfo.getWheelPlan());
                    parameters.put("revenueWeight", dvlaInfo.getRevenueWeight());
                    parameters.put("typeApproval", dvlaInfo.getTypeApproval());
                    parameters.put("taxed", dvlaInfo.getTaxed());
                    parameters.put("taxDetails", dvlaInfo.getTaxDetails());

                    return parameters;


                }
            };
            //[END] SEND values to database

            requestQueue.add(request);


            DVLAresult.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    outputView.setText(output);
                    progress.dismiss();
                }
            });


        }
    }
}

I thought about an issue with permissions but android not asking for permission while trying sending information to database.

I have internet permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Error looks like this:

04-25 03:01:25.611 29073-29808/com.sandro.openalprsample E/Volley: [615] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                                       at libcore.net.UriCodec.encode(UriCodec.java:132)
                                                                       at java.net.URLEncoder.encode(URLEncoder.java:57)
                                                                       at com.android.volley.Request.encodeParameters(Request.java:480)
                                                                       at com.android.volley.Request.getBody(Request.java:466)
                                                                       at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:236)
                                                                       at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
                                                                       at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
                                                                       at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
                                                                       at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

I have tried this one , but it do not work:

public  boolean isStoragePermissionGranted() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {
                Log.v(TAG,"Permission is granted");
                return true;
            } else {

                Log.v(TAG,"Permission is revoked");
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                return false;
            }
        }
        else { //permission is automatically granted on sdk<23 upon installation
            Log.v(TAG,"Permission is granted");
            return true;
        }


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
            Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
            //resume tasks needing this permission
        }
    }
  • Hi @fog take reference from here to add permission on runtime http://stackoverflow.com/questions/35973235/android-permission-denial-starting-intent-with-revoked-permission-android-perm/35973323#35973323 – Saveen Apr 25 '16 at 02:11
  • @Saveen Updated ... have a look on the method I have tried –  Apr 25 '16 at 02:24
  • @Fog this is not exception related to permission but there is some problem with encoding string using volly – Saveen Apr 25 '16 at 02:29
  • Please recheck your URL that you are passing – Saveen Apr 25 '16 at 02:31
  • @Saveen It is problem related to permission, because I turn all permission ON and everything works fine –  Apr 25 '16 at 02:31
  • Please look your exception – Saveen Apr 25 '16 at 02:32
  • I need to make permission for Contacts and Location –  Apr 25 '16 at 02:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110089/discussion-between-saveen-and-j-fog). – Saveen Apr 25 '16 at 02:36

1 Answers1

1

Obviously. Your targetSdkVersion is targeting 23 and you need to use runtime permission for android.permission.WRITE_EXTERNAL_STORAGE Android 6.0 include runtime permission feature. Some permissions are considered as dangerous permissions and need to request the at run time. android.permission.WRITE_EXTERNAL_STORAGE is one of them.

You can learn which permissions are dangerous and which permissions are normal from here Normal and Dangerous Permissions.

And you can learn how to use Android Runtime Permission from here: Requesting Permissions at Run Time.

Tony Green
  • 377
  • 1
  • 4
  • 9