BasicNetwork.performRequest: Unexpected response code 401

0

I have a login in Android studio using Volley that needs to access an API and bring the username and password to be able to show another activity, but when I put the user and the password I get the error.

  

BasicNetwork.performRequest: Unexpected response code 401

here is the code:

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.JsonArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends AppCompatActivity {

Button btnLogin;
EditText edtUserName, edtPassWord;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    if (SharedPrefManager.getInstance(this).isLoggedIn()) {
        finish();
        startActivity(new Intent(this, MenuCirculo.class));
    }
    btnLogin = (Button) findViewById(R.id.btnLogin);
    edtUserName = (EditText) findViewById(R.id.edtUserName);
    edtPassWord = (EditText) findViewById(R.id.edtPassword);

    //if user presses on login
    //calling the method login
    findViewById(R.id.btnLogin).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            userLogin();
        }
    });





}
private void userLogin(){
    //first getting the values
    final String username = edtPassWord.getText().toString().trim();
    final String password = edtPassWord.getText().toString().trim();

    //validating inputs
    if (TextUtils.isEmpty(username)) {
        edtUserName.setError("Please enter your username");
        edtPassWord.requestFocus();
        return;
    }

    if (TextUtils.isEmpty(password)) {
        edtPassWord.setError("Please enter your password");
        edtPassWord.requestFocus();
        return;
    }

    //if everything is fine
    String url = "http://50.56.103.12:28080/tnkservices/v1/usuario/authenticate";
    StringRequest stringRequest =  new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        //converting response to json object
                        JSONObject obj = new JSONObject(response);


                        //if no error in response
                        if (!obj.getBoolean("error")) {
                            Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

                            //getting the user from the response
                            JSONObject userJson = obj.getJSONObject("usuario");

                            //creating a new user object
                            User user = new User(userJson.getString("email"));



                            //storing the user in shared preferences
                            SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);

                            //starting the profile activity
                            finish();
                            startActivity(new Intent(getApplicationContext(), MenuCirculo.class));
                        } else {
                            Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if (error != null && error.getMessage() != null) {
                        Log.e("Response error", error.getMessage());
                    }
                    Toast.makeText(getApplicationContext(), "error: " + error.toString(), Toast.LENGTH_SHORT).show();
                }
            }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            String credentials = "username:password";
            String auth = "Basic "
                    + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
            headers.put("Content-Type", "application/json");
            headers.put("Authorization", auth);
            return headers;
        }
    };

    VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}
 }
    
asked by zilencio0 o0icneliz 28.02.2018 в 21:36
source

1 answer

1

I speak to you without knowing how the web-service is, but I think you are passing the parameters in the header of the query, not the GET.

have you tried to put? String url=" link " + username + "& password=" + pasword;

I use the parameters in the header for web-services methods that are not public.

Greetings

    
answered by 01.03.2018 / 16:34
source