2

So i´m trying to login on my app and when i click the login button after inserting the username and password this error shows up in the android studio at the run tab on the bottom toolbar in the left corner:

E/Volley: [11996] BasicNetwork.performRequest: Unexpected response code 301 "MYURL"

with my site link and nothing more. you can learn more about the error HTTP 301 here! Here is my code:

login.java

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class login extends Fragment{
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View mView = inflater.inflate(R.layout.login, container, false);
            final EditText etUtilizador = mView.findViewById(R.id.etUtilizador);
            final EditText etPassword = mView.findViewById(R.id.etPassword);
            final Button btLogin = mView.findViewById(R.id.btLogin);
            Button btlink = mView.findViewById(R.id.btlink);
            btlink.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent registerIntent = new Intent(getActivity(), registar.class);
                    getActivity().startActivity(registerIntent);
                }
            });
            btLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final String username = etUtilizador.getText().toString();
                    final String password = etPassword.getText().toString();
                    Response.Listener<String> responselistener=new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            JSONObject jsonResponse= null;
                            try {
                                jsonResponse = new JSONObject(response);
                                boolean success = jsonResponse.getBoolean("success");
                                if (success) {
                                    String name = jsonResponse.getString("name");
                                    int age = jsonResponse.getInt("age");

                                    Intent intent;
                                    intent = new Intent(getContext(), utilizador.class);
                                    intent.putExtra("name", name);
                                    intent.putExtra("age", age);
                                    intent.putExtra("username", username);
                                    login.this.startActivity(intent);
                                } else {
                                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                                    builder.setMessage("Login Failed")
                                            .setNegativeButton("Retry", null)
                                            .create()
                                            .show();
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    };
                    loginrequest loginRequest = new loginrequest(username, password, responselistener);
                    RequestQueue queue = Volley.newRequestQueue(getActivity());
                    queue.add(loginRequest);
                }
            });
            return mView;
        }
    }

loginrequest.java

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

public class loginrequest extends StringRequest {
    private static final String LOGIN_REQUEST_URL = "http://elabora.pt/login.php";
    private Map<String, String> params;

    public loginrequest(String username, String password, Response.Listener<String> listener) {
        super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

If you need more classes or the android manifest comment below and i edit the post.

I already have the login.php to connect to the database at the file manager and everything.

If you can help me understanding why this error is showing up i would be gratefull.

Dimitri Putin
  • 29
  • 1
  • 7

3 Answers3

12

HTTP code 301 means "moved permanently". This means the URL you're trying to reach is no longer there. I had the same problem when my site implemented SSL. They redirected all request from http:// to https:// using an .htaccess file in the server, so the address in the Android app had to be changed accordingly, since it's not going to accept the redirection. You should change your LOGIN_REQUEST_URL variable to:

private static final String LOGIN_REQUEST_URL = "https://elabora.pt/login.php";

Hope this helps (it did solve my problem, though)

Juan Carlos
  • 1,073
  • 1
  • 9
  • 13
0

I had the same problem. Unexpected response code 301

I was using https://example.com but it was redirecting to https://www.example.com via .htaccess Changing to https://www.example.com in the app code solved my problem.

Try to open the url in the browser here you can see the exactly redirected URL copy this to your code.

Rana Hyder
  • 363
  • 2
  • 10
0

You can specify a http folder in your host or sever instead of auto redirect to https when you use volley. It seems Volley can not handle https request.

Mohsen Hrt
  • 263
  • 2
  • 9