在同一个活动中调用Twoo AsyncTask

时间:2015-01-30 21:41:41

标签: android android-asynctask

我正在使用AsyncTask来救出服务器网址中的json数据。我不知道为什么,但有时工作正常,其他人的应用程序完成错误。这是我的日志猫的错误:

01-30 22:28:00.998: E/AndroidRuntime(3404): FATAL EXCEPTION: AsyncTask #2
01-30 22:28:00.998: E/AndroidRuntime(3404): Process: my.package.com, PID: 3404
01-30 22:28:00.998: E/AndroidRuntime(3404): java.lang.RuntimeException: An error occured while executing doInBackground()
01-30 22:28:00.998: E/AndroidRuntime(3404):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
01-30 22:28:00.998: E/AndroidRuntime(3404):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-30 22:28:00.998: E/AndroidRuntime(3404):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-30 22:28:00.998: E/AndroidRuntime(3404):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-30 22:28:00.998: E/AndroidRuntime(3404):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-30 22:28:00.998: E/AndroidRuntime(3404):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-30 22:28:00.998: E/AndroidRuntime(3404):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-30 22:28:00.998: E/AndroidRuntime(3404):     at java.lang.Thread.run(Thread.java:841)
01-30 22:28:00.998: E/AndroidRuntime(3404): Caused by: java.lang.NullPointerException
01-30 22:28:00.998: E/AndroidRuntime(3404):     at my.package.com.ProgramacionActivity$DownloadJSONdos.doInBackground(ProgramacionActivity.java:342)
01-30 22:28:00.998: E/AndroidRuntime(3404):     at my.package.com.ProgramacionActivity$DownloadJSONdos.doInBackground(ProgramacionActivity.java:1)
01-30 22:28:00.998: E/AndroidRuntime(3404):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-30 22:28:00.998: E/AndroidRuntime(3404):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-30 22:28:00.998: E/AndroidRuntime(3404):     ... 4 more
01-30 22:28:01.188: E/log_tag(3404): Error parsing data org.json.JSONException: End of input at character 0 of 

有人可以告诉我为什么这些应用程序有时可以正常运行而其他时候运行不好?在我的活动中,我有两个AsyncTask,我称之为:

new DownloadJSONdos().execute();
   new DownloadJSON().execute();

我的活动代码:

public class ProgramacionActivity extends Fragment implements
AdapterView.OnItemSelectedListener{

    ProgressDialog mProgressDialog;
    private ViewPager mViewPager;
    String idioma;
    LinearLayout linear_marcador;

    String results;
    String jsonStr;

    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    LazyAdapterHorariosLiga adapter;

    ArrayList<HashMap<String, String>> arraylist;
    ArrayList<String> elementos_marcador;
    ArrayList<Proximo_Partido> elementos_proximos;

     JSONArray eventos = null;
    // Hashmap for ListView
    ArrayList<HashMap<String, String>> eventosList;

    ImageLoader imageLoader;

    static final String TAG_ICONO_LOCAL= "icono_local";
    static final String TAG_EQUIPO_LOCAL= "equipo_local";
    static final String TAG_GOLES_LOCAL= "goles_local";
    static final String TAG_ICONO_VISITANTE= "icono_visitante";
    static final String TAG_EQUIPO_VISITANTE= "equipo_visitante";
    static final String TAG_GOLES_VISITANTE= "goles_visitante";
    static final String TAG_ESTADO= "estado";
    static final String TAG_ESTADIO= "estadio";
    static final String TAG_COMPETICION= "competicion";
    TextView equipo_local, equipo_visitante, goles_local, goles_visitante, estado, estadio, competicion;
    int i;

    TextView competi_portada_uno, competi_portada_dos, competi_portada_tres, fecha1, fecha2, fecha3;
    ImageView icono_local1, icono_local2, icono_local3,icono_visitante1, icono_visitante2, icono_visitante3;


    ImageView icono_local, icono_visitante;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v= inflater.inflate(R.layout.programacion_main, container, false);

         linear_marcador = (LinearLayout) v.findViewById(R.id.linear_marcador);
         animate(linear_marcador).alpha(0).setDuration(0);


         mViewPager = (ViewPager) v.findViewById(R.id.viewpager);
         PagerTabStrip pagerTabStrip = (PagerTabStrip) v.findViewById(R.id.pagerTabStrip);
         pagerTabStrip.setTabIndicatorColor(getResources().getColor(R.color.blanco));

         equipo_local=(TextView) v.findViewById(R.id.eq_local);
         equipo_visitante=(TextView) v.findViewById(R.id.eq_visitante);
         goles_local=(TextView) v.findViewById(R.id.gol_local);
         equipo_visitante=(TextView) v.findViewById(R.id.eq_visitante);
         goles_visitante=(TextView) v.findViewById(R.id.gol_visitante);
         estado=(TextView) v.findViewById(R.id.estado_parti);
         estadio=(TextView) v.findViewById(R.id.estadiaco);
         competicion=(TextView) v.findViewById(R.id.competicionaca);
         icono_local=(ImageView) v.findViewById(R.id.icono_local);
         icono_visitante=(ImageView) v.findViewById(R.id.icono_visitante);


         icono_local1=(ImageView) v.findViewById(R.id.icono_local1);
         icono_visitante1=(ImageView) v.findViewById(R.id.icono_visitante1);
         icono_local2=(ImageView) v.findViewById(R.id.icono_local2);
         icono_visitante2=(ImageView) v.findViewById(R.id.icono_visitante2);
         icono_local3=(ImageView) v.findViewById(R.id.icono_local3);
         icono_visitante3=(ImageView) v.findViewById(R.id.icono_visitante3);

         competi_portada_uno=(TextView) v.findViewById(R.id.competi_portada1);
         competi_portada_dos=(TextView) v.findViewById(R.id.competi_portada2);
         competi_portada_tres=(TextView) v.findViewById(R.id.competi_portada3);
         fecha1=(TextView) v.findViewById(R.id.fecha1);
         fecha2=(TextView) v.findViewById(R.id.fecha2);
         fecha3=(TextView) v.findViewById(R.id.fecha3);

          Typeface font = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Roboto-Thin.ttf");
          Typeface font_dos = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Medium.ttf");

          equipo_local.setTypeface(font_dos);
          equipo_visitante.setTypeface(font_dos);
          estadio.setTypeface(font_dos);
          competicion.setTypeface(font_dos);
          goles_local.setTypeface(font);
          goles_visitante.setTypeface(font);
          competi_portada_uno.setTypeface(font_dos);
          competi_portada_dos.setTypeface(font_dos);
          competi_portada_tres.setTypeface(font_dos);
          fecha1.setTypeface(font_dos);
          fecha2.setTypeface(font_dos);
          fecha3.setTypeface(font_dos);

         new java.util.Timer().schedule( 
                    new java.util.TimerTask() {
                        @Override
                        public void run() {
                             animate(linear_marcador).alpha(1).setDuration(1200);

                        }
                    }, 
                    2500 
            );


         TitleAdapter titleAdapter = new TitleAdapter(getActivity().getSupportFragmentManager());
         mViewPager.setAdapter(titleAdapter);


           mViewPager.setCurrentItem(0);

            mFlipper = ((ViewFlipper) v.findViewById(R.id.flipper));
            mFlipper.startFlipping();

            Spinner s = (Spinner) v.findViewById(R.id.spinner);
            ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(),
                    android.R.layout.simple_spinner_item, mStrings);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s.setAdapter(adapter);
            s.setOnItemSelectedListener(this);


            linear_marcador.setOnClickListener(new View.OnClickListener() {
                 @Override
                    public void onClick(View v) {

                     Intent intenttresss = new Intent(getActivity(), Marcador.class);
                     //intenttresss.putExtra("idioma", idioma);
                     startActivity(intenttresss);




                    }
             });

            imageLoader = new ImageLoader(getActivity());

            if ((new UtilsDos(getActivity()).isConnected())){
             new DownloadJSONdos().execute();
             new DownloadJSON().execute();

            }

         return v;

        }





    public void onItemSelected(AdapterView parent, View v, int position, long id) {
        switch (position) {

        case 0:
            mFlipper.setInAnimation(AnimationUtils.loadAnimation(getActivity(),
                     android.R.anim.fade_in));
            mFlipper.setOutAnimation(AnimationUtils.loadAnimation(getActivity(),
                    android.R.anim.fade_out));

            break;
        case 1:
            mFlipper.setInAnimation(AnimationUtils.loadAnimation(getActivity(),
                     android.R.anim.fade_in));
            mFlipper.setOutAnimation(AnimationUtils.loadAnimation(getActivity(),
                    android.R.anim.fade_out));

            break;
        case 2:
            mFlipper.setInAnimation(AnimationUtils.loadAnimation(getActivity(),
                    android.R.anim.fade_in));
            mFlipper.setOutAnimation(AnimationUtils.loadAnimation(getActivity(),
                    android.R.anim.fade_out));
            break;
        default:
            mFlipper.setInAnimation(AnimationUtils.loadAnimation(getActivity(),
                     android.R.anim.fade_in));
            mFlipper.setOutAnimation(AnimationUtils.loadAnimation(getActivity(),
                    android.R.anim.fade_out));

            break;
        }
    }

    public void onNothingSelected(AdapterView parent) {
    }

    private String[] mStrings = {
            "Push up", "Push left", "Cross fade", "Hyperspace"};

    private ViewFlipper mFlipper;



 // DownloadJSON AsyncTask
            private class DownloadJSON extends AsyncTask<Void, Void, Void> {

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    // Create a progressdialog
                    mProgressDialog = new ProgressDialog(getActivity());
                    // Set progressdialog title
                    //mProgressDialog.setTitle("Android JSON Parse Tutorial");
                    // Set progressdialog message
                    mProgressDialog.setMessage("Actualizando...");
                    mProgressDialog.setIndeterminate(false);
                    // Show progressdialog
                    mProgressDialog.show();
                }

                @Override
                protected Void doInBackground(Void... params) {
                    // Create an array
                    arraylist = new ArrayList<HashMap<String, String>>();
                    elementos_marcador =new ArrayList<String>();

                    // Retrieve JSON Objects from the given URL address
                    jsonobject = JSONfunctions
                            .getJSONfromURL("myurl");

                    try {
                        // Locate the array name in JSON
                        jsonarray = jsonobject.getJSONArray("results");

                        for (int i = 0; i < jsonarray.length(); i++) {
                            HashMap<String, String> map = new HashMap<String, String>();
                            jsonobject = jsonarray.getJSONObject(i);
                            // Retrive JSON Objects


                            elementos_marcador.add( jsonobject.getString("icono_local"));
                            elementos_marcador.add( jsonobject.getString("equipo_local"));
                            elementos_marcador.add( jsonobject.getString("goles_local"));
                            elementos_marcador.add( jsonobject.getString("icono_visitante"));
                            elementos_marcador.add( jsonobject.getString("equipo_visitante"));
                            elementos_marcador.add( jsonobject.getString("goles_visitante"));
                            elementos_marcador.add( jsonobject.getString("estado"));
                            elementos_marcador.add( jsonobject.getString("estadio"));
                            elementos_marcador.add(jsonobject.getString("competicion"));

                            arraylist.add(map);
                        }
                    } catch (JSONException e) {
                        Log.e("Error", e.getMessage());
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void args) {
                    // Locate the listview in listview_main.xml

                     equipo_local.setText(elementos_marcador.get(1));
                     equipo_visitante.setText(elementos_marcador.get(4));
                     goles_local.setText(elementos_marcador.get(2));
                     goles_visitante.setText(elementos_marcador.get(5));
                     estado.setText(elementos_marcador.get(6));
                     estadio.setText(elementos_marcador.get(7));
                     competicion.setText(elementos_marcador.get(8));
                     imageLoader.DisplayImage(elementos_marcador.get(0), icono_local);
                     imageLoader.DisplayImage(elementos_marcador.get(3), icono_visitante);
                     mProgressDialog.dismiss();


                }
            }





        // DownloadJSON AsyncTask
            private class DownloadJSONdos extends AsyncTask<Void, Void, Void> {

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    // Create a progressdialog
                    mProgressDialog = new ProgressDialog(getActivity());
                    // Set progressdialog title
                    //mProgressDialog.setTitle("Android JSON Parse Tutorial");
                    // Set progressdialog message
                    mProgressDialog.setMessage("Actualizando...");
                    mProgressDialog.setIndeterminate(false);
                    // Show progressdialog
                    //mProgressDialog.show();
                }

                @Override
                protected Void doInBackground(Void... params) {
                    // Create an array
                    arraylist = new ArrayList<HashMap<String, String>>();
                    elementos_proximos =new ArrayList<Proximo_Partido>();
                    // Retrieve JSON Objects from the given URL address
                    jsonobject = JSONfunctions
                            .getJSONfromURL("myurl");

                    try {
                        // Locate the array name in JSON
                        jsonarray = jsonobject.getJSONArray("results");

                        for (int i = 0; i < jsonarray.length(); i++) {
                            Proximo_Partido mi_parti = new Proximo_Partido();
                            jsonobject = jsonarray.getJSONObject(i);
                            // Retrive JSON Objects

                            mi_parti.setCompeticion( jsonobject.getString("competicion"));
                            mi_parti.setIcono_local( jsonobject.getString("icono_local"));
                            mi_parti.setIcono_visitante( jsonobject.getString("icono_visitante"));
                            mi_parti.setHorario( jsonobject.getString("fecha_hora"));


                            elementos_proximos.add(mi_parti);
                        }
                    } catch (JSONException e) {
                        Log.e("Error", e.getMessage());
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void args) {


                     competi_portada_uno.setText(elementos_proximos.get(0).getCompeticion());
                     competi_portada_dos.setText(elementos_proximos.get(1).getCompeticion());
                     competi_portada_tres.setText(elementos_proximos.get(2).getCompeticion());
                     fecha1.setText(elementos_proximos.get(0).getHorario());
                     fecha2.setText(elementos_proximos.get(1).getHorario());
                     fecha3.setText(elementos_proximos.get(2).getHorario());
                     imageLoader.DisplayImage(elementos_proximos.get(0).getIcono_local(), icono_local1);
                     imageLoader.DisplayImage(elementos_proximos.get(1).getIcono_local(), icono_local2);
                     imageLoader.DisplayImage(elementos_proximos.get(2).getIcono_local(), icono_local3);
                     imageLoader.DisplayImage(elementos_proximos.get(0).getIcono_visitante(), icono_visitante1);
                     imageLoader.DisplayImage(elementos_proximos.get(1).getIcono_visitante(), icono_visitante2);
                     imageLoader.DisplayImage(elementos_proximos.get(2).getIcono_visitante(), icono_visitante3);


                     mProgressDialog.dismiss();


                }
            }

2 个答案:

答案 0 :(得分:0)

添加

if(jsonobject == null)
    return null;

jsonarray = jsonobject.getJSONArray("results");

doInBackground()

AsyncTask方法中的

然后在第一个postExecute()的{​​{1}}方法中,将其添加到顶部

AsyncTask

在第二个if(elementos_marcador == null || elementos_marcador.size() == 0) return; 的{​​{1}}方法中将此添加到顶部

postExecute()

注意:这会阻止您的应用崩溃。但是你仍然要弄清楚为什么json有时会腐败。

答案 1 :(得分:-1)

我认为通过Web服务类实现AsyncTask是一种更好的方法。请参阅下文,了解我在项目中的表现。

<强> CODE:

public class WebServiceRestTask extends AsyncTask<HttpUriRequest, Void, Object> {
    private static final String TAG = "WebServiceRestTask";
    private AbstractHttpClient mClient;
    private WeakReference<WebServiceRestCallback> mCallback;
    private int ws_task;

    public WebServiceRestTask(int ws_task) {
        this(new DefaultHttpClient(), ws_task);

    }

    public WebServiceRestTask(AbstractHttpClient client, int task_number) {
        mClient = client;
        this.ws_task = task_number;
    }

    public interface WebServiceRestCallback {
        public void onRequestSuccess(String response);

        public void onRequestError(Exception error);
    }

    public void setResponseCallback(WebServiceRestCallback callback) {
        mCallback = new WeakReference<WebServiceRestCallback>(callback);
    }

    @Override
    protected Object doInBackground(HttpUriRequest... params) {
        try {
            HttpUriRequest request = params[0];
            HttpResponse serverResponse = mClient.execute(request);

            BasicResponseHandler handler = new BasicResponseHandler();
            String response = handler.handleResponse(serverResponse);
            return response + ws_task;
        } catch (Exception e) {
            Log.w(TAG, e);
            return e;
        }
    }

    @Override
    protected void onPostExecute(Object result) {
        if (mCallback != null && mCallback.get() != null) {
            if (result instanceof String) {
                mCallback.get().onRequestSuccess((String) result);
            } else if (result instanceof Exception) {
                mCallback.get().onRequestError((Exception) result);
            } else {
                mCallback.get().onRequestError(
                        new IOException("Unknown Error Contacting Host"));
            }
        }
    }

}