我的格式为https://example.com/wp-json/newapp/v1/home/?required=
的网址我需要传递required = [{" a":" p1"," b":" q1",&#34 ; C":" 1"},{"":" P2"" b":" Q 2&# 34;," C":" 1"},{"":" P3"" b&#34 ;: " Q3"" C":" 1"}]
我正在使用Retrofit。
我尝试使用此
改进实例类
public class ApiClient {
public static final String BASE_URL = "https://example.com/wp-json/newapp/v1/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
端点
public interface ApiInterface {
@GET("home/?required={value}")
Call<HomeResponse> getHome(@Path("value") HomeParams[] homeParams);
}
创建了一个传递参数的类
public class HomeParams {
private String a;
private String b;
private String c;
public HomeParams(String a, String b, String c) {
this.a = a;
this.b = b;
this.c = c;
}
}
Webservice请求
private void getHome() {
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
HomeParams[] homeParams = new HomeParams[]{
new HomeParams("p1","q1","1"),
new HomeParams("p2","q2","1"),
new HomeParams("p3","q3","4")
};
Log.e(TAG, "getHome: "+homeParams );
Call<HomeResponse> call = apiInterface.getHome(homeParams);
call.enqueue(new Callback<HomeResponse>() {
@Override
public void onResponse(Call<HomeResponse>call, Response<HomeResponse> response) {
HomeResponse homeResponse = response.body();
Log.e(TAG, "onResponse: --------------" );
}
@Override
public void onFailure(Call<HomeResponse>call, Throwable t) {
Log.e(TAG, "onFailure: "+t.getLocalizedMessage() );
}
});
}
我收到此错误
04-06 22:33:10.121 4916-4916/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.new.app, PID: 4916
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.new.app/com.new.app.MainActivity}: java.lang.IllegalArgumentException: URL query string "required={value}" must not have replace block. For dynamic query parameters use @Query.
for method ApiInterface.getHome
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2560)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2626)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5740)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)
Caused by: java.lang.IllegalArgumentException: URL query string "required={value}" must not have replace block. For dynamic query parameters use @Query.
for method ApiInterface.getHome
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:695)
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:686)
at retrofit2.ServiceMethod$Builder.parseHttpMethodAndPath(ServiceMethod.java:296)
at retrofit2.ServiceMethod$Builder.parseMethodAnnotation(ServiceMethod.java:241)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:169)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy0.getHome(Unknown Source)
at com.new.app.MainActivity.getHome(MainActivity.java:154)
at com.new.app.MainActivity.onCreate(MainActivity.java:60)
at android.app.Activity.performCreate(Activity.java:6543)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2513)
... 9 more
我也试过使用@Query。 怎么办呢?
答案 0 :(得分:1)
看来你发送params的方式是错误的。根据您的网址,必需是一个查询参数,因此需要以相同的方式发送。
使用此功能:
public interface ApiInterface {
@GET("home/")
Call<HomeResponse> getHome(@Query("required") HomeParams[] homeParams);
}