使用改造的GET请求无法正常工作,应用崩溃了

时间:2019-02-04 16:34:48

标签: java android

我需要使用翻新程序进行get请求,但是在调用了我界面中的方法后,应用程序崩溃了。这是我必须获取的JSON代码:

[
{
    "username": "matteo",
    "conteggio": 5,
    "isYou": 0
},
{
    "username": "giovanni",
    "conteggio": 7,
    "isYou": 1
}
]

我收到了添加邮件作为参数的信息,所以这是我的界面:

public interface ServerService{
String BASE_URL="https://seconda.herokuapp.com/";
@GET("total?mail={mail}")
Call<List<GetListParameters>> getList(@Path("mail") String mail);
}

这是崩溃的活动(此代码是OnClickListener):

Retrofit retrofit=new Retrofit.Builder()
                        .baseUrl(ServerService.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                ServerService ss=retrofit.create(ServerService.class);
                Call call=ss.getList(Mail); //the app crashes here
                call.enqueue(new Callback<List<GetListParameters>>() {
                    @Override
                    public void onResponse(Call<List<GetListParameters>> call, Response<List<GetListParameters>> response) {
                        if(response.isSuccessful()){
                            ArrayList<UtenteAdapter> UtenteList=new ArrayList<>();
                            for(GetListParameters item: response.body()){
                                UtenteList.add(new UtenteAdapter(item.getUsername(), item.isYou(), item.getConteggio()));
                            }
                            ListView listView = findViewById(R.id.listView);
                            listView.setAdapter(new ListAdapter(getApplicationContext(), R.layout.riga_lista_utente, UtenteList));
                        }else{
                            Toast.makeText(getApplicationContext(), "Errore "+response.code()+" "+response.message(), Toast.LENGTH_SHORT).show();
                        }
                            Toast.makeText(getApplicationContext(), response.body().toString(), Toast.LENGTH_LONG).show();
                        }

                    @Override
                    public void onFailure(Call<List<GetListParameters>> call, Throwable t) {

                    }
                });

我找不到我的错误,非常感谢您的帮助。

这是错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jacopo.waitinglist, PID: 8346
java.lang.IllegalArgumentException: URL query string "mail={mail}" must not have replace block. For dynamic query parameters use @Query.
    for method ServerService.getList
    at retrofit2.Utils.methodError(Utils.java:52)
    at retrofit2.Utils.methodError(Utils.java:42)
    at retrofit2.RequestFactory$Builder.parseHttpMethodAndPath(RequestFactory.java:257)
    at retrofit2.RequestFactory$Builder.parseMethodAnnotation(RequestFactory.java:205)
    at retrofit2.RequestFactory$Builder.build(RequestFactory.java:161)
    at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:65)
    at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:25)
    at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:168)
    at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
    at java.lang.reflect.Proxy.invoke(Proxy.java:913)
    at $Proxy0.getList(Unknown Source)
    at com.example.jacopo.waitinglist.Activity.MainActivity$2.onClick(MainActivity.java:59)
    at android.view.View.performClick(View.java:6935)
    at android.view.View$PerformClick.run(View.java:26211)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:7000)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

应用程序终止。

1 个答案:

答案 0 :(得分:0)

它抱怨:

IllegalArgumentException: URL query string "mail={mail}" must not have replace block.
For dynamic query parameters use @Query.

这意味着,它与?和它后面的查询字符串不同。

@GET("total")
Call<List<GetListParameters>> getList(@NonNull @Query(value = "mail") String mail);

^,这将自动将查询字符串附加到URL上。