执行doInBackground()时致命异常Asynctask#1

时间:2014-04-20 20:30:52

标签: android android-asynctask

我正在尝试将片段中的web服务方法调用到Android应用程序。但我得到致命异常Asynctask#1,java.lang.RuntimeException: An error occured while executing doInBackground() error. 这是我的代码:

public class HomeFragment extends Fragment   {
    View v;
    JSONArray android=null;
    ListView list;
    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
    TextView id;
    TextView name;
    TextView price;
    ConnectionTask mConTask = null;
    ProgressDialog progressDialog = null;
    JsonCreator jsonCreator = new JsonCreator();
    private static String url = "http://api.learn2crack.com/android/jsonos/";
    private static final String TAG_OS = "android";
    private static final String TAG_VER = "ver";
    private static final String TAG_NAME = "name";
    private static final String TAG_API = "api";      

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        oslist = new ArrayList<HashMap<String, String>>();
         new ConnectionTask().execute();
    }

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {   
            v = inflater.inflate(R.layout.fragment_home, container, false);
            return v;
    }

    public class ConnectionTask extends AsyncTask<String, String, JSONObject>{

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
             id = (TextView)v.findViewById(R.id.id);
             name = (TextView)v.findViewById(R.id.name);
             price = (TextView)v.findViewById(R.id.price);
                progressDialog = ProgressDialog.show(getActivity(), "", "Yükleniyor... ",true ,true);

        }

        @Override
        protected JSONObject doInBackground(String... params) {
            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.getJSONFromUrl(url);
            return json;
        }

        @Override
        protected void onPostExecute( JSONObject json) {

            if(json == null){

                //İnternet kontrolü
                if(!Constants.isInternetAvailable(getActivity())){
                    progressDialog.dismiss();
                    Toast toast = Toast.makeText(getActivity(), "Lütfen internet bağlantınızı kontrol ediniz.", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
                //İnternet var ise veri boş
                else{
                    progressDialog.dismiss();
                    Toast toast = Toast.makeText(getActivity(), "Hiçbir veri bulunmamaktadır.", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                } 
            }
            else{
                    try {
                    // Getting JSON Array from URL
                    android = json.getJSONArray(TAG_OS);
                    for(int i = 0; i < android.length(); i++){
                    JSONObject c = android.getJSONObject(i);
                    String ver = c.getString(TAG_VER);
                    String name = c.getString(TAG_NAME);
                    String api = c.getString(TAG_API);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_VER, ver);
                    map.put(TAG_NAME, name);
                    map.put(TAG_API, api);
                    oslist.add(map);
                    list=(ListView)v.findViewById(R.id.listView1);
                    ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                            R.layout.list_v,
                            new String[] { TAG_VER,TAG_NAME, TAG_API }, new int[] {
                                    R.id.id,R.id.name, R.id.price});

                    list.setAdapter(adapter);   
                    }
                }
                catch (JSONException e) {
                    e.printStackTrace();
                }

        }
        }
        @Override
        protected void onCancelled() {
            mConTask = null;
            progressDialog.dismiss();
        }

    }
}

logcat输出:

 04-20 20:14:49.492: E/AndroidRuntime(2144): FATAL EXCEPTION: AsyncTask #1
    04-20 20:14:49.492: E/AndroidRuntime(2144): Process: info.androidhive.slidingmenu, PID: 2144
    04-20 20:14:49.492: E/AndroidRuntime(2144): java.lang.RuntimeException: An error occured while executing doInBackground()
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at java.lang.Thread.run(Thread.java:841)
    04-20 20:14:49.492: E/AndroidRuntime(2144): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at info.androidhive.slidingmenu.jsonlib.JSONParser.getJSONFromUrl(JSONParser.java:38)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at info.androidhive.slidingmenu.HomeFragment$ConnectionTask.doInBackground(HomeFragment.java:84)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at info.androidhive.slidingmenu.HomeFragment$ConnectionTask.doInBackground(HomeFragment.java:1)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     ... 4 more
    04-20 20:14:49.492: E/AndroidRuntime(2144): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at libcore.io.Posix.getaddrinfo(Native Method)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     ... 18 more
    04-20 20:14:49.492: E/AndroidRuntime(2144): Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
    04-20 20:14:49.492: E/AndroidRuntime(2144):     ... 21 more
    04-20 20:14:49.732: E/OpenGLRenderer(2144): Getting MAX_TEXTURE_SIZE from GradienCache
    04-20 20:14:49.732: E/OpenGLRenderer(2144): MAX_TEXTURE_SIZE: 16384
    04-20 20:14:49.744: E/OpenGLRenderer(2144): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
    04-20 20:14:49.752: E/OpenGLRenderer(2144): MAX_TEXTURE_SIZE: 16384
    04-20 20:14:51.684: E/WindowManager(2144): android.view.WindowLeaked: Activity info.androidhive.slidingmenu.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52845490 V.E..... R......D 0,0-1026,288} that was originally added here
    04-20 20:14:51.684: E/WindowManager(2144):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.Dialog.show(Dialog.java:286)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.ProgressDialog.show(ProgressDialog.java:116)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.ProgressDialog.show(ProgressDialog.java:104)
    04-20 20:14:51.684: E/WindowManager(2144):  at info.androidhive.slidingmenu.HomeFragment$ConnectionTask.onPreExecute(HomeFragment.java:77)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.os.AsyncTask.execute(AsyncTask.java:535)
    04-20 20:14:51.684: E/WindowManager(2144):  at info.androidhive.slidingmenu.HomeFragment.onActivityCreated(HomeFragment.java:53)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.Fragment.performActivityCreated(Fragment.java:1708)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.BackStackRecord.run(BackStackRecord.java:684)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.Activity.performStart(Activity.java:5240)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.os.Handler.dispatchMessage(Handler.java:102)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.os.Looper.loop(Looper.java:136)
    04-20 20:14:51.684: E/WindowManager(2144):  at android.app.ActivityThread.main(ActivityThread.java:5017)
    04-20 20:14:51.684: E/WindowManager(2144):  at java.lang.reflect.Method.invokeNative(Native Method)
    04-20 20:14:51.684: E/WindowManager(2144):  at java.lang.reflect.Method.invoke(Method.java:515)
    04-20 20:14:51.684: E/WindowManager(2144):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    04-20 20:14:51.684: E/WindowManager(2144):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    04-20 20:14:51.684: E/WindowManager(2144):  at dalvik.system.NativeStart.main(Native Method)

如何解决?感谢...

1 个答案:

答案 0 :(得分:1)

logcat清楚地说道;

Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)

在manifest.xml文件中添加以下行

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