代码之家  ›  专栏  ›  技术社区  ›  Sebastian

OsmBonuspack-GeocoderNameim::getFromLocationName-来自服务器的无效响应:HTTP/1.1 403禁止

  •  1
  • Sebastian  · 技术社区  · 10 年前

    我的地理编码器提名。getFromLocation()工作正常。

    现在,我在GeocoderNominim过程中出现错误,没有更改任何内容!?

    来自服务器的无效响应:HTTP/1.1 403禁止

    我的Logcat输出:

    02-02 11:15:31.570: D/BONUSPACK(25238): GeocoderNominatim::getFromLocationName:http://nominatim.openstreetmap.org/search?format=json&accept-language=de&addressdetails=1&limit=1&q=Bolivia
    02-02 11:15:32.210: E/BONUSPACK(25238): Invalid response from server: HTTP/1.1 403 Forbidden
    

    浏览器中的签入工作正常( http://nominatim.openstreetmap.org/search?format=json&accept-language=de&addressdetails=1&limit=1&q=Bolivia )

    我的异步任务:

     public class SearchGeocode extends AsyncTask<String, Integer, List<Address>> {
    
        Context context = MainActivity.this;
        GeocoderNominatim coderNominatim = new GeocoderNominatim(context);
        ProgressDialog progressDialog;
        String countryTitleString;
    
        protected List<Address> doInBackground(String... countryTitle) {
    
            int i = 0;
            publishProgress(i);
    
            countryTitleString = Arrays.toString(countryTitle);
    
            List<Address> geoResults = null;
            try {
                geoResults = coderNominatim.getFromLocationName(countryTitleString, 1);
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(context, "Geocoding error! Internet available?", Toast.LENGTH_SHORT).show();
            }
            return geoResults;
    
        }
    
        protected void onProgressUpdate(Integer... progress) {
    
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setCancelable(true);
            progressDialog.setMessage("Loading ...");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setProgress(0);
            progressDialog.show();
    
        }
    
        protected void onPostExecute(List<Address> geoResults) {
            super.onPostExecute(geoResults);
    
             progressDialog.hide();
    
            if (geoResults.size() == 0) { //if no address found, display an error
                Toast.makeText(context, countryTitleString +" - Country not found.", Toast.LENGTH_SHORT).show();
            } else {
                Address address = geoResults.get(0);
                Bundle extras = address.getExtras();
                BoundingBoxE6 bb = extras.getParcelable("boundingbox");
                mapView.zoomToBoundingBox(bb);
                //makeToast(countryTitle);
            }
        }
    }
    

    知道为什么我从服务器得到无效的响应吗?

    1 回复  |  直到 10 年前
        1
  •  2
  •   MKer    10 年前

    是的:2015年6月,Nomingim更改了他们的使用策略,现在需要一个用户代理。

    所以Geocoder指定构造函数 evolved ,并要求您设置用户代理。通常,类似于:“your_application/version”

    您还可以查看GeocoderNomintim javadoc。

    (我想您使用的是一个相当旧的OSMBonusPack版本)