Eclipse - 执行doInBackground()时发生错误

时间:2015-04-13 15:04:18

标签: android eclipse background

我有一个简单的应用程序。只是为了连接服务器并获得结果。但是当我运行它时,我得到了这样的信息:

04-13 11:55:41.935: E/AndroidRuntime(16005): FATAL EXCEPTION: AsyncTask #1
04-13 11:55:41.935: E/AndroidRuntime(16005): Process: com.tumta.consultatempo, PID: 16005
04-13 11:55:41.935: E/AndroidRuntime(16005): java.lang.RuntimeException: An error occured while executing doInBackground()

我的班级:

package com.tumta.consultatempo;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;

public class ConsultaSituacaoTempo extends AsyncTask<Void, Void, String> {

    private ConsultaConcluidaListener listener;

    private static final String URL_STRING = "http://api.openweathermap.org/data/2.5/weather?q=Sorocaba,sp";

    public ConsultaSituacaoTempo(ConsultaConcluidaListener listener){
        this.listener = listener;
    }


    @Override
    protected String doInBackground(Void... arg0) {
        try {
            String resultado = ConsultaServidor();
            return InterpretaResultado(resultado);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    private String InterpretaResultado(String resultado) throws JSONException {
        JSONObject object = new JSONObject(resultado);
        JSONArray jsonArray = object.getJSONArray("weather");
        JSONObject jsonWaether = jsonArray.getJSONObject(0);
        int id = jsonWaether.getInt("id");
        String descricao = jsonWaether.getString("description");

        return "Situacao do tempo em Sorocaba: " + id + " - " + descricao;
    }


    private String ConsultaServidor() throws IOException {
        InputStream is = null;

        try{
            URL url = new URL(URL_STRING);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            conn.getResponseCode();

            is = conn.getInputStream();

            Reader reader = null;
            reader = new InputStreamReader(is);
            char[] buffer = new char[2048];
            reader.read(buffer);
            return new String(buffer);
        }finally{
            if(is != null){
                is.close();
            }
        }
    }


    @Override
    protected void onPostExecute(String result) {
        listener.onConsultaConcluida(result);
        super.onPostExecute(result);
    }

    public interface ConsultaConcluidaListener{
        void onConsultaConcluida(String situacaoTempo);
    }
}

我不知道为什么我会得到这个例外。有谁知道什么是错的? Thank`s。

***** EDIT ***** 这是我的完整LogCat:

04-13 15:41:22.149: E/AndroidRuntime(16547): FATAL EXCEPTION: AsyncTask #1
04-13 15:41:22.149: E/AndroidRuntime(16547): Process: com.tumta.consultatempo, PID: 16547
04-13 15:41:22.149: E/AndroidRuntime(16547): java.lang.RuntimeException: An error occured while executing doInBackground()
04-13 15:41:22.149: E/AndroidRuntime(16547):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at java.lang.Thread.run(Thread.java:818)
04-13 15:41:22.149: E/AndroidRuntime(16547): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at java.net.InetAddress.getAllByName(InetAddress.java:215)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at com.tumta.consultatempo.ConsultaSituacaoTempo.ConsultaServidor(ConsultaSituacaoTempo.java:63)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at com.tumta.consultatempo.ConsultaSituacaoTempo.doInBackground(ConsultaSituacaoTempo.java:30)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at com.tumta.consultatempo.ConsultaSituacaoTempo.doInBackground(ConsultaSituacaoTempo.java:1)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-13 15:41:22.149: E/AndroidRuntime(16547):    ... 4 more
04-13 15:41:22.149: E/AndroidRuntime(16547): Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at libcore.io.Posix.android_getaddrinfo(Native Method)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
04-13 15:41:22.149: E/AndroidRuntime(16547):    at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
04-13 15:41:22.149: E/AndroidRuntime(16547):    ... 18 more
04-13 15:41:22.149: E/AndroidRuntime(16547): Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
04-13 15:41:22.149: E/AndroidRuntime(16547):    ... 21 more

1 个答案:

答案 0 :(得分:0)

  

引起:java.lang.SecurityException:Permission denied(缺少INTERNET权限?)

INTERNET权限添加到清单文件中。

你必须添加这一行:

<uses-permission android:name="android.permission.INTERNET" /> 

在AndroidManifest.xml中的应用程序标记之外