使用参数进行改造和GET

 勇敢的柯柯_j 发布于 2023-01-11 10:27

我正在尝试使用Retrofit向Google GeoCode API发送请求.服务界面如下所示:

public interface FooService {    
    @GET("/maps/api/geocode/json?address={zipcode}&sensor=false")
    void getPositionByZip(@Path("zipcode") int zipcode, Callback cb);
}

当我打电话给服务时:

OkHttpClient okHttpClient = new OkHttpClient();

RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Constants.GOOGLE_GEOCODE_URL).setClient(new OkClient(okHttpClient)).build();

FooService service = restAdapter.create(FooService.class);

service.getPositionByZip(zipCode, new Callback() {
    @Override public void success(String jsonResponse, Response response) {
       ...
    }
@Override public void failure(RetrofitError retrofitError) {
    }
});

我收到以下stacktrace:

06-07 13:18:55.337: E/AndroidRuntime(3756): FATAL EXCEPTION: Retrofit-Idle
06-07 13:18:55.337: E/AndroidRuntime(3756): Process: com.marketplacehomes, PID: 3756
06-07 13:18:55.337: E/AndroidRuntime(3756): java.lang.IllegalArgumentException: FooService.getPositionByZip: URL query string "address={zipcode}&sensor=false" must not have replace block.
06-07 13:18:55.337: E/AndroidRuntime(3756):     at retrofit.RestMethodInfo.methodError(RestMethodInfo.java:120)
06-07 13:18:55.337: E/AndroidRuntime(3756):     at retrofit.RestMethodInfo.parsePath(RestMethodInfo.java:216)
06-07 13:18:55.337: E/AndroidRuntime(3756):     at retrofit.RestMethodInfo.parseMethodAnnotations(RestMethodInfo.java:162)
06-07 13:18:55.337: E/AndroidRuntime(3756):     at 

我看了一下StackOverflow问题:Retrofit:@GET命令中的多个查询参数?但它似乎不适用.

我从这里逐字逐句地接受了代码:http://square.github.io/retrofit/所以我对理解这个问题感到有点失落.

思考?

2 个回答
  • AFAIK {...}只能用作路径,而不能用在查询参数中.试试这个:

    public interface FooService {    
    
        @GET("/maps/api/geocode/json?sensor=false")
        void getPositionByZip(@Query("address") String address, Callback<String> cb);
    }
    

    如果您要传递未知数量的参数,可以使用以下操作:

    public interface FooService {    
    
        @GET("/maps/api/geocode/json")
        @FormUrlEncoded
        void getPositionByZip(@FieldMap Map<String, String> params, Callback<String> cb);
    }
    

    2023-01-11 10:29 回答
  • @QueryMap 为我工作而不是 FieldMap

    如果你有一堆GET参数,另一种将它们传递到你的网址的方法是HashMap.

    class YourActivity extends Activity {
    
    private static final String BASEPATH = "http://www.example.com";
    
    private interface API {
        @GET("/thing")
        void getMyThing(@QueryMap Map<String, String> params, new Callback<String> callback);
    }
    
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.your_layout);
    
       RestAdapter rest = new RestAdapter.Builder().setEndpoint(BASEPATH).build();
       API service = rest.create(API.class);
    
       Map<String, String> params = new HashMap<String, String>();
       params.put("key1", "val1");
       params.put("key2", "val2");
       // ... as much as you need.
    
       service.getMyThing(params, new Callback<String>() {
           // ... do some stuff here.
       });
    }
    }
    

    调用的URL将是http://www.example.com/thing/?key1=val1&key2=val2

    2023-01-11 10:29 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有