使用json进行GET查询 - Retrofit 2.0

时间:2016-07-05 11:18:34

标签: android json gson retrofit

如何进行此查询,我将在下面提及?

方法@GET。查询应如下所示:

/top40?data={"ranking":"world"} /top40?data={"ranking":"country"}

@GET("/api/top40")
    Call<FamousTop40Model> getStatus(
        // what should be there?
    );

    class Factory {
        private static FamousTop40Api service;

        public static FamousTop40Api getInstance() {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiConstants.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            service = retrofit.create(FamousTop40Api.class);

            return service;
        }
    }

你们能帮助我吗?

编辑:+我的标题中应该有accessKey

3 个答案:

答案 0 :(得分:2)

这有助于我:

public interface FamousTop40Api {
    @GET("/api/top40")
    Call<FamousTop40Model> getStatus(
            @Query("data") String ranking
    );

    class Factory {
        private static FamousTop40Api service;

        public static FamousTop40Api getInstance() {
            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
            httpClient.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Interceptor.Chain chain) throws IOException {
                  Request original = chain.request();

                  Request request = original.newBuilder()
                          .header("accessKey", MainActivity.ACCESS_KEY)
                          .method(original.method(), original.body())
                          .build();

                  return chain.proceed(request);
                }
            });

            OkHttpClient client = httpClient.build();

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiConstants.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();

            service = retrofit.create(FamousTop40Api.class);

            return service;
        }
    }
}

因此,您需要在@Query

标题中添加accessKeyOkHttpClient

FamousTop40Api.Factory.getInstance().getStatus("{\"ranking\":\"country\"}").enqueue();

答案 1 :(得分:0)

你可以像这样调用你的GET方法:

 Call<StoreSettings> call = apiService.getStoreSettings(mStore.getStoreID(), mCustomer.getCustId(),
                mStore.getBeaconID(), ProximityConstants.SETTINGS_CUST_TYPE, ProximityConstants.ACTION_STORE_VISIT);
        call.enqueue(new Callback<StoreSettings>() {
            @Override
            public void onResponse(Call<StoreSettings> call, Response<StoreSettings> response) {
                ProximityUtil.writeLog(TAG, "Received store settings");
                mStoreSettings = response.body();
                SharedPreferences.Editor editor = mAppPreferences.edit();
                editor.putString(ProximityConstants.SETTINGS_OBJ_STORE_SETTINGS, new Gson().toJson(mStoreSettings));
                editor.commit();
                Intent intent = new Intent(BeaconScanService.this, ProximityWelcomeActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                BeaconScanService.this.startActivity(intent);
            }

            @Override
            public void onFailure(Call<StoreSettings> call, Throwable t) {
                Toast.makeText(BeaconScanService.this, "Error: " + t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

要调用GET方法webservice,请执行以下操作:

{{1}}

答案 2 :(得分:-1)

尝试如下:创建类Task.java:

message

如下所示:

private void message(final Text t, final String m)
{
    //NB active is declared as 'volatile'
    if (active == false)
    {
        active = true;
        t.getDisplay().asyncExec(new Runnable()
        {
            @Override
            public void run()
            {
                MessageBox mb = new MessageBox(t.getShell());
                mb.setText(m);
                mb.setMessage(t.getMessage() + "\n\n" + m);
                mb.open();
                active = false;
            }
        });
    }
    else
    {
        System.out.println("Already active: " + t.getMessage());
    }
}

使用如下:

public class Task {  
private String ranking;

public Task(String ranking) {
    this.ranking = ranking;
}
}