sending username and password as param in http request

时间:2016-10-09 15:53:47

标签: android


I got one of the sample codes online that uses Restful api for simple login and registration. I have already got the external jar file and compiled it but one of the method is not getting resolved.
The "invokeWS" method is giving error ,though the RequestParams worked fine, please suggest. This is the link that I used to get the jar file--
http://loopj.com/android-async-http/

I am putting the code here

package com.example.dell.restful_demo;

import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;


public class LoginActivity extends AppCompatActivity {

// Progress Dialog Object
ProgressDialog prgDialog;
// Error Msg TextView Object
TextView errorMsg;
// Email Edit View Object
EditText emailET;
// Passwprd Edit View Object
EditText pwdET;

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

    // Find Error Msg Text View control by ID
    errorMsg = (TextView) findViewById(R.id.login_error);
    // Find Email Edit View control by ID
    emailET = (EditText) findViewById(R.id.loginEmail);
    // Find Password Edit View control by ID
    pwdET = (EditText) findViewById(R.id.loginPassword);
    // Instantiate Progress Dialog object
    prgDialog = new ProgressDialog(this);
    // Set Progress Dialog Text
    prgDialog.setMessage("Please wait...");
    // Set Cancelable as False
    prgDialog.setCancelable(false);

}


public void loginUser(View view) {
    // Get Email Edit View Value
    String email = emailET.getText().toString();
    // Get Password Edit View Value
    String password = pwdET.getText().toString();
    // Instantiate Http Request Param Object
    RequestParams params = new RequestParams();
    // When Email Edit View and Password Edit View have values other than Null
    if (Utility.isNotNull(email) && Utility.isNotNull(password)) {
        // When Email entered is Valid
        if (Utility.validate(email)) {
            // Put Http parameter username with value of Email Edit View control
            params.put("username", email);
            // Put Http parameter password with value of Password Edit Value control
            params.put("password", password);
            // Invoke RESTful Web Service with Http parameters
            invokeWS(params);
        }
        // When Email is invalid
        else {
            Toast.makeText(getApplicationContext(), "Please enter valid email", Toast.LENGTH_LONG).show();
        }
    }
    // When any of the Edit View control left blank
    else {
        Toast.makeText(getApplicationContext(), "Please fill the form, don't leave any field blank", Toast.LENGTH_LONG).show();
    }

}

}

1 个答案:

答案 0 :(得分:1)

如果您使用的是基本身份验证,则必须通过授权标头发送用户名和密码,而不是身体参数。

这是一个例子:

GET / securefiles / HTTP / 1.1
主持人:www.httpwatch.com
授权:基本aHR0cHdhdGNoOmY =

https://www.httpwatch.com/httpgallery/authentication/

凭据在Base64中编码,您可以在此处看到解释:

https://security.stackexchange.com/questions/29916/why-does-http-basic-authentication-encode-the-username-and-password-with-base64