【问题标题】:The volley library in android studio cannot send requestsandroid studio中的volley库无法发送请求
【发布时间】:2017-12-24 05:32:14
【问题描述】:

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

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

这是我的登录活动

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

【问题讨论】:

标签: java android networking android-volley


【解决方案1】:

您的错误是因为您在 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.
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多