我怎样才能发送"清洁"改造的网址

时间:2015-07-17 06:17:50

标签: android google-maps retrofit

我使用Retrofit(我是初学者)向谷歌地图方向api发送http请求。这是我要发送的网址:

  

https://maps.googleapis.com/maps/api/directions/json?origin=45.509166,-73.497897&destination=45.5027,-73.503455&waypoints=optimize:false|45.509196,-73.495494|45.511166,-73.493584|45.515887,-73.500751|45.516835,-73.507189|45.514994,-73.515065|45.507828,-73.515879|45.504038,-73.516008|45.508971,-73.505665|&sensor=false

我收到一条消息:

  

{      " ERROR_MESSAGE" :"请求中的路点过多(11)。此请求的最大允许航路点数为8,加上原点和目的地。",      "路线" :[],      "状态" :" MAX_WAYPOINTS_EXCEEDED"   }

这似乎是正常的答案,因为当我查看Logcat(Android工作室)时,发送的网址是:

  

https://maps.googleapis.com/maps/api/directions/json?origin=origin%3D45.509166%2C-73.497897&destination=destination%3D45.5027%2C-73.503455&waypoints=waypoints%3Doptimize%3Afalse|45.509196%2C-73.495494|45.511166%2C-73.493584|45.515887%2C-73.500751|45.516835%2C-73.507189|45.514994%2C-73.515065|45.507828%2C-73.515879|45.504038%2C-73.516008|45.508971%2C-73.505665|&sensor=sensor%3Dfalse

我想这与标题有关。你能帮我帮忙吗?#34;清洁"网址,以便我发送第一个而不是第二个。

编辑:我将添加我的代码。

鉴于我使用android-priority-jobqueue作为我的后台作业,以及我在后台工作中发出我的http请求这一事实,我必须使用同步请求和Retrofit,被阻止的线程将是后台工作的一个。 首先,api:

    //Retrofit API
    //DirectionApiRequestInterface.java
    public interface DirectionApiRequestInterface {

        @GET("/maps/api/directions/json")
       /*
       //asynch request
        public void getJson(@Query("origin") String origin,
                            @Query("destination") String destination,
                            @Query("waypoints") String waypoints,
                            @Query("sensor") String sensor,
                            //and api key?
                            Callback<String> callback);*/
        //synch request: all wait on the same thread
        public Response getJson(@Query("origin") String origin,
                            @Query("destination") String destination,
                            @Query("waypoints") String waypoints,
                            @Query("sensor") String sensor);

    }

现在,在我的工作中,我有这些要求:

/**
 * Created by Max-poly on 2015-07-14.
 * in this file, we will manage the priorityJobQueue (android-priority-jobqueue from square.)
 * The tasks performed here are:
 * 1- make the Http request
 * 2- parse the response(which we'll try to get in String)
 * 3- calculate the distance between all the points and send the drawn route UI
 */
public class DirectionRequestJob extends Job {
    public static final int PRIORITY = 1;
    private static String baseUrl_ ="https://maps.googleapis.com";
    private static DirectionApiRequestInterface client_;
    private CustomLogger customLogger;

    private ArrayList<String>(); url_;


    /*note that the array list contains the splitted url:
        origin:        url.get(0)
        destination:   url.get(1)
        waypoints:     url.get(2)
        sensor:        url.get(3)
     */
    public DirectionRequestJob(ArrayList<String> url) {
        super(new Params(PRIORITY).requireNetwork().persist());
        url_ = new ArrayList<String>();
        url_.addAll(url);
    }

    //lifecycle of a Job
    @Override
    public void onAdded(){
       //doesn't apply to our case
    }

    @Override
    public void onRun() throws Throwable{
            setupClient();

            // retrofit synch response
            String response = converter(get().getJson(url_.get(0),urls_.get(1),urls_.get(2),urls_.get(3)));

            //parse
            //evaluate the routes/distance/duration etc
    }

    @Override
    protected void onCancel() {
        // Job has exceeded retry attempts or shouldReRunOnThrowable() has returned false.
    }

    @Override
    protected boolean shouldReRunOnThrowable(Throwable throwable) {
        return false;
    }
    //-end lifecyle

    public static DirectionApiRequestInterface get(){
        return client_;
    }

    static{
        setupClient();
    }

    //here is where the rest adapter is setup
    private static void setupClient(){
        RestAdapter.Builder builder = new RestAdapter.Builder()
                .setEndpoint(baseUrl_)
                .setClient(new OkClient(new OkHttpClient()))
                .setLogLevel(RestAdapter.LogLevel.FULL);

        RestAdapter restAdapter = builder.build();
        client_ = restAdapter.create(DirectionApiRequestInterface.class);
    }

    public String converter(Response response){
        return new String(((TypedByteArray) response.getBody()).getBytes());
    }
}

如您所见,响应会返回上面的错误消息。

1 个答案:

答案 0 :(得分:1)

我相信您的url_项字符串为origin=the_origindestination=the_destination等等。如果您只保留=右侧的内容,则应该有效。