nullpointerexception当从谷歌地点api请求xml附近的地方
问题描述:
我正在做一个应用程序,请求某个类型的最近的地方,并返回列表中的每个结果的名称,坐标和信息,我使用谷歌地方的api为此。nullpointerexception当从谷歌地点api请求xml附近的地方
我的代码如下
Helper.java具有一个发送网址传送至Google服务器的方法和检索一个XML文件,并将其解析到包含名称,坐标和信息对象的列表:
public Document getDocument(int radius, String type, LatLng current) {
String url = "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?"
+ "location="
+ current.latitude
+ ","
+ current.longitude
+ "&radius="
+ radius
+ "&types="
+ type
+ "&sensor=true&key=MyKeyHere"; //i replace this with my api key
Log.d("URL", url);
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpClient.execute(httpPost, localContext);
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(in);
Log.d("Document content", doc.getTextContent());
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public ArrayList<MyLocation> getNearbyLocations(Document doc) {
Log.d("DOC**", doc.getTextContent());
ArrayList<MyLocation> nearbyLocations = new ArrayList<MyLocation>();
String name;
double lati;
double longi;
String info;
// MyLocation theLocation;
NodeList nLName = doc.getElementsByTagName("name");
NodeList nLLat = doc.getElementsByTagName("lat");
NodeList nLLong = doc.getElementsByTagName("lng");
NodeList nLvicinity = doc.getElementsByTagName("vicinity");
for (int i = 0; i < nLName.getLength(); i++) {
name = nLName.item(i).getNodeValue();
lati = Double.parseDouble(nLLat.item(i).getNodeValue());
longi = Double.parseDouble(nLLong.item(i).getNodeValue());
info = nLvicinity.item(i).getNodeValue();
nearbyLocations.add(new MyLocation(name, new LatLng(lati, longi),
info));
Log.d("LIST***", nearbyLocations.toString());
}
return nearbyLocations;
}
Search.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Helper= new Helper();
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, ls);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
ls.onLocationChanged(location);
docu = Helper.getDocument(5000, "airport" ,new LatLng(alti, longi));
nearByLocation= Helper.getNearbyLocations(docu); ////it gives me a nullpointerexception here
//Log.d("DOC***", docu.getTextContent());
listView = (ListView) findViewById(R.id.my_list2);
adapter = new ItemAdapter(this, getItems());
listView.setAdapter(adapter);
}
第
问题是程序在调用方法getNearByLocations(doc)时崩溃,我认为问题是检索到的文档总是空的,所以我不知道我检索XML文件的方法是否是错误的。
这里是我的logcat:
10-04 19:40:43.049: E/AndroidRuntime(1737): Caused by: java.lang.NullPointerException
10-04 19:40:43.049: E/AndroidRuntime(1737): at com.Gourp_KSA.tourfit.Helper.getNearbyLocations(Helper.java:95)
10-04 19:40:43.049: E/AndroidRuntime(1737): at com.Gourp_KSA.tourfit.Search.onCreate(Search.java:85)
10-04 19:40:43.049: E/AndroidRuntime(1737): at android.app.Activity.performCreate(Activity.java:5104)
10-04 19:40:43.049: E/AndroidRuntime(1737): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-04 19:40:43.049: E/AndroidRuntime(1737): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
编辑&答:
所以,我发现了什么问题。这是我在主线程上建立网络连接。我做了一个AsyncTask类来调用getNearbyLocations()方法。我希望这可以帮助
答
当我相信您应该向Google Places API发送HTTP GET请求时,看起来您正在使用HTTP POST请求。
如果您有兴趣使用能够为您完成所有这些工作的Java库,并允许您通过Java方法和对象与Google Places API进行交互,则可以尝试Sprockets(披露:我是开发人员)。
等效链轮电话的方式获得Place
的List
对象将是:
Places.nearbySearch(Params.create().latitude(lat).longitude(lng)
.radius(5000).types("airport")).getResult();
哪条线是'MapHelper.java:95'? – ssantos
@ssantos它是Helper.java我在这里提出问题时犯了一个错误,现在我编辑它。这是Log.d(“DOC **”,doc.getTextContent())行;你可以在Helper.java的第二个方法中看到它 – Abdullah