Retrofit post request with parameters

后端 未结 3 1886
北海茫月
北海茫月 2021-01-23 02:50

I am using postman extension for post request. I want to make same request with android. I used retrofit library for access my goal. But I can\'t get successful result. Where is

3条回答
  •  一个人的身影
    2021-01-23 03:29

    Which version you use in retrofit? Below code for version 2.0. If any Header require please check. If you can share url and username or password. I will check.

    public interface Interfacem {
    
        @FormUrlEncoded
        @POST("login")
        Call getResponse(@Field("username") String username,@Field("password")String password );
    }
    
    
    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl("http://myurl.com/")
                            .build();
    
                    Interfacem service = retrofit.create(Interfacem.class);
                    Call result =service.getResponse("myUsername","myPassword");
                    result.enqueue(new Callback() {
                        @Override
                        public void onResponse(Response response) {
                            try {
                                System.out.println(response.body().string().toString());
                            } catch (IOException|NullPointerException e) {
                                e.printStackTrace();
                            }
    
                        }
    
                        @Override
                        public void onFailure(Throwable t) {
                            t.printStackTrace();
                        }
                    });
    

提交回复
热议问题