通过Retrofit使用API​​访问数据

时间:2017-07-12 12:00:08

标签: android json api login retrofit2

enter image description here

我正在从API访问登录但我不知道如何使用JSON在用户的其他活动中获得响应,因为我不熟悉改造。我已经在我的课程中设置了所有参数。应该提取的细节如下。我添加了我的API,如下所示。

MainActivity:

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                User user=new User(
                        emailid.getText().toString(),
                        password.getText().toString()

                );
                sendNetworkRequest(user);

            }
        });


        mVideoView = (VideoView) findViewById(R.id.bgVideoView);
        CreateAccount=(Button) findViewById(R.id.newAccountButton);

        Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video);

        mVideoView.setVideoURI(uri);


        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.setLooping(true);
                mediaPlayer.setVolume(0,0);
                mVideoView.requestFocus();
                mediaPlayer.seekTo(position);
                mVideoView.start();


            }
        });

       CreateAccount.setOnClickListener(this);

        frgtpassword=(TextView)findViewById(R.id.forgotpassword);
        frgtpassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getBaseContext(),ForgetPasswordScreen.class);
startActivity(intent);
            }
        });
    }

    private void sendNetworkRequest(User user){
        Retrofit.Builder builder= new Retrofit.Builder()
                .baseUrl("https://api.payhans.com/web/sign-in/")
                .addConverterFactory(GsonConverterFactory.create());

        Retrofit retrofit=builder.build();

        UserClient client=retrofit.create(UserClient.class);
        Call<User> call=client.loginUser(user);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                Toast.makeText(MainActivity.this, "Yeah!+User-ID", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(getBaseContext(),LoginActivity.class);
                startActivity(intent);


            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Toast.makeText(MainActivity.this, "sometihing went wrong", Toast.LENGTH_SHORT).show();

            }
        });
    }}

UserClient:

public interface UserClient {
    @POST("user")
    Call<User>loginUser(@Body User user);
}

用户

public class User {
    private String user_id;
    private String pwd;

    public User(String user_id, String pwd) {
        this.user_id = user_id;
        this.pwd = pwd;
    }

}

1 个答案:

答案 0 :(得分:0)

您必须只设置基本网址.baseUrl("https://api.payhans.com/web/")而不是完整的网址,然后当您在UserClient界面中注释该调用时,在POST注释中传递URL sign-in/

public interface UserClient {
    @POST("sign-in/")
    Call<User>loginUser(@Body User user);
}

同时仔细检查您是否以正确的方式传递数据,因为您要将User作为@Body发送,但在API的屏幕截图中,您似乎必须通过user_id并且{URL}等pwd