Asynctask错误;从其他活动更改活动时

时间:2014-01-09 09:13:18

标签: android android-asynctask

public class MovieActivity extends ListActivity {

private ProgressDialog pDialog;

// URL to get contacts JSON

private String url = "http://www.kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json?key=165fd244a33a11b50938eacd1fcbe6aa&targetDt=20140107";

Calendar calendar = Calendar.getInstance();
String curY = String.valueOf(calendar.get(Calendar.YEAR));
String curM = String.valueOf(calendar.get(Calendar.MONTH) + 1);
String curD = String.valueOf(calendar.get(Calendar.DATE) - 1);
String day = curY + curM + curD;


// JSON Node names
private static final String TAG_BOXOFFICERESULT = "boxOfficeResult";
private static final String TAG_DAILYBOXOFFICELIST = "dailyBoxOfficeList";
private static final String TAG_RNUM = "rnum";
private static final String TAG_MOVIENM = "movieNm";
private static final String TAG_OPENDT = "openDt";

// contacts JSONArray
JSONArray movies = null;

// Hashmap for ListView
ArrayList<HashMap<String, String> > movieList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d("Response", ">" + "a" );
    setContentView(R.layout.activity_movie);

    Log.d("Response", ">" + "b" );

    movieList = new ArrayList<HashMap<String, String>>();

    Log.d("Response", ">" + "c" );

    // Calling async task to get json
    new GetContacts().execute();

    Log.d("Response", ">" + "d" )

}

/**
 * Async task class to get json by making HTTP call
 * */
private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MovieActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        Log.d("Response", ">" + "1" );

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response", ">" + "2" );


        if (jsonStr != null) {


            try {

                JSONObject jsonObj = new JSONObject(jsonStr);


                JSONObject a = jsonObj.getJSONObject(TAG_BOXOFFICERESULT);


                movies = a.getJSONArray(TAG_DAILYBOXOFFICELIST);

                for (int i=0; i <movies.length(); i++) {

                    JSONObject c  = movies.getJSONObject(i);
                    String rnum = c.getString(TAG_RNUM);
                    String movienm = c.getString(TAG_MOVIENM);
                    String opendt = c.getString(TAG_OPENDT);

                    // tmp hashmap for single contact
                    HashMap<String, String> movie = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    movie.put(TAG_RNUM, rnum);
                    movie.put(TAG_OPENDT, opendt);
                    movie.put(TAG_MOVIENM, movienm);


                    // adding contact to contact list
                    movieList.add(movie);
                }


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

                Log.d("Response: ", "> " + "2b");
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        Log.d("Response", ">" + "2000");
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */

        Log.d("Response", ">" + 1 );
        ListAdapter adapter = new SimpleAdapter(MovieActivity.this, movieList, R.layout.list_item, 
                new String[] { TAG_RNUM, TAG_OPENDT, TAG_MOVIENM }, new int[] { R.id.rnum, R.id.opendt, R.id.movienm });

        Log.d("Response", ">" + R.id.rnum);
        Log.d("Response", ">" + "1000");


        Log.d("Response", ">" + R.id.opendt);
        Log.d("Response", ">" + "2000");
        Log.d("Response", ">" + R.id.movienm);
        Log.d("Response", ">" + "3000");

        setListAdapter(adapter);
    }

}

}

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn1 = (Button) findViewById(R.id.button1);

    Log.e("kjw", "outlistener_main");
    btn1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Log.e("kjw", "inlistener_main");

            Intent i = new Intent(getApplicationContext(), MovieActivity.class);
            startActivity(i);


        }
    });
    //Intent gi = getIntent();
    //if (gi != null)   btn1.setText(gi.getExtras().getString("movie"));
}

}

简单地说,当我想从MovieActivity更改MainActivity时。运行时,

01-09 18:54:16.580: E/AndroidRuntime(30879): FATAL EXCEPTION: AsyncTask #1
01-09 18:54:16.580: E/AndroidRuntime(30879): java.lang.RuntimeException: An error occured while executing doInBackground()
01-09 18:54:16.580: E/AndroidRuntime(30879):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at java.lang.Thread.run(Thread.java:856)
01-09 18:54:16.580: E/AndroidRuntime(30879): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at com.example.test_06.ServiceHandler.makeServiceCall(ServiceHandler.java:71)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at com.example.test_06.ServiceHandler.makeServiceCall(ServiceHandler.java:35)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at com.example.test_06.MovieActivity$GetContacts.doInBackground(MovieActivity.java:117)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at com.example.test_06.MovieActivity$GetContacts.doInBackground(MovieActivity.java:1)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-09 18:54:16.580: E/AndroidRuntime(30879):    ... 5 more
01-09 18:54:16.580: E/AndroidRuntime(30879): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at libcore.io.Posix.getaddrinfo(Native Method)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
01-09 18:54:16.580: E/AndroidRuntime(30879):    at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
01-09 18:54:16.580: E/AndroidRuntime(30879):    ... 20 more
01-09 18:54:16.580: E/AndroidRuntime(30879): Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
01-09 18:54:16.580: E/AndroidRuntime(30879):    ... 23 more
01-09 18:54:16.590: D/dalvikvm(30879): GC_CONCURRENT freed 179K, 6% free 12523K/13255K, paused 13ms+4ms, total 44ms
01-09 18:54:16.590: D/dalvikvm(30879): WAIT_FOR_CONCURRENT_GC blocked 13ms
01-09 18:54:17.735: E/WindowManager(30879): Activity com.example.test_06.MovieActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42058e40 that was originally added here
01-09 18:54:17.735: E/WindowManager(30879): android.view.WindowLeaked: Activity com.example.test_06.MovieActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42058e40 that was originally added here
01-09 18:54:17.735: E/WindowManager(30879):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:407)
01-09 18:54:17.735: E/WindowManager(30879):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
01-09 18:54:17.735: E/WindowManager(30879):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
01-09 18:54:17.735: E/WindowManager(30879):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
01-09 18:54:17.735: E/WindowManager(30879):     at android.view.Window$LocalWindowManager.addView(Window.java:554)
01-09 18:54:17.735: E/WindowManager(30879):     at android.app.Dialog.show(Dialog.java:277)
01-09 18:54:17.735: E/WindowManager(30879):     at com.example.test_06.MovieActivity$GetContacts.onPreExecute(MovieActivity.java:105)
01-09 18:54:17.735: E/WindowManager(30879):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
01-09 18:54:17.735: E/WindowManager(30879):     at android.os.AsyncTask.execute(AsyncTask.java:534)
01-09 18:54:17.735: E/WindowManager(30879):     at com.example.test_06.MovieActivity.onCreate(MovieActivity.java:67)
01-09 18:54:17.735: E/WindowManager(30879):     at android.app.Activity.performCreate(Activity.java:5206)
01-09 18:54:17.735: E/WindowManager(30879):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
01-09 18:54:17.735: E/WindowManager(30879):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
01-09 18:54:17.735: E/WindowManager(30879):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
01-09 18:54:17.735: E/WindowManager(30879):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
01-09 18:54:17.735: E/WindowManager(30879):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
01-09 18:54:17.735: E/WindowManager(30879):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 18:54:17.735: E/WindowManager(30879):     at android.os.Looper.loop(Looper.java:137)
01-09 18:54:17.735: E/WindowManager(30879):     at android.app.ActivityThread.main(ActivityThread.java:4946)
01-09 18:54:17.735: E/WindowManager(30879):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 18:54:17.735: E/WindowManager(30879):     at java.lang.reflect.Method.invoke(Method.java:511)
01-09 18:54:17.735: E/WindowManager(30879):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
01-09 18:54:17.735: E/WindowManager(30879):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
01-09 18:54:17.735: E/WindowManager(30879):     at dalvik.system.NativeStart.main(Native Method)    

发生。

我不知道为什么会这样,因为MovieActivity正在其他项目中运行。帮帮我..!

2 个答案:

答案 0 :(得分:1)

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

您似乎错过了清单中的互联网权限:

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

答案 1 :(得分:0)

您是否在清单文件中添加了网络权限并添加了此权限

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