改进Android 400错误ASP.NET API

时间:2016-05-02 13:22:27

标签: java c# android asp.net retrofit

我正在努力将我的Android应用程序连接到ASP.NET API。

当我使用邮递员(http://localhost:3863/api/account/login x-www-form-encoded)发帖时,我得到HTTP 200:

{
    "success": true,
    "username": "mathijs.cop"
}

当我从http://10.0.2.2:3863/api/account/login上的android内部尝试时,我得到(请注意IP已更改,因为我们是从Android模拟器中调用的):

05-02 12:57:14.098 25405-26563/be.kdg.integratieproject D/Retrofit: ---> HTTP POST http://10.0.2.2:3863/api/account/login
05-02 12:57:14.098 25405-26563/be.kdg.integratieproject D/Retrofit: Content-Type: application/x-www-form-urlencoded; charset=UTF-8
05-02 12:57:14.098 25405-26563/be.kdg.integratieproject D/Retrofit: Content-Length: 35
05-02 12:57:14.098 25405-26563/be.kdg.integratieproject D/Retrofit: username=mathijs.cop&password=admin
05-02 12:57:14.098 25405-26563/be.kdg.integratieproject D/Retrofit: ---> END HTTP (35-byte body)
05-02 12:57:14.105 25405-26563/be.kdg.integratieproject D/Retrofit: <--- HTTP 400 http://10.0.2.2:3863/api/account/login (6ms)
05-02 12:57:14.105 25405-26563/be.kdg.integratieproject D/Retrofit: Content-Type: text/html; charset=us-ascii
05-02 12:57:14.105 25405-26563/be.kdg.integratieproject D/Retrofit: Server: Microsoft-HTTPAPI/2.0
05-02 12:57:14.105 25405-26563/be.kdg.integratieproject D/Retrofit: Date: Mon, 02 May 2016 12:57:22 GMT
05-02 12:57:14.105 25405-26563/be.kdg.integratieproject D/Retrofit: Connection: close
05-02 12:57:14.105 25405-26563/be.kdg.integratieproject D/Retrofit: Content-Length: 334
05-02 12:57:14.105 25405-26563/be.kdg.integratieproject D/Retrofit: OkHttp-Selected-Protocol: http/1.1
05-02 12:57:14.105 25405-26563/be.kdg.integratieproject D/Retrofit: OkHttp-Sent-Millis: 1462193834103
05-02 12:57:14.105 25405-26563/be.kdg.integratieproject D/Retrofit: OkHttp-Received-Millis: 1462193834105
05-02 12:57:14.106 25405-26563/be.kdg.integratieproject D/Retrofit: <--- END HTTP (334-byte body)
05-02 12:57:14.108 25405-25405/be.kdg.integratieproject E/LOGIN: UNSUCCESSFUL

这是我的API控制器:

    namespace MVC.Controllers.API
{
    public class AccountController : ApiController
    {

        /// <summary>
        /// Inloggen van een gebruiker.
        /// </summary>
        /// <param name="aanmeldViewModel"></param>
        /// <returns></returns>
        [System.Web.Http.HttpPost]
        public IHttpActionResult Login(AanmeldViewModel aanmeldViewModel)
        {
            if (WebSecurity.Login(aanmeldViewModel.Username, aanmeldViewModel.Password, true))
            {
                return Ok(new { success = true, username = aanmeldViewModel.Username });
            }
            return Ok(new { success = false, message = "De opgegeven gebruikersnaam of wachtwoord is niet geldig!" });
        }
    }
}

LoginActivity:

       loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = usernameText.getText().toString();

                password = passwordText.getText().toString();
                    //logt de gebruiker in met username en paswoord
                    login();
              }
        });



        private void login(){
            getService().login(username,password,this);
        }


 @Override
    public void success(Login login, Response response) {
        Log.e("LOGIN","SUCCESS");
        Log.e("loginboolean", Boolean.toString(login.isSuccess()));
        Log.e("Loginnaam", login.getUsername()+"");
        loggedIn = login.isSuccess();
        System.out.println(loggedIn);
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        sharedPreferences.edit().putBoolean("authorized",loggedIn).apply();
        if(login.isSuccess() == false){
            passwordText.setTextColor(getResources().getColor(R.color.red));
            passwordText.setHintTextColor(getResources().getColor(R.color.red));
            passwordText.clearFocus();
            usernameText.clearFocus();
        }else{
            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            intent.putExtra("loggedIn",loggedIn);
            startActivity(intent);
        }

    }

    @Override
    public void failure(RetrofitError error) {
        Log.e("LOGIN","UNSUCCESSFUL");
        Toast.makeText(this,"U moet een username en password worden ingegeven!", Toast.LENGTH_LONG).show();

    }

回调服务界面:

@FormUrlEncoded
@POST("/account/login")
void login(@Field("username") String username, @Field("password") String password, Callback<Login> callback);

为什么邮递员工作但我的申请没有?

1 个答案:

答案 0 :(得分:0)

更新:

问题是我的模拟器无法连接到我的本地主机。

所以我的解决方案是在外部服务器上部署项目,这样就可以完美地运行:

05-03 09:44:43.269 2334-2366/be.kdg.integratieproject D/Retrofit: ---> HTTP POST http://10.134.216.25:8017/api/account/login
05-03 09:44:43.269 2334-2366/be.kdg.integratieproject D/Retrofit: Content-Type: application/x-www-form-urlencoded; charset=UTF-8
05-03 09:44:43.269 2334-2366/be.kdg.integratieproject D/Retrofit: Content-Length: 35
05-03 09:44:43.269 2334-2366/be.kdg.integratieproject D/Retrofit: username=mathijs.cop&password=admin
05-03 09:44:43.269 2334-2366/be.kdg.integratieproject D/Retrofit: ---> END HTTP (35-byte body)
05-03 09:44:43.309 2334-2366/be.kdg.integratieproject D/Retrofit: <--- HTTP 200 http://10.134.216.25:8017/api/account/login (39ms)
05-03 09:44:43.309 2334-2366/be.kdg.integratieproject D/Retrofit: Cache-Control: no-cache
05-03 09:44:43.309 2334-2366/be.kdg.integratieproject D/Retrofit: Pragma: no-cache
05-03 09:44:43.309 2334-2366/be.kdg.integratieproject D/Retrofit: Content-Type: application/json; charset=utf-8
05-03 09:44:43.309 2334-2366/be.kdg.integratieproject D/Retrofit: Expires: -1
05-03 09:44:43.309 2334-2366/be.kdg.integratieproject D/Retrofit: Server: Microsoft-IIS/7.5
05-03 09:44:43.310 2334-2366/be.kdg.integratieproject D/Retrofit: X-AspNet-Version: 4.0.30319
05-03 09:44:43.310 2334-2366/be.kdg.integratieproject D/Retrofit: Set-Cookie: .ASPXAUTH=E283B64A86F26A8987CB6E7DF00FF7F970540F6D4786BCE73CAD8F2002D8B83ABAD64934D32863D9D97312CAD94E47DA2AFD43934C15E429BA25C339BB8CAB7EC7D60AEF048EC0591E3D031836AAAF1C92394401B956B161FE6BC626DFE8BAEF517F9C54F5FC71C632E16B13A7FF614E; expires=Tue, 03-May-2016 10:14:36 GMT; path=/; HttpOnly
05-03 09:44:43.310 2334-2366/be.kdg.integratieproject D/Retrofit: X-Powered-By: ASP.NET
05-03 09:44:43.310 2334-2366/be.kdg.integratieproject D/Retrofit: Date: Tue, 03 May 2016 09:44:36 GMT
05-03 09:44:43.310 2334-2366/be.kdg.integratieproject D/Retrofit: Content-Length: 41
05-03 09:44:43.310 2334-2366/be.kdg.integratieproject D/Retrofit: OkHttp-Selected-Protocol: http/1.1
05-03 09:44:43.310 2334-2366/be.kdg.integratieproject D/Retrofit: OkHttp-Sent-Millis: 1462268683276
05-03 09:44:43.310 2334-2366/be.kdg.integratieproject D/Retrofit: OkHttp-Received-Millis: 1462268683308
05-03 09:44:43.310 2334-2366/be.kdg.integratieproject D/Retrofit: {"success":true,"username":"mathijs.cop"}
05-03 09:44:43.310 2334-2366/be.kdg.integratieproject D/Retrofit: <--- END HTTP (41-byte body)
05-03 09:44:43.313 2334-2334/be.kdg.integratieproject E/LOGIN: SUCCESS
05-03 09:44:43.313 2334-2334/be.kdg.integratieproject E/loginboolean: true
05-03 09:44:43.313 2334-2334/be.kdg.integratieproject E/Loginnaam: mathijs.cop

但是,如果你想使用本地服务器,我读到一些关于url中的端口号的东西是android模拟器无法识别的。有关详细信息,请参阅此处:

400 bad request accessing ASP.NET mvc 4 application from android