AsyncTask返回不会传递给其他方法

时间:2012-10-13 18:44:04

标签: android android-asynctask parameter-passing

我已经研究了许多这方面的问题和例子,但未能理解并解决问题。问题是我需要从我的AsyncTask类中的InBackground方法获取String数组到另一个类,其中一些进程发生在数据中。

这是我的myMapService类:

public class myMapService extends AsyncTask<String, Void, String[]> {
    private ProgressDialog progressDialog;
    private keyword activity;

    private String rst = " " ; 

    private String[] strArr1;
    private String[] strArr2;
    private String[] resultList;

    public String[] getResultList() { return this.resultList; }


    public myMapService(keyword activity, ProgressDialog progressDialog)
    {
        this.activity = activity;
        this.progressDialog = progressDialog;
    }



    @Override
    protected void onPreExecute()
    {
        progressDialog.show();
    }


    @Override
    protected String[] doInBackground(String... arg0) {


        String result = "";
        int responseCode = 0;

        int executeCount = 0;
        HttpResponse response;

        StringBuilder sb = new StringBuilder();
        String line;
        try 
        {
            HttpClient client = new DefaultHttpClient();
            HttpGet httppost = new HttpGet("http://XXX.com/ccvo/mel-asset-data/query.php?lon="+ arg0[0].toString() + "&lat="+ arg0[1].toString() +"&within=" + arg0[2].toString()  + "&keyword="+ arg0[3].toString().replace(" ", "%20"));


            Log.v("Results", "from web: " + arg0[0]);
            Log.v("Results", "from web: " + arg0[1]);
            Log.v("Results", "from web: " + arg0[2]);
            Log.v("Results", "from web: " + arg0[3]);


                do
                {
                    progressDialog.setMessage("Passing paratmeters.. ("+(executeCount+1)+"/5)");
                    // Execute HTTP Post Request
                    executeCount++;
                    response = client.execute(httppost);
                    responseCode = response.getStatusLine().getStatusCode();                        

                } while (executeCount < 5 && responseCode == 408);

                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                while ((line = rd.readLine()) != null)
                {
                    result = line.trim();
                    sb.append(line);

                }
        }catch (Exception e2) {
            responseCode = 408;
            e2.printStackTrace();
            }
        rst = result.toString();


// splits everything        
        if(rst != null && rst.length() > 0)
        {
          strArr1 = rst.split("\\|");
          for(int i=0;i<strArr1.length;i++)
          {


                Log.d("Results", "Array size1.1 "  + i);

            Log.v("Results", "Array split1.2 "  + strArr1[i] );


            if(strArr1[i] != null && strArr1[i].length() >0 && strArr1[i].contains(","))
            {

           strArr2 = strArr1[i].split(",");
              for(int j=0; j<strArr2.length ;j++)
              {
                 // strArr3[i][j] = strArr2[j].toString();

                  if(strArr2.length == 4)
                  {

                    // int temp =  Integer.parseInt(strArr2[j]) ;
                    //  strArr2[j] = Integer.toString(Integer.parseInt(strArr2[j])) ;
                  }
                    Log.d("Results", "Array size2.3 "  + j );

                Log.d("Results", "Array split2.4 "  + strArr2[j] );


              }
            }
          }
        }



/*      if(rst != null && rst.length() > 0)
        {
          strArr = rst.split("\\|");

          for(int i=0;i<strArr.length;i++)
          {
            Log.d("Results", "Array split 1: " + strArr[i]);
           // Log.d("Results", "Array split 2: " + arrfetch[i]);

          }
        }
        */
        this.resultList = strArr2; 

        return strArr2;
    }



@Override
protected void onPostExecute(String[] strArr2)
{
    progressDialog.dismiss();

}

}

strArr2数组返回ategory,纬度,经度,距离值。此类通过按钮侦听器触发,而地图将提供可用对象。在我的keyword.java中:

View firstButton = findViewById(R.id.button1);
firstButton.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {

    gps = new GPSTracker(keyword.this);



    // check if GPS enabled


        String latitude = Double.toString(gps.getLatitude());
        String longitude = Double.toString(gps.getLongitude());

        ProgressDialog progressDialog = new ProgressDialog(keyword.this);
        progressDialog.setCancelable(false);

        myMapService myMapService = new myMapService(keyword.this, progressDialog);
        myMapService.execute(longitude.toString(),latitude.toString(), withinn.toString(), keywordSeleced.toString() );


        Intent i = new Intent(getBaseContext(), GoogleMapsAppActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getApplicationContext().startActivity(i);


        }
        });

我需要在我的GoogleMapsAppActivity.java中使用我的AsyncTask类的结果,它扩展了MapActivity。我需要将这些值传递给这些行:

 Drawable makerDefault = this.getResources().getDrawable(R.drawable.poke);
 MirItemizedOverlay itemizedOverlay = new MirItemizedOverlay(makerDefault);
 itemizedOverlay.addOverlayItem((int) (1E6 * -37.720754), (int) (1E6 * 145.048798), "La trobe");
 mapView.getOverlays().add(itemizedOverlay);
 MapController mc = mapView.getController();
 mc.setCenter(new GeoPoint((int) (1E6 * -37.720754), (int) (1E6 * 145.048798))); /
 mc.zoomToSpan(itemizedOverlay.getLatSpanE6(), itemizedOverlay.getLonSpanE6());

任何帮助真的很感激。先感谢您。

2 个答案:

答案 0 :(得分:1)

onPostExecute()GoogleMapsAppActivity上调用包含这些行的方法。

答案 1 :(得分:0)

在你的onPostExecute()方法中:

super.onPostExecute(strArr2);

它会将strArr2返回给你的调用类

myMapService.execute()之后的

中的

String[] results = myMapService.get();
//results == strArr2

就这么简单!