2016-12-16 188 views
-1

我想製作一個簡單的天氣應用程序。每次我嘗試訪問雅虎天氣api以返回JSON對象時,我都會遇到此異常。爲了不使用api來完成任務,我複製粘貼JSON對象到一個單獨的字符串中,並將該字符串用作JSON對象。Android致命異常異步任務#1

這是我的非同步任務:

公共類WeatherInfoThread擴展的AsyncTask {

@Override 
    protected String doInBackground(Void... params) { 

     String resultString = null; 
     try { 
      url = new URL("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22"+location+"%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 

     try { 
      connection = url.openConnection(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      inputStream = connection.getInputStream(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 

     try { 
      jsonInfo = bufferedReader.readLine(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     resultString = jsonInfo; 



     try { 
      weatherinfo = new JSONObject(testString); 
      // Log.d(JSON_INFO,weatherinfo.toString()+"df"); 
      JSONObject channel = weatherinfo.getJSONObject("query").getJSONObject("results").getJSONObject("channel"); 
      JSONObject item = channel.getJSONObject("item"); 
      currentTemp = item.getJSONObject("condition"); 
      cCurrentInt = currentTemp.getInt("code"); 

      forecast = item.getJSONArray("forecast"); 
      cOne = forecast.getJSONObject(1).getString("text"); 
      cTwo = forecast.getJSONObject(2).getString("text"); 
      cThree = forecast.getJSONObject(3).getString("text"); 
      cFour = forecast.getJSONObject(4).getString("text"); 
      cFive = forecast.getJSONObject(5).getString("text"); 
      dOne = forecast.getJSONObject(1).getString("day"); 
      dTwo = forecast.getJSONObject(2).getString("day"); 
      dThree = forecast.getJSONObject(3).getString("day"); 
      dFour = forecast.getJSONObject(4).getString("day"); 
      dFive = forecast.getJSONObject(5).getString("day"); 


      // Log.d(JSON_INFO,forecast.get(1).toString()+"ddf"); 




     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

這是我得到的錯誤:

12-16 16:48:06.677 10523-10549/com.example.aakashmahesh.weatherapp E/AndroidRuntime:致命異常:的AsyncTask#1 了java.lang.RuntimeException:同時在android.os.Asyn執行doInBackground() 發生錯誤(java.util.concurrent.FutureTask.setException)(FutureTask.java:124) (c)在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307) 在java.util.concurrent.FutureTask.run(FutureTask.java:137) 在android.os.AsyncTask $ SerialExecutor $ 1.run( AsyncTask.java:230) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569) 在java.lang中。 Thread.run(TH read.java:856) 造成的:java.lang.SecurityException異常:權限被拒絕(缺少INTERNET權限) 在java.net.InetAddress.lookupHostByName(InetAddress.java:418) 在java.net.InetAddress.getAllByNameImpl(? (InetAddress.java:236) at libcore.net.http.HttpConnection。(HttpConnection.java:70) at libcore.net.http.HttpConnection。( HttpConnection.java:50) at libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340) at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 在libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 在libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) 在libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.makeSslConnection( HttpsURLConnectionImpl.java:461) 在libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.connect(HttpsURLConnectionImpl.java:433) 在libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 在libcore.net。 http.HttpEngine.sendRequest(HttpEngine.java:239) 在libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 在libcore.net.htt p.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 在libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271) 在com.example.aakashmahesh.weatherapp.MainActivity $ WeatherInfoThread.doInBackground(MainActivity.java: 269) at com.example.aakashmahesh.weatherapp.MainActivity $ WeatherInfoThread.doInBackground(MainActivity。java:250) at android.os.AsyncTask $ 2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305) at java.util.concurrent.FutureTask .run(FutureTask.java:137) at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java。 util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:856) 原因:libcore.io.GaiException:getaddrinfo失敗:EAI_NODATA(無地址關聯與h ostname) 在libcore.io.Posix.getaddrinfo(本機方法) 在libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55) 在java.net.InetAddress.lookupHostByName(InetAddress.java:405) 在java的.net.InetAddress.getAllByNameImpl(InetAddress.java:236) at libcore.net.http.HttpConnection。(HttpConnection.java:70) libcore上的java.net.InetAddress.getAllByName(InetAddress.java:214) 。 net.http.HttpConnection。(HttpConnection.java:50) at libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340) at lib core.http.HttpConnectionPool.get(HttpConnectionPool.java:87) at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java: 315) 在libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461) 在libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.connect(HttpsURLConnectionImpl.java:433) 在libcore.net.http。 HttpEngine.sendSocketRequest(HttpEngine.java:289) at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConne ctionImpl.java:273) 在libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 在libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271) 在com.example.aakashmahesh。 weatherapp.MainActivity $ WeatherInfoThread.doInBackground(MainActivity.java:269) at com.example.aakashmahesh.weatherapp.MainActivity $ WeatherInfoThread.doInBackground(MainActivity.java:250) at android.os.AsyncTask $ 2.call(AsyncTask.java :287) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at android.os.AsyncTask $ Serial.Explorer $ 1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:856) 原因:libcore.io.ErrnoException:getaddrinfo失敗:EACCES(權限被拒絕) at libcore.io.Posix.getaddrinfo (Native Method) at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55) at java.net.InetAddress.lookupHostByName(InetAddress.java:405) 在java.net。InetAddress.getAllByNameImpl(InetAddress.java:236) at java.net.InetAddress.getAllByName(InetAddress.java:214) at libcore.net.http.HttpConnection。(HttpConnection.java:70) at libcore.net.http .HttpConnection。(HttpConnection.java:50) at libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340) at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) at libcore.net.http.HttpsURLConnec tionImpl $ HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461) 在libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.connect(HttpsURLConnectionImpl.java:433) 在libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 在libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 在libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 在libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl。 java:168) at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271) at com.example.aakashmahesh.weatherapp.M ainActivity $ WeatherInfoThread.doInBackground(MainActivity.java:269) at com.example.aakashmahesh.weatherapp.MainActivity $ WeatherInfoThread.doInBackground(MainActivity.java:250) at android.os.AsyncTask $ 2.call(AsyncTask.java:287 ) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at android.os.AsyncTask $ SerialExecutor $ 1。運行(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:856) 12-16 16:48:06.677 1370-17136/system_process W/ActivityManager:強制結束活動com.example.aakashmahesh.weatherapp/.MainActivity 12- 16 16:48:06.737 1370-17136/system_process D/dalvikvm:GC_FOR_ALLOC釋放647K,12%空閒16559K/18695K,暫停10ms,總共10毫秒 12-16 16:48:06.767 10523-10523/com.example.aakashmahesh。 weatherapp D/libEGL:loaded /system/lib/egl/libEGL_emulation.so 12-16 16:48:06.767 10523-10523/com.example.aakashmahesh.weatherapp D/libEGL:loaded/system/lib/egl/libGLESv1_CM_emulation。 so 12-16 16:48:06.767 10523-10523/com.example.aakashmahesh.weatherapp D/libEGL:loaded /system/lib/egl/libGLESv2_emulation.so

                  [ 12-16 16:48:06.767 10523:10523 D/   ] 
                     HostConnection::get() New Host Connection established 0xb8071030, tid 10523 
+0

確定有更多的日誌。您發佈的異常缺少發生異常的行。請發佈完整的相關日誌 – zon7

+0

是的,堆棧跟蹤應該告訴你它正在發生的行#。 – TWL

回答

1

閱讀錯誤,你會嗎?

造成的:java.lang.SecurityException異常:權限在

1

否認(?缺少INTERNET權限)可能是你沒有寫在清單文件訪問網絡權限

所致:JAVA .lang.SecurityException:權限被拒絕(缺少INTERNET權限?)

+0

謝謝。我沒有在Manifest文件中寫入訪問Internet的權限。我被難倒了。不會忘記這個答案。謝謝。 –