2016-04-25 199 views
0

我與Android 6.0 = PERMISSION_NOT_GRANTED的Android 6.0 = PERMISSION_NOT_GRANTED(問題與聯繫人和位置的權限)

附:問題我發現問題與聯繫人和位置權限,是否有人知道如何解決它?

我可以從JSON獲取數據,但由於PERMISSION_NOT_GRANTED異常,我無法將信息發送到數據庫。

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


     } 
    } 
} 

我想過一個關於權限的問題,但是android在嘗試向數據庫發送信息時沒有要求權限。

我有上網權限:

<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" /> 

錯誤看起來是這樣的:

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) 

我已經試過這一個,但它不工作:

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 
     } 
    } 
+0

Hi @fog從這裏引用添加在運行時的權限http://stackoverflow.com/questions/35973235/android-permission-denial-starting-intent-with-revoked-permission-android-perm/35973323#35973323 – Saveen

+0

@Saveen更新...看看我試過的方法 –

+0

@Fog這不是例外與權限有關,但使用volly編碼字符串有一些問題 – Saveen

回答

1

明顯。您的targetSdkVersion定位爲23,您需要使用運行時權限android.permission.WRITE_EXTERNAL_STORAGE Android 6.0包含運行時權限功能。某些權限被視爲危險權限,需要在運行時請求。 android.permission.WRITE_EXTERNAL_STORAGE就是其中之一。

您可以從這裏瞭解哪些權限是危險的以及哪些權限是正常的Normal and Dangerous Permissions

您可以從這裏瞭解如何使用Android運行時權限:Requesting Permissions at Run Time

+0

更新...看看我試過的方法 –