2016-12-16 184 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的权限。我被难倒了。不会忘记这个答案。谢谢。 –