我如何在Retrofit2中发出这样的请求

时间:2018-09-28 12:23:51

标签: android rest retrofit retrofit2

我有任务。 我想使用此模式并使用Retrofit2向服务器发出请求:

http://server.com?test[ {我的数据:字符串}] = {我的SecondData:整数}。

示例:http://server.com?test[ifD3234oeW]=10

您能在界面中向我展示一个可以解决我问题的函数吗?

谢谢您的回答!

2 个答案:

答案 0 :(得分:0)

1。在API界面中创建函数:

public interface ApiInterface {

@GET
retrofit2.Call<"return_type"> testServer(@Url String url); }

2。创建您要使用的接口实例:

public ApiInterface apiInterface;

3。进行API调用

apiInterface = ApiClient.GetApiClient().create(ApiInterface.class);

            retrofit2.Call<"return_type"> result = apiInterface.testServer(url);

            result.enqueue(new Callback<return_type>() {
                @Override
                public void onResponse(retrofit2.Call<return_type> call, Response<return_type> response) {
                    if (response != null){
                           //do something
                    }
                }

                @Override
                public void onFailure(retrofit2.Call<return_type> call, Throwable t) {
                   //do something
                }
            });

答案 1 :(得分:0)

尝试如下

public interface ServiceInterface {

    @GET("?test[{first_data}]={second_data}")
    Call<POJO> getData(
        @Path("first_data") String firstData,
        @Path("second_data") int secondData
    );

}   

替换POJO(Call的返回类型),并根据需要替换变量名。

注意:我尚未测试以上代码。

相关问题