java - android studio中volley库无法发送请求

标签 java android networking android-volley

我想将 json 对象发送到服务器。我在 Response.ErrorListener() 上收到此错误。当我尝试通过 android studio 中的 volley 库发送请求时:

Class 'Anonymous class derived from ErrorListener' must either be declared abstract or implement abstract method 'onErrorResponse(VolleyError)' in ErrorListener'

这是我的登录 Activity

package com.example.mujtaba.quizzer;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.StringRequest;
import com.example.mujtaba.quizzer.Activity.QuizMaking;
import com.example.mujtaba.quizzer.Activity.QuizTaking;

import org.w3c.dom.Text;

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

public class Login extends AppCompatActivity {
    private Button button;
    private TextView username;
    private TextView password;
    private Spinner role;
    private String url = "http://yourdomain.com/path";
    private RequestQueue MyRequestQueue;

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

        username=(TextView) findViewById(R.id.username);
        password=(TextView) findViewById(R.id.password);
        button=(Button) findViewById(R.id.button);
        role = (Spinner) findViewById(R.id.role);

// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.role_spinner, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        role.setAdapter(adapter);
    }


    public void Quiz(View v) {   //select a new activity on the basis of role
        StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //This code is executed if the server responds, whether or not the response contains data.
                //The String 'response' contains the server's response.
            }
        }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
            @Override
            public void onErrorResponse(VolleyLog error) {
                //This code is executed if there is an error.
            }
        }) {
            protected Map<String, String> getParams() {
                Map<String, String> MyData = new HashMap<String,String>();
                MyData.put("Field", "Value"); //Add the data you'd like to send to the server.
                return MyData;
            }
        };

        MyRequestQueue.add(MyStringRequest);

        String text = role.getSelectedItem().toString();
        switch (text) {

            case "Student":
                Intent i = new Intent(getBaseContext(), QuizTaking.class);
                startActivity(i);
                break;

            case "Instructor":
                Intent j = new Intent(getBaseContext(), QuizMaking.class);
                startActivity(j);
                break;
        }
    }
}

最佳答案

您的错误是因为您在 ErrorListener 中调用了该接口(interface)中未定义的方法(VolleyLog)。更改如下:

new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
            @Override
            public void onErrorResponse(VolleyLog error) {
                //This code is executed if there is an error.
            }

对于:

new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
            @Override
            public void onErrorResponse(VolleyError error) {//Change is here
                //This code is executed if there is an error.
            }

关于java - android studio中volley库无法发送请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47958283/

相关文章:

java - this.getReadableDatabase() 处出现 NullPointerException;

字符串拆分中的 Java 正则表达式量词

java - 如何在不打开android studio文件管理器的情况下从设备文件管理器的特定文件夹中获取文件?

java - 使用 HashMap 进行 JsonSerialize

java - Android 没有资源匹配 android :windowIsFloating

android - 在 android 中使用 keyczar

android - 如何使用 Firebase 推送消息取决于用户在设备上的帐户状态

sql - Oracle 11g - 插入多行的最有效方法

c# - 用于 HTTP 基本身份验证的 UnityWebRequest 嵌入用户 + 密码数据在 Android 上不起作用

c# - Stream.Write 是线程安全的吗?