我在这个环节有一公里:
http://myurl.com/mykml.kml
我想从中获取com.ekito.simpleKML.modelkml对象。
我正在尝试:

String url = "http://myurl.com/mykml.kml";
Serializer kmlSerializer = new Serializer();
Kml kml = kmlSerializer.read(url);

但是kml对象仍然为空。
这是到ekito simple kml库的链接:https://github.com/Ekito/Simple-KML

最佳答案

我看到他在网上看不到文件。测试这个例子!

     private ProgressDialog progressBar;
     public static final int KML_PROGRESS = 0;
     public String fileURL ="http://myurl.com/mykml.kml";


     // set in OnClick Button
     new DownloadKML().execute(fileURL);

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case KML_PROGRESS:
            progressBar = new ProgressDialog(this);
            progressBar.setMessage("Downloading file…");
            progressBar.setIndeterminate(false);
            progressBar.setMax(100);
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBar.setCancelable(true);
            progressBar.show();
            return progressBar;
        default:
            return null;
    }
}




class DownloadKML extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
    super.onPreExecute();
    showDialog(KML_PROGRESS);
}

@Override
protected String doInBackground(String...  url) {
 int count;

try {

     URL url = new URL(  url[0] );
     URLConnection connect = url.openConnection();
     connect.connect();
     int progressOfFile = connect.getContentLength();
     InputStream input = new BufferedInputStream(url.openStream());
     OutputStream output = new FileOutputStream("/sdcard/KML_Samples.kml");

    byte data[] = new byte[1024];

   long total = 0;

    while ((count = input.read(data)) != -1) {
        total += count;
        publishProgress(""+(int)((total*100)/progressOfFile) );
        output.write(data, 0, count);
    }

    output.flush();
    output.close();
    input.close();
} catch (Exception e) {}
return null;

}
protected void onProgressUpdate(String... progress) {
      progressBar.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String unused) {
    dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
    String pathKML = Environment.getExternalStorageDirectory().toString() + "/KML_Samples.kml";

    // load
    Serializer kmlSerializer = new Serializer();
    Kml kml = kmlSerializer.read(url);
 }
}

09-27 00:06