seraphy_pcy 2015-03-04 06:04 采纳率: 0%
浏览 1971

android客户端基于百度地图与服务器连接失败

图片说明
LocationListener的派生类class图标上一直有个红色方框

intent启动GPSReport
system.out会输出Service startcommand!
不输出TestLocatioinListener里的位置时间、经纬度等。
之前logcat有报错could not find class com.baidu.platform.comapi.basestruct.Geopoint
referenced from TestLoacatioListener里的OnLoactionChanged()函数
我把jar包换成2_1_3版本的就不再报错了可是红色方框还是在
求大神指教 如何才能成功连接上服务器!

public class GPSReport extends Service {

TestLocationListener testLocationListener = new TestLocationListener();
TelephonyManager telephonyManager = null;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    System.out.println("Service create!!");

    // 开始持续定位
    LocationManager locationManager = (LocationManager) GPSReport.this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000, 0, testLocationListener);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    System.out.println("Service startcommand!!");
    return super.onStartCommand(intent, flags, startId);

}

private class TestLocationListener implements LocationListener {

    private HttpResponse httpResponse = null;// 服务端响应
    private HttpEntity httpEntity = null;// 响应的内容

    @Override
    // 当位置改变时做的->post方法上报gps信息
    public void onLocationChanged(Location location) {
        //虚拟机测试无IMEI码,故暂用手动设定值
        //System.out.println(telephonyManager.getDeviceId());
        //System.out.println("1234567");
        System.out.println(location.getTime());
        System.out.println(location.getLongitude());
        System.out.println(location.getLatitude());
        System.out.println(location.getSpeed());

         GeoPoint nowPoint = CoordinateConvert.fromWgs84ToBaidu(new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6)));
         System.out.println("baidu坐标:");
         System.out.println(nowPoint.getLatitudeE6()/1000000.0);
         System.out.println(nowPoint.getLongitudeE6()/1000000.0);

        /////////自动监测上报条件语句。为了调试方便暂时新增两个按钮,手动点选上报
        //if(location.getSpeed()*3.6f>RTApplication.minBusSpeed&&location.getSpeed()*3.6f<RTApplication.maxBusSpeed){}

        // 获取相关参数,存于NameValurPairs中
        //String userID = telephonyManager.getDeviceId();
        String userID = "86302014796";
        String time = Long.toString(location.getTime());
        String longitude = Double.toString(nowPoint.getLongitudeE6()/1000000.0);
        String latitude = Double.toString(nowPoint.getLatitudeE6()/1000000.0);
        String speed = Float.toString(location.getSpeed());

        NameValuePair idPair= new BasicNameValuePair("userID", userID);
        NameValuePair timePair = new BasicNameValuePair("time", time);
        NameValuePair longitudePair = new BasicNameValuePair("longitude",longitude);
        NameValuePair latitudePair = new BasicNameValuePair("latitude",latitude);
        NameValuePair speedPair = new BasicNameValuePair("speed", speed);

        final List<NameValuePair> gpsPairs = new ArrayList<NameValuePair>();
        gpsPairs.add(idPair);
        gpsPairs.add(timePair);
        gpsPairs.add(longitudePair);
        gpsPairs.add(latitudePair);
        gpsPairs.add(speedPair);

        ////////////////////////多线程上报
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                // post方法上报
                try {
                    System.out.println("========run()=======");
                    HttpEntity requestHttpEntity = new UrlEncodedFormEntity(gpsPairs);
                    HttpPost httpPost = new HttpPost("http://10.10.185.31:80");//======URL========
                    httpPost.setEntity(requestHttpEntity);
                    HttpClient httpClient = new DefaultHttpClient();

                    // 获取服务端响应及内容
                    InputStream inputStream = null;
                    try {
                        httpResponse = httpClient.execute(httpPost);
                        httpEntity = httpResponse.getEntity();
                        inputStream = httpEntity.getContent();
                        // 循环取出响应内容
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                        String result = "";
                        String line = "";
                        while ((line = reader.readLine()) != null) {
                            result = result + line;
                        }
                        System.out.println(result);

                    } catch (Exception e) {
                        System.out.println("ex");
                        e.printStackTrace();
                    } finally {
                        inputStream.close();// 关掉输入流
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        });

        thread.start();

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

@Override
public void onDestroy() {
    // 关闭持续定位
    LocationManager locationManager = (LocationManager) GPSReport.this
            .getSystemService(Context.LOCATION_SERVICE);
    locationManager.removeUpdates(testLocationListener);
    super.onDestroy();
    System.out.println("Service destroy!!");
}

}

  • 写回答

1条回答 默认 最新

  • LIUCQW 2015-03-07 14:03
    关注

    你有百度地图sdk的key么

    评论

报告相同问题?

悬赏问题

  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题