使用参数进行改造和GET

Per*_*tra 78 android get retrofit

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

public interface FooService {    
    @GET("/maps/api/geocode/json?address={zipcode}&sensor=false")
    void getPositionByZip(@Path("zipcode") int zipcode, Callback<String> cb);
}
Run Code Online (Sandbox Code Playgroud)

当我打电话给服务时:

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<String>() {
    @Override public void success(String jsonResponse, Response response) {
       ...
    }
@Override public void failure(RetrofitError retrofitError) {
    }
});
Run Code Online (Sandbox Code Playgroud)

我收到以下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 
Run Code Online (Sandbox Code Playgroud)

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

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

思考?

Bar*_*ers 134

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

public interface FooService {    

    @GET("/maps/api/geocode/json?sensor=false")
    void getPositionByZip(@Query("address") String address, Callback<String> cb);
}
Run Code Online (Sandbox Code Playgroud)

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

public interface FooService {    

    @GET("/maps/api/geocode/json")
    @FormUrlEncoded
    void getPositionByZip(@FieldMap Map<String, String> params, Callback<String> cb);
}
Run Code Online (Sandbox Code Playgroud)

  • FormUrlEncoded只能在带有请求体的HTTP方法上指定(例如@POST) (8认同)
  • 错误的答案在这里,@ FormUrlEncoded不能与GET一起使用 (6认同)
  • @GilbertoIbarra 错误,添加更多内容:`void getPositionByZip(@Query("address") String address, @Query("number") String number, Callback&lt;String&gt; cb);` (2认同)

mad*_*527 36

@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.
   });
}
}
Run Code Online (Sandbox Code Playgroud)

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


The*_*eIT 6

我还想澄清一下,如果您要构建复杂的 url 参数,则需要手动构建它们。即如果您的查询是example.com/?latlng=-37,147,而不是单独提供 lat 和 lng 值,您将需要在外部构建 latlng 字符串,然后将其作为参数提供,即:

public interface LocationService {    
    @GET("/example/")
    void getLocation(@Query(value="latlng", encoded=true) String latlng);
}
Run Code Online (Sandbox Code Playgroud)

注意encoded=true是必要的,否则改造将在字符串参数中编码逗号。用法:

String latlng = location.getLatitude() + "," + location.getLongitude();
service.getLocation(latlng);
Run Code Online (Sandbox Code Playgroud)


Ole*_*ndr 5

@Headers(
    "Accept: application/json",
    "Content-Type: application/json",
    "Platform: android")
@GET("api/post/post/{id}")
fun showSelectedPost(
    @Path("id") id: String,
    @Header("Version") apiVersion: Int
): Call<Post>
Run Code Online (Sandbox Code Playgroud)

对我有用的改造+ Kotlin + RestAPI示例。

我希望这@Header, @GET,@Path带参数也会对某人有所帮助)