当我致电服务时:
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) {
}
});
我收到以下堆栈:
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
I查看了stackoverflow问题: @retrofit:@get命令中的多个查询参数?
i从这里逐字了解了代码:
Http://square.github.io/retrofit/,所以我有点损失理解该问题。 thoughts?
{...}
只能用作路径,而不是在查询 - 帕拉姆内部。试试看:
public interface FooService {
@GET("/maps/api/geocode/json?sensor=false")
void getPositionByZip(@Query("address") String address, Callback<String> cb);
}
如果您有未知数的参数要通过,则可以使用这样的事情:
@QueryMap
为我工作,而不是
FieldMap
如果您有一堆获取参数,则将其传递到您的URL的另一种方法是
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
@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>
+kotlin
+restapiRestapi为我有用的示例。
我希望这个@GET
,使用参数也会帮助某人)
我还想澄清,如果您要构建复杂的URL参数,则需要手动构建它们。即,如果您的查询为@Path
,而不是单独提供LNG值,则需要在外部构建LATLNG字符串,然后将其作为参数,即::
example.com/?latlng=-37,147
注意是必要的,否则改造将在字符串参数中编码逗号。用法:
public interface LocationService {
@GET("/example/")
void getLocation(@Query(value="latlng", encoded=true) String latlng);
}
中,我用1111替换了我的API键...
encoded=true
https://openweathermap.org/
String latlng = location.getLatitude() + "," + location.getLongitude();
service.getLocation(latlng);
可以将其与Kotlin和Corutines一起使用。可以像这样使用的存储库