我试图抓一个网站来获取lat,使用Jsoup放入gmaps但是我得到了一个堆栈跟踪
FATAL EXCEPTION: main
Process: au.com.industryresponsetraining.map, PID: 18842
java.lang.NumberFormatException: Invalid double: ""
由于
,我已经阅读过了您正在解析onCreate()值而不添加任何默认值,因此异常
除了我的onCreate()是我的Jsoup运行的地方。 如果我添加像
这样的东西double myDouble;
String myString = ((EditText) findViewById(R.id.editText1)).getText().toString();
if (myString != null && !myString.equals("")) {
myDouble = Double.valueOf(myString);
} else {
myDouble = 0;
}
引自here我的地图最终出海。
我的网页上有一个有效的lat,就在它上面
这是我的活动
package au.com.industryresponsetraining.map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
String ln = "";
String lt = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
new Thread(new Runnable() {
@Override
public void run() {
try {
//get the Document object from the site. Enter the link of site you want to fetch
Document document = Jsoup.connect("http://www.industryresponsetraining.com.au/mdt/lat.html").get();
//Get the title of blog using title tag
//title = document.select("p").text().toString();
lt = document.select("h6").text().toString();
ln = document.select("h7").text().toString();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
double lats = Double.valueOf(lt);
double lngs = Double.valueOf(ln);
LatLng marker = new LatLng(lats, lngs);
mMap.addMarker(new MarkerOptions().position(marker).title("my Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(marker));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15));
}
}
实际的Google地图内容已从developers页面
中引用答案 0 :(得分:1)
它应该是double
值,而不是empty
(""
)String
Ansewer:如
所述实际上,在你的onMapReady之后发生了2行代码。因此,当onMapReady执行时,你的lt和ln总是“”。因此,您需要确定何时使用新值更新了lt和'ln,然后在此之前创建市场。
编辑工作代码
package au.com.industryresponsetraining.map;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private static final String TAG = MapsActivity.class.getSimpleName();
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
new ParseAsyncTask().execute("http://www.industryresponsetraining.com.au/mdt/lat.html");
}
class ParseAsyncTask extends AsyncTask<String, Void, String[]> {
@Override
protected String[] doInBackground(String... params) {
String url = params[0];
try {
//get the Document object from the site. Enter the link of site you want to fetch
Document document = Jsoup.connect(url).get();
//Get the title of blog using title tag
//title = document.select("p").text().toString();
String lat = document.select("h6").text().toString();
Log.d(TAG, lat);
String lng = document.select("h7").text().toString();
Log.d(TAG, lng);
return new String[]{lat, lng};
} catch (IOException e) {
e.printStackTrace();
return new String[]{"0.0", "0.0"};
}
}
@Override
protected void onPostExecute(String[] result) {
createMarker(result);
}
}
private void createMarker(String[] result) {
if (mMap != null) {
double lats = Double.valueOf(result[0]);
double lngs = Double.valueOf(result[1]);
LatLng marker = new LatLng(lats, lngs);
mMap.addMarker(new MarkerOptions().position(marker).title("my Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(marker));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15));
}
}
}
答案 1 :(得分:0)
这只是由于并行进程以“错误”顺序完成而导致的计时问题。 (“错误”与你的预期相反。)
在onCreate()
中,您可以通过调用getMapAsync()
启动异步操作以获取 Google地图。该操作将运行一段时间,一旦完成,就会触发onMapReady()
方法。
在onCreate()
中,您还可以创建自己的线程,该线程运行 jsoup 代码来获取网页并从中提取坐标。 jsoup 的connect()
和get()
方法运行一段时间,完成后再提取坐标。
如果 Google Map 代码之前已完成,则 jsoup 代码尚未提取并提取坐标。
由于谷歌地图代码具有更高的优先级(您首先需要地图才能在其上绘制标记),您应该先让它完成。
然后在onMapReady()
方法中,您可以开始 jsoup 代码。
最小的代码修改可能类似于:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
String ln = "";
String lt = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
new Thread(new Runnable() {
@Override
public void run() {
try {
//get the Document object from the site. Enter the link of site you want to fetch
Document document = Jsoup.connect("http://www.industryresponsetraining.com.au/mdt/lat.html").get();
//Get the title of blog using title tag
//title = document.select("p").text().toString();
lt = document.select("h6").text().toString();
ln = document.select("h7").text().toString();
// The UI must be modified from the UI thread.
runOnUiThread(new Runnable() {
@Override
public void run() {
double lats = Double.valueOf(lt);
double lngs = Double.valueOf(ln);
LatLng marker = new LatLng(lats, lngs);
mMap.addMarker(new MarkerOptions().position(marker).title("my Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(marker));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15));
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
需要runOnUiThread()
,因为 Google地图是用户界面的一部分,无法从后台线程修改用户界面。
然而(上图)并不是真正适合Android的方式。而不是仅为 jsoup 代码创建标准Java线程,然后在该线程中使用runOnUiThread()
,您应该使用AsyncTask
,如下所示:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
String ln = "";
String lt = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
AsyncTask jsoupTask = new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
// This runs in a background thread.
Document document;
try {
//get the Document object from the site. Enter the link of site you want to fetch
document = Jsoup.connect("http://www.industryresponsetraining.com.au/mdt/lat.html").get();
} catch (Exception e) {
e.printStackTrace();
}
return document; // Might be null!
}
@Override
protected void onPostExecute(Document document) {
// This is triggered once the code in doInBackground() has been completed.
super.onPostExecute(document);
if (document != null) {
try {
//Get the title of blog using title tag
//title = document.select("p").text().toString();
lt = document.select("h6").text().toString();
ln = document.select("h7").text().toString();
double lats = Double.valueOf(lt);
double lngs = Double.valueOf(ln);
LatLng marker = new LatLng(lats, lngs);
mMap.addMarker(new MarkerOptions().position(marker).title("my Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(marker));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15));
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
jsoupTask.execute();
}
}