如何发送 "clean" url 进行改造

How can I send a "clean" url with retrofit

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

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" : "Too many waypoints in the request (11). The maximum allowed waypoints for this request is 8, plus the origin, and destination.", "routes" : [], "status" : "MAX_WAYPOINTS_EXCEEDED" }

这个答案看起来很正常,因为当我查看 Logcat(Android 工作室)时,发送的 url 是:

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

我想这与headers有关。你能帮我 "clean" 和 url 以便我发送第一个而不是第二个吗?

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

考虑到我正在使用 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());
    }
}

如你所见returns我上面的错误信息。

我相信您的 url_ 项字符串是 origin=the_origindestination=the_destination 等等。如果你只留下内容,=右边的内容应该可以。