【问题标题】:java.lang.IllegalArgumentException: Illegal character in query at index 133:java.lang.IllegalArgumentException:索引 133 处的查询中存在非法字符:
【发布时间】:2017-05-19 12:00:06
【问题描述】:

当我为 getdriving direction api 创建 URL 时,我想用两个航点在两个位置之间绘制行驶路线

 Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 133 but the url gives json

喜欢这个my gerdirection api url

所以当我用谷歌搜索时,它说当我在我的 url 中使用它时使用 urlencoder 不会抛出错误但我的 json 像这样是空的

error_message   "Invalid request. Missing the 'origin' parameter."
routes  
status  "INVALID_REQUEST"

请告诉我哪里出错了

我的代码

public String getMapsApiDirectionsUrl() {
    final LatLng source = new LatLng(dsla, dslo);
    final LatLng path1 = new LatLng(dp1la, dp2lo);
    final LatLng path2=new LatLng(dp2la,dp2lo);
    final LatLng dest = new LatLng(ddla, ddlo);
    String origin = "origin=" + source.latitude + "," + source.longitude;
    String waypoints = "waypoints=optimize:true"+"|"+ path1.latitude + ","+ path1.longitude +"|"+ path2.latitude +","+ path2.longitude ;
    String destination = "destination=" + dest.latitude + "," + dest.longitude;
   String way="",sour="",desty="";
    try {
        way = URLEncoder.encode(waypoints, "UTF-8");
      desty = URLEncoder.encode(destination, "UTF-8");
     sour = URLEncoder.encode(origin, "UTF-8");
    }catch (UnsupportedEncodingException e) {

    }
    String key = "AIzaSyDOidVs6_dHl0cjEJ0-OhMfUY0oNFf1SOE ";
    String params = origin+ "&" + destination+ "&" + waypoints +"&"+key;
    String output = "json";

   // String url1="https://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&key=YOUR_API_KEY";
    String url = "https://maps.googleapis.com/maps/api/directions/"
            +output + "?" +params;
    Log.d("taqg",url);
    return url;
}

我的 parsejson

 public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
String results="";
// constructor


public String getJSONFromUrl(final  String url) {
    class GetDataJSON extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {

            // Making HTTP request
            try {
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }

                json = sb.toString();
                is.close();
            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }


            return json;
        }

        @Override
        protected void onPostExecute(String result) {
        results=json;
            Log.d("tag",results);
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();

   return results;

    }
  }

如果 parsejson 有任何错误也告诉我

【问题讨论】:

    标签: android json google-directory-api


    【解决方案1】:

    我认为你的 uri 编码有问题。

    对于方向 uri,我使用了这段代码。它工作正常。根据您的需要修改它..

    private String getDirectionsUrl(LatLng origin,LatLng dest){
        // Origin of route
        String str_origin = "origin="+origin.latitude+","+origin.longitude;
        // Destination of route
        String str_dest = "destination="+dest.latitude+","+dest.longitude;
        // Sensor enabled
        String sensor = "sensor=false";
        // Building the parameters to the web service
        String parameters = str_origin+"&"+str_dest+"&"+sensor;
        // Output format
        String output = "json";
        return "https://maps.googleapis.com/maps/api/directions/"+output+"?"+parameters;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-23
      • 1970-01-01
      • 2021-03-26
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      相关资源
      最近更新 更多