android - 基本网络.performRequest : Unexpected response code 301

标签 android android-studio android-volley

所以我正在尝试登录我的应用程序,当我在插入用户名和密码后单击登录按钮时,此错误显示在 android studio 中左下角底部工具栏的运行选项卡中:

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

只有我的站点链接,仅此而已。 您可以了解有关错误 HTTP 301 here 的更多信息! 这是我的代码:

登录.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;
        }
    }

登录请求.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;
    }
}

如果您需要更多类(class)或下面的 android list 评论,我会编辑帖子。

我已经有 login.php 来连接到文件管理器中的数据库和所有东西。

如果您能帮助我理解为什么会出现此错误,我将不胜感激。

最佳答案

HTTP 代码 301 表示“永久移动”。这意味着您尝试访问的 URL 已不存在。当我的网站实现 SSL 时,我遇到了同样的问题。他们使用服务器中的 .htaccess 文件将所有请求从 http://重定向到 https://,因此必须相应地更改 Android 应用程序中的地址,因为它不会接受重定向。您应该将 LOGIN_REQUEST_URL 变量更改为:

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

希望这会有所帮助(虽然它确实解决了我的问题)

关于android - 基本网络.performRequest : Unexpected response code 301,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48926797/

相关文章:

android - 如何在基于 WordPress 的网站上实现 oAuth2?

android - 我们能否在应用首次打开之前获取用户详细信息

java - ScrollView:将触摸事件传递给 child

android - MvvmCross - 将汉堡菜单更改为后退按钮

Android Studio keystore 密码不可见

java - Ubuntu 上的 Bad JetBrains Intellij IDEA 和 Android Studios CPU 使用率

java - ARCore虚拟物体运动

java - com.android.volley.parse 错误 org.json.jsonexception java.lang.string 类型的值无法在 Android Volley 中转换为 jsonArray

android - Volley 内存不足错误,奇怪的分配尝试

Android Volley Kotlin : (Mutable)Map<(raw) Any?,(原始)有吗?>!与 MutableMap<String, String>