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


  类“从ErrorListener派生的匿名类”必须声明为抽象,或在ErrorListener中实现抽象方法“ onErrorResponse(VolleyError)”


这是我的LoginActivity

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中调用该接口中未定义的方法(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/

10-08 21:26