Что может быть не так с моим проектом Android Volley? - PullRequest
0 голосов
/ 02 сентября 2018

Добрый день люди, я пользуюсь Android Volley, версия 1.1.1. Проект выполняется быстро, когда это GET-запрос, но как только я добавляю params и превращаю его в POST-запрос (как во всех примерах, которые я нашел во всем Интернете), он не хочет запускаться и выдает мне три ошибки:

error no. 1" There is no applicable constructor to '(into, java.lang.String, com.vm.okone.MainActivity.(anonymous),com.vm.okone.MainActivity.(anonymous))', error no. 2 " method onResponse does not override method from its superclass, error no. 3 " method onErrorResponse does not override method from its superclass.

Вот мой POST-код

//TextView
final TextView mTextView = (TextView)findViewById(R.id.serverResp);

        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

        //url
        String url = "http://127.0.0.1:8080/index.jsp";


        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Display the response string.
                    mTextView.setText(response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    mTextView.setText("That didn't work!");
                }
            }) {
            //adding parameters to the request
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("name", "asdf");
                params.put("email", "qwerty");
                return params;
            }
        };

        // Add the request to the RequestQueue.
        queue.add(stringRequest);

1 Ответ

0 голосов
/ 02 сентября 2018

Конструктор класса Volley Response является закрытым, поэтому вы не можете его расширить. Я проверил ваши коды, ошибок не было, поэтому, возможно, существовала другая проблема.

Поместите коды в Oncreate () или другие методы в MainActivity и попробуйте снова.

Редактировать

import android.app.Activity;
import android.os.Bundle;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView mTextView = findViewById(R.id.textView);

        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

        //url
        String url = "http://127.0.0.1:8080/index.jsp";

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the response string.
                        mTextView.setText(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mTextView.setText("That didn't work!");
            }
        }) {
            //adding parameters to the request
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("name", "asdf");
                params.put("email", "qwerty");
                return params;
            }
        };

        // Add the request to the RequestQueue.
        queue.add(stringRequest);
    }
}
...