我写了一个应用程序。该功能将纬度和经度传达给 webserver
,然后mysql
访问标记map
。
我使用了计时器,以便他每隔几秒就可以访问一次。但不知道为什么有异常。有没有办法解决它。
public class TeamB extends MapActivity {
protected static final int REFRESH_DATA2 = 0x00000002;
Handler mHandler2 = new Handler()
{
@Override
public void handleMessage(Message msg2 )
{
switch (msg2.what)
{
case REFRESH_DATA2:
String result2 = null;
if (msg2.obj instanceof String)
result2 = (String) msg2.obj;
if (result2 != null)
if(ss>0){
ok();
initMarkerOverlay();
}
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Location mLocation = getLocation(this);
X=mLocation.getLongitude();
Y=mLocation.getLatitude();
xx = String.valueOf(X);
yy = String.valueOf(Y);
mView = (MapView) findViewById(R.id.mView);
ImageView v = (ImageView)findViewById(R.id.imageView1);
v.getBackground().setAlpha(100);
initMapView();
Timer timers = new Timer();
timers.scheduleAtFixedRate(new MyTask(), 10000, 100000);
Timer timerl = new Timer();
timerl.scheduleAtFixedRate(new MyTask2(), 1, 5000);
}
private class MyTask extends TimerTask{
@Override
public void run() {
uri="http://1.xxx.xx.xx:8080/GGPSS2/SAVE2";
ss=0;
String msg2 = null ;
Thread C = new Thread(new sendPostRunnable2(msg2));
C.start();
}
}
private class MyTask2 extends TimerTask{
@Override
public void run() {
uri="http://1.163.xx.xxx:8080/GGPSS2/LOAD";
ss=1;
String msg2 = null ;
Thread C = new Thread(new sendPostRunnable2(msg2));
C.start();
}
}
private String sendPostDataToInternet2(String strTxt2)
{
HttpPost httpRequest2 = new HttpPost(uri);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("gpsx",xx));
params.add(new BasicNameValuePair("gpsy",yy));
try
{
httpRequest2.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse2 = new DefaultHttpClient()
.execute(httpRequest2);
s=httpResponse2.getStatusLine().getStatusCode();
if (httpResponse2.getStatusLine().getStatusCode() == 200)
{
String strResult2 = EntityUtils.toString(httpResponse2.getEntity());
ew = strResult2;
return strResult2;
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
class sendPostRunnable2 implements Runnable
{
String strTxt2 = null;
public sendPostRunnable2(String strTxt2)
{
this.strTxt2 = strTxt2;
}
@Override
public void run()
{
String result2 = sendPostDataToInternet2(strTxt2);
mHandler2.obtainMessage(REFRESH_DATA2, result2).sendToTarget();
}
}
private void ok()
{
i=0;
String html =ew;
Document doc = Jsoup.parse(html);
Elements as = doc.select("#dd");
Elements bs = doc.select("#ff");
for (Element asds : as)
{
xs[i]=asds.select("div").text();
xxs[i] = Double.parseDouble(xs[i]);
i++;
}
i=0;
for (Element bbs : bs)
{
ys[i]=bbs.select("div").text();
yys[i] = Double.parseDouble(ys[i]);
i++;
}
}
public Location getLocation(Context context)
{
LocationManager locMan = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Location location = locMan
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
location = locMan
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
return location;
}
private void initMapView()
{
mView.setBuiltInZoomControls(true);
mView.setTraffic(true);
mlOverlay = new MyLocationOverlay(this, mView);
mControl = mView.getController();
mlOverlay.runOnFirstFix(new Runnable()
{
public void run()
{
mControl.setZoom(15);
mControl.animateTo(mlOverlay.getMyLocation());
}
});
mView.getOverlays().add(mlOverlay);
}
private class MarkerOverlay extends ItemizedOverlay<OverlayItem>
{
private List<OverlayItem> olItems = new ArrayList<OverlayItem>();
public MarkerOverlay(Drawable defaultMarker)
{
super(boundCenterBottom(defaultMarker));
if(rd>1)
{
for(int j=0;j<g;j++)
{
olItems.add(new OverlayItem(sucker[j], "???", "???"));
}
populate();
rd=0;
}
Timer timerl = new Timer();
timerl.scheduleAtFixedRate(new MyTask(), 1, 6000);
}
private class MyTask extends TimerTask
{
@Override
public void run()
{
for(int j=0;j<g;j++)
{
olItems.remove(sucker[j]);
}
populate();
for(int j=0;j<g;j++){
olItems.add(new OverlayItem(sucker[j], "???", "???"));
}
populate();
}
}
@Override
protected OverlayItem createItem(int i)
{
return olItems.get(i);
}
@Override
public int size()
{
return olItems.size();
}
@Override
protected boolean onTap(int index)
{
Toast.makeText(
TeamB.this,
olItems.get(index).getSnippet(),
Toast.LENGTH_SHORT)
.show();
return true;
}
}
private void initMarkerOverlay()
{
g=0;
sup=i-3;
if(sup<0){
sup=0;
i=3;
}
for(int j=sup;j<i;j++)
{
sucker[g] = new GeoPoint((int) (yys[j]*1E6),(int) (xxs[j]*1E6));
g++;
}
if(sx>0)
{
Drawable marker = getResources().getDrawable(R.drawable.suck);
markOverlay = new MarkerOverlay(marker);
mView.getOverlays().add(markOverlay);
sx=0;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.exit:
System.exit(0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
这是logcat错误代码:
: E/AndroidRuntime(21872): FATAL EXCEPTION: main :
E/AndroidRuntime(21872): java.lang.ArrayIndexOutOfBoundsException:
length=93; index=93 : E/AndroidRuntime(21872): at
com.google.android.maps.ItemizedOverlay.getIndexToDraw(ItemizedOverlay.java:211)
: E/AndroidRuntime(21872): at
com.google.android.maps.ItemizedOverlay.draw(ItemizedOverlay.java:240)
: E/AndroidRuntime(21872): at
com.google.android.maps.Overlay.draw(Overlay.java:179) :
E/AndroidRuntime(21872): at
com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:45) :
E/AndroidRuntime(21872): at
com.google.android.maps.MapView.onDraw(MapView.java:530) :
E/AndroidRuntime(21872): at android.view.View.draw(View.java:11184)
: E/AndroidRuntime(21872): at
android.view.ViewGroup.drawChild(ViewGroup.java:2892) :
E/AndroidRuntime(21872): at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) :
E/AndroidRuntime(21872): at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) :
E/AndroidRuntime(21872): at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) :
E/AndroidRuntime(21872): at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) :
E/AndroidRuntime(21872): at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) :
E/AndroidRuntime(21872): at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) :
E/AndroidRuntime(21872): at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) :
E/AndroidRuntime(21872): at android.view.View.draw(View.java:11187)
: E/AndroidRuntime(21872): at
android.widget.FrameLayout.draw(FrameLayout.java:450) :
E/AndroidRuntime(21872): at
com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2299)
: E/AndroidRuntime(21872): at
android.view.ViewRootImpl.draw(ViewRootImpl.java:2210) :
E/AndroidRuntime(21872): at
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1816)
: E/AndroidRuntime(21872): at
android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628) :
E/AndroidRuntime(21872): at
android.os.Handler.dispatchMessage(Handler.java:99) :
E/AndroidRuntime(21872): at android.os.Looper.loop(Looper.java:137)
: E/AndroidRuntime(21872): at
android.app.ActivityThread.main(ActivityThread.java:4511) :
E/AndroidRuntime(21872): at
java.lang.reflect.Method.invokeNative(Native Method) :
E/AndroidRuntime(21872): at
java.lang.reflect.Method.invoke(Method.java:511) :
E/AndroidRuntime(21872): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
: E/AndroidRuntime(21872): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) :
E/AndroidRuntime(21872): at dalvik.system.NativeStart.main(Native
Method)