无法使用或从Android Retrofit客户端调用Restful API,如下所述

时间:2017-12-30 22:51:19

标签: android retrofit restful-authentication

我正在开发一个简单的Android应用程序(用于练习),允许用户使用改装登录到此api(https://dbb7xl5qn7.execute-api.eu-west-1.amazonaws.com/dev/)。我已经在android中创建了客户端界面但是我在连接到Api时遇到了问题。这是API文档,但我遗漏了一些东西。提前致谢。应用程序运行正常,但是当我输入详细信息时,它无法连接。我试着调试,我收到了这条消息

api界面

    public interface APIInterface {

        @POST("login/")
        Call<DetailUser> loginUser(@Body LoginUser user);

       // @POST("/rest-auth/registration/")
       // Call<DetailUser> createUser(@Body CreateUser createUser);

    }

API客户端

    public class APIClient {

        private static Retrofit retrofit = null;

        static Retrofit getClient() {

            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();


            retrofit = new Retrofit.Builder()
                    .baseUrl("https://dbb7xl5qn7.execute-api.eu-west-1.amazonaws.com/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();



            return retrofit;
        }

    }

这是主要活动

    public class MainActivity extends AppCompatActivity {


        private TextView mIdPK,mIdUsername,mIdEmail,mIdToken;

        private PrefManager prefManager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mIdPK = (TextView) findViewById(R.id.tv_id_pk);
            mIdUsername = (TextView) findViewById(R.id.tv_id_username);
            mIdEmail = (TextView) findViewById(R.id.tv_id_email);
            mIdToken = (TextView) findViewById(R.id.tv_id_token);


            prefManager = new PrefManager(this);

            mIdPK.setText(prefManager.getIdPk());
            mIdUsername.setText(prefManager.getIdUsername());
            mIdEmail.setText(prefManager.getIdEmail());
            mIdToken.setText(prefManager.getIdToken());



        }
    }

并登录Activitity

    public class LoginActivity extends AppCompatActivity {

        private EditText mEtUsername, mEtPassword;
        private Button buttonLogin, buttonRegister;
        APIInterface apiInterface;

        private PrefManager prefManager;

        private boolean loginStat;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

            mEtUsername = (EditText) findViewById(R.id.et_login_username);
            mEtPassword = (EditText) findViewById(R.id.et_login_password);
            buttonLogin = (Button) findViewById(R.id.btn_login);
            buttonRegister = (Button) findViewById(R.id.btn_login_register);
            apiInterface = APIClient.getClient().create(APIInterface.class);
            prefManager = new PrefManager(this);

            loginStat = prefManager.getLoginStat();


            if(loginStat){

                startActivity(new Intent(LoginActivity.this,MainActivity.class));
                finish();

            }else{
                buttonLogin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String username = mEtUsername.getText().toString();
                        String password = mEtPassword.getText().toString();
                        LoginUser user = new LoginUser(username, password);

                        // Retrofit Process
                        Call call1 = apiInterface.loginUser(user);
                        call1.enqueue(new Callback() {
                            @Override
                            public void onResponse(Call call, Response response) {
                                String ok = "OK";
                                Log.v("RESPON:",response.message().toString());
                                if(ok.equals(response.message().toString())){
                                    DetailUser user1 = (DetailUser) response.body();
                                    User user2 = user1.user;

                                    String idpk = String.valueOf(user2.pk);

                                    prefManager.setIdPk(idpk);
                                    prefManager.setIdEmail(user2.email);
                                    prefManager.setIdToken(user1.token);
                                    prefManager.setIdUsername(user2.username);
                                    prefManager.setLoginStat(true);

                                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
                                    finish();

                                }else{
                                    Toast.makeText(getApplicationContext(),"Login failed",Toast.LENGTH_SHORT).show();
                                }

                            }

                            @Override
                            public void onFailure(Call call, Throwable t) {
                                call.cancel();
                            }
                        });


                    }
                });

            }


            buttonRegister.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                    finish();
                }
            });

        }
    }

1 个答案:

答案 0 :(得分:0)

看起来您的基本网址不正确。你能否确认你的实际情况如下:

如果您的api url是:

http://www.example.com/api/login

然后,

基本网址=&#34; http://www.example.com/api/&#34;

下一条路径&#34;登录&#34;应该如下:

@POST("login")