简体   繁体   English

Android Studio中的凌空库无法发送请求

[英]The volley library in android studio cannot send requests

I want send json objects to a server. 我想将json对象发送到服务器。 I'm getting this error on Response.ErrorListener() . 我在Response.ErrorListener()上收到此错误。 When I try to send requests through the volley library in android studio: 当我尝试通过android studio中的凌空库发送请求时:

Class 'Anonymous class derived from ErrorListener' must either be declared abstract or implement abstract method 'onErrorResponse(VolleyError)' in ErrorListener' 类“从ErrorListener派生的匿名类”必须声明为抽象,或在ErrorListener中实现抽象方法“ onErrorResponse(VolleyError)”

This is my LoginActivity 这是我的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;
        }
    }
}

Your error is because you are calling a method(VolleyLog) within ErrorListener that is not defined in that interface. 您的错误是因为您要在ErrorListener中调用该接口中未定义的方法(VolleyLog)。 Change below: 更改以下:

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

for: 对于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM