2017-07-25 114 views
0

過程:com.example.dell.api,PID:22968 了java.lang.RuntimeException:執行doInBackground() 在機器人發生錯誤。 (FutureTask.java:222) 在java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor。 java的:1 112) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) 引起:java.lang.SecurityException:權限被拒絕(缺少INTERNET權限?) at java.net.InetAddress.lookupHostByName(InetAddress.java:451) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) at java.net.InetAddress.getAllByName(InetAddress。 java:215) at com.android.okhttp.HostResolver $ 1.getAllByName(HostResolver.java:29) at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232) at com.android .okhttp.internal.http.RouteSelector.next(RouteSelector.java:124) 在com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:370) 在com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:298) 在com.android。 okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:399) 在com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:110) 在com.android.okhttp.internal.http。 DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89) at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25) at com.example.dell.api.MainActivity $ JSONTask.doInBackground(MainActivity。 java:147) at com.example.dell.api.MainActivity $ JSONTask.doInBack android.os.AsyncTask $ 2.call(AsyncTask.java:292) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask(MainActivity.java:131) at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587) 在java.lang.Thread.run(Thread.java:818)了java.lang.RuntimeException:發生錯誤而執行doInBackground(


這是我的代碼:

protected List<MovieModel> doInBackground(String... params) { 
     HttpURLConnection connection = null; 
     BufferedReader reader = null; 

     try { 
      URL url = new URL(params[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 
      InputStream stream = connection.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(stream)); 
      StringBuffer buffer = new StringBuffer(); 
      String line = ""; 
      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 
      } 

      String finalJson = buffer.toString(); 

      JSONObject parentObject = new JSONObject(finalJson); 
      JSONArray parentArray = parentObject.getJSONArray("movies"); 

      List<MovieModel> movieModelList = new ArrayList<>(); 

      Gson gson = new Gson(); 
      for (int i = 0; i < parentArray.length(); i++) { 
       JSONObject finalObject = parentArray.getJSONObject(i); 
       /** 
       * below single line of code from Gson saves you from writing the json parsing yourself 
       * which is commented below 
       */ 
       MovieModel movieModel = gson.fromJson(finalObject.toString(), MovieModel.class); // a single line json parsing using Gson 
       movieModel.setMovie(finalObject.getString("movie")); 
       movieModel.setYear(finalObject.getInt("year")); 
       movieModel.setRating((float) finalObject.getDouble("rating")); 
       movieModel.setDirector(finalObject.getString("director")); 

       movieModel.setDuration(finalObject.getString("duration")); 
       movieModel.setTagline(finalObject.getString("tagline")); 
       movieModel.setImage(finalObject.getString("image")); 
       movieModel.setStory(finalObject.getString("story")); 

       List<MovieModel.Cast> castList = new ArrayList<>(); 
       for (int j = 0; j < finalObject.getJSONArray("cast").length(); j++) { 
        MovieModel.Cast cast = new MovieModel.Cast(); 
        cast.setName(finalObject.getJSONArray("cast").getJSONObject(j).getString("name")); 
        castList.add(cast); 
       } 
       movieModel.setCastList(castList); 
       // adding the final object in the list 
       movieModelList.add(movieModel); 
      } 
      return movieModelList; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      if (connection != null) { 
       connection.disconnect(); 
      } 
      try { 
       if (reader != null) { 
        reader.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 
enter code here 
+1

'產生的原因:java.lang.SecurityException異常:(?缺少INTERNET許可)許可被拒絕' – fluffyBatman

回答

1

你忘了INTERNET權限添加到您的清單,在這個

<uses-permission android:name="android.permission.INTERNET" /> 
相關問題