我使用Retrofit(我是初学者)向谷歌地图方向api发送http请求。这是我要发送的网址:
我收到一条消息:
{ " ERROR_MESSAGE" :"请求中的路点过多(11)。此请求的最大允许航路点数为8,加上原点和目的地。", "路线" :[], "状态" :" MAX_WAYPOINTS_EXCEEDED" }
这似乎是正常的答案,因为当我查看Logcat(Android工作室)时,发送的网址是:
我想这与标题有关。你能帮我帮忙吗?#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());
}
}
如您所见,响应会返回上面的错误消息。
答案 0 :(得分:1)
我相信您的url_
项字符串为origin=the_origin
,destination=the_destination
等等。如果您只保留=
右侧的内容,则应该有效。