我已经研究了这方面的许多问题和示例,但未能理解和解决问题。问题是我需要将字符串数组从我的 AsyncTask 类中的 InBackground 方法获取到另一个对数据进行某些处理的类。
这是我的 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 数组返回属性、纬度、经度、距离值。此类通过按钮侦听器触发,而地图将提供可用对象。在我的 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());
任何帮助都非常感谢。先感谢您。