在Retrofit2 API值传递问题中需要帮助

时间:2018-11-28 08:57:07

标签: android retrofit2

在按下“登录”按钮后,只有传递的部分网址http://127.0.0.1/AdminaService/AdminaService.svc/Verify_User_And_Password_For_login可以在我的日志中看到。 edttxt字段中的值未添加或传递。

[MY api] http://127.0.0.1/AdminaService/AdminaService.svc/Verify_User_And_Password_For_login/ {PUSER_ID} / {PPASSWORD} / {PUSER_SESSION_ID} / {PIPADDRESS} / {PAPPLICATION_ID}

1,2,定义有效或无效。 json格式 {     “ VerifyUserAndPasswordForLoginResult”:“ 1” }

登录

public class LoginActivity extends AppCompatActivity {

    private EditText edit_user, edit_pass;
    private Button btn_login;
    String user_id,pass;
    ApiInterface apiInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getSupportActionBar().hide();
        if (!isConnected(LoginActivity.this)) buildDialog(LoginActivity.this).show();
        edit_user = findViewById(R.id.editText_username);
        edit_pass = findViewById(R.id.editText_password);
        btn_login=findViewById(R.id.button_signIn);
        apiInterface= getapiInterface();




        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userLogin();
            }
        });

    }

    private void userLogin() {
        user_id = edit_user.getText().toString().trim();
        pass = edit_pass.getText().toString().trim();

        if (user_id.isEmpty()) {
            edit_user.setError("Id Required");
            edit_user.requestFocus();
            return;
        }
        if (pass.isEmpty()) {
            edit_pass.setError("Password Required");
            edit_pass.requestFocus();
            return;
        }
        Call<LoginUser> call;
        call = getapiInterface().login(user_id,pass);
        Log.wtf("URL Called", call.request().url() + "");
        call.enqueue(new Callback<LoginUser>() {
            @Override
            public void onResponse(Call<LoginUser> call,Response<LoginUser> response) {
                LoginUser loginUser =response.body();
                if(response.isSuccessful()){
                    Toast.makeText(LoginActivity.this,"Logged id",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<LoginUser> call,Throwable t) {
                Toast.makeText(LoginActivity.this,t.getMessage(),Toast.LENGTH_LONG).show();

            }
        });


        }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {

        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void onPause() {
        super.onPause();
    }


    // To check the internet connectivity of the handset
    public boolean isConnected(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netinfo = cm.getActiveNetworkInfo();

        if (netinfo != null && netinfo.isConnectedOrConnecting()) {
            android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

            return (mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting());
        } else
            return false;
    }

    //To show the error dialog if there is no internet connection
    public AlertDialog.Builder buildDialog(Context c) {

        AlertDialog.Builder builder = new AlertDialog.Builder(c);
        builder.setTitle("No Internet Connection");
        builder.setMessage("You need to have Mobile Data or wifi to access this. Press ok to Exit");

        builder.setPositiveButton("Ok",new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog,int which) {

                finish();
            }
        });

        return builder;
    }


}

接口

public interface ApiInterface {



    @FormUrlEncoded
    @POST("Verify_User_And_Password_For_login/")
    Call<LoginUser> login(@Query("{PUSER_ID}") String user_id ,@Query("{PPASSWORD}") String pass);
}

ApiUtils

public class ApiUtils {

    private static final String Base_url="http://127.0.0.1/AdminaService/AdminaService.svc/";

    public static ApiInterface getapiInterface(){
        return RetrofitClient.getClient(Base_url).create(ApiInterface.class);
    }
}

Pojo课堂:

public class LoginUser {


        @SerializedName("VerifyUserAndPasswordForLoginResult")
        @Expose
        private String verifyUserAndPasswordForLoginResult;

        public String getVerifyUserAndPasswordForLoginResult() {
            return verifyUserAndPasswordForLoginResult;
        }

        public void setVerifyUserAndPasswordForLoginResult(String verifyUserAndPasswordForLoginResult) {
            this.verifyUserAndPasswordForLoginResult = verifyUserAndPasswordForLoginResult;
        }


}

1 个答案:

答案 0 :(得分:0)

由于这是一个发布请求,我认为您应该使用@Field而不是查询或路径。 您的方法声明如下:

 @FormUrlEncoded
 @POST("Verify_User_And_Password_For_login/")
 Call<LoginUser> login(@Field("PUSER_ID") String user_id ,@Field("PPASSWORD") String pass);

将值作为方法调用传递。