Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改造并获取使用参数_Android_Get_Retrofit - Fatal编程技术网

Android 改造并获取使用参数

Android 改造并获取使用参数,android,get,retrofit,Android,Get,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); } public interface

我正在尝试使用改型向Google GeoCode API发送请求。服务接口如下所示:

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

    @GET("/maps/api/geocode/json")
    @FormUrlEncoded
    void getPositionByZip(@FieldMap Map<String, String> params, Callback<String> cb);
}
我看了一下StackOverflow问题:但它似乎不适用

我从这里一字不差地提取了代码:所以我有点不明白这个问题


想法?

好吧,
{…}
只能用作路径,不能在查询参数中使用。请尝试以下方法:

public interface FooService {    

    @GET("/maps/api/geocode/json?sensor=false")
    void getPositionByZip(@Query("address") String address, Callback<String> cb);
}
公共接口服务{
@获取(“/maps/api/geocode/json?sensor=false”)
void getPositionByZip(@Query(“address”)字符串地址,回调cb);
}
如果要传递的参数数量未知,可以使用执行以下操作:

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

    @GET("/maps/api/geocode/json")
    @FormUrlEncoded
    void getPositionByZip(@FieldMap Map<String, String> params, Callback<String> cb);
}
公共接口服务{
@获取(“/maps/api/geocode/json”)
@FormUrlEncoded
void getPositionByZip(@FieldMap-Map-params,回调cb);
}

@QueryMap
为我工作,而不是
FieldMap

如果您有一组GET参数,另一种将它们传递到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.
   });
}
}
class YourActivity扩展活动{
专用静态最终字符串基路径=”http://www.example.com";
专用接口API{
@获取(“/事物”)
void getMyThing(@QueryMap-params,新回调);
}
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_布局);
RestAdapter rest=new RestAdapter.Builder().setEndpoint(BASEPATH.build();
API服务=rest.create(API.class);
Map params=新的HashMap();
参数put(“键1”、“值1”);
参数put(“键2”、“值2”);
//…你需要多少就有多少。
getMyThing(参数,新回调(){
//…在这里做些事情。
});
}
}

调用的URL将是

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

public interface LocationService {    
    @GET("/example/")
    void getLocation(@Query(value="latlng", encoded=true) String latlng);
}
注意,
encoded=true
是必需的,否则改型将对字符串参数中的逗号进行编码。用法:

String latlng = location.getLatitude() + "," + location.getLongitude();
service.getLocation(latlng);

Kotlin中完成工作示例,我已将我的API键替换为1111

        val apiService = API.getInstance().retrofit.create(MyApiEndpointInterface::class.java)
        val params = HashMap<String, String>()
        params["q"] =  "munich,de"
        params["APPID"] = "11111111111111111"

        val call = apiService.getWeather(params)

        call.enqueue(object : Callback<WeatherResponse> {
            override fun onFailure(call: Call<WeatherResponse>?, t: Throwable?) {
                Log.e("Error:::","Error "+t!!.message)
            }

            override fun onResponse(call: Call<WeatherResponse>?, response: Response<WeatherResponse>?) {
                if (response != null && response.isSuccessful && response.body() != null) {
                    Log.e("SUCCESS:::","Response "+ response.body()!!.main.temp)

                    temperature.setText(""+ response.body()!!.main.temp)

                }
            }

        })
val-apiService=API.getInstance().reformation.create(MyApiEndpointInterface::class.java)
val params=HashMap()
参数[“q”]=“德国慕尼黑”
参数[“APPID”]=“11111111”
val call=apiService.getWeather(params)
排队(对象:Callback{
覆盖失效时的乐趣(调用:调用?、t:可丢弃?){
Log.e(“错误::”,“错误”+t!!.message)
}
覆盖fun onResponse(调用:调用?,响应:响应?){
if(response!=null&&response.issusccessful&&response.body()!=null){
Log.e(“成功::,“响应”+Response.body()!!.main.temp)
temperature.setText(“+response.body()!!.main.temp)
}
}
})
@标题(
“接受:应用程序/json”,
“内容类型:应用程序/json”,
“平台:android”)
@获取(“api/post/post/{id}”)
趣味节目精选帖(
@路径(“id”)id:String,
@标题(“版本”)apiVersion:Int
):呼叫
改装+科特林+RestAPI对我有效的示例


我希望这个带有参数的
@Header
@GET
@Path
也能帮助其他人)

如何添加多个查询params@GilbertoIbarra错误,通过添加更多:
void getPositionByZip(@Query(“address”)字符串地址,@Query(“number”)字符串编号,回调cb)
FormUrlEncoded只能在HTTP方法上指定,请求正文(例如,@POST)错误答案在这里,@FormUrlEncoded不能与GET
@FormUrlEncoded
不与
@GET
注释一起使用。为了更清楚起见,您可以放置getWeather(params)方法定义吗?
@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>