gpt4 book ai didi

android - 如何通过改造发送 "clean"url

转载 作者:行者123 更新时间:2023-11-30 02:01:03 24 4
gpt4 key购买 nike

我使用 Retrofit(我是初学者)向 google maps directions 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" : "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

我想这与标题有关。你能帮我“清理” 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());
}
}

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

最佳答案

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

关于android - 如何通过改造发送 "clean"url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31469448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com