【问题标题】:BasicNetwork.performRequest: Unexpected response code 301BasicNetwork.performRequest:意外响应代码 301
【发布时间】:2018-02-22 11:55:29
【问题描述】:

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

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

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

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;
    }
}

如果您需要更多课程或下面的 android manifest 评论,我会编辑帖子。

我已经有了 login.php 来连接文件管理器中的数据库和一切。

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

【问题讨论】:

  • this error shows up: 它出现在哪里?您在哪里看到/找到的?
  • ???????????什么是运行选项卡?我们能看到那个角落吗?你在期待什么?谁把错误信息放在那里?
  • 在 android studio 底部有 TODO 选项卡,在 TODO 选项卡左侧有 Run 选项卡
  • logcat 中没有更多内容吗? 301是什么意思?
  • 这就是我要问的 m8

标签: android android-studio android-volley


【解决方案1】:

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

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

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

【讨论】:

  • 谢谢,解决了 http >> https,
【解决方案2】:

我遇到了同样的问题。 Unexpected response code 301

我使用的是https://example.com,但它通过.htaccess 重定向到https://www.example.com 在应用代码中更改为https://www.example.com 解决了我的问题。

尝试在浏览器中打开网址,您可以看到完全重定向的网址,将其复制到您的代码中。

【讨论】:

    【解决方案3】:

    您可以在您的主机或服务器中指定一个 http 文件夹,而不是在使用 volley 时自动重定向到 https。 Volley 似乎无法处理 https 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      相关资源
      最近更新 更多