0

Good day people, I'm using Android Volley, version 1.1.1. The project runs swiftly when it's a GET request but as soon as I add the params and turn it to a POST request (like in all the examples I found all over the internet) it does not want to run and gives me three errors:

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.

Here is my POST code

//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);
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Vuyile
  • 9
  • 5
  • Check this sample: https://stackoverflow.com/a/45651360/4409113 – ʍѳђઽ૯ท Sep 02 '18 at 09:37
  • It is still giving me the same errors and I'm starting to thing that its something to do with context but for now I don't know how to apply it. – Vuyile Sep 02 '18 at 10:07
  • That depends on where you are using those codes? That's strange because everything looks normal in my side. – ʍѳђઽ૯ท Sep 02 '18 at 10:09
  • Can you post your import statements at the top of your file? – P Fuster Sep 02 '18 at 10:17
  • import android.os.Bundle; import android.app.Activity; import android.widget.TextView; import java.util.Map; import java.util.HashMap; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.RequestQueue; import com.android.volley.VolleyError; import com.android.volley.AuthFailureError; import com.android.volley.toolbox.Volley; import com.android.volley.toolbox.StringRequest; – Vuyile Sep 02 '18 at 14:35
  • Hmm nothing wrong with imports, you might have to post your actual logcat stacktrace to see what it's saying. Maybe you missed out something in your error description. Also the actual onCreate code snippet might reveal something. – P Fuster Sep 02 '18 at 21:48

1 Answers1

0

Volley Response class constructor is private, so you can't extend it. I tested your codes,no errors occurred, so maybe other issue existed.

Place codes in Oncreate() or other methods within MainActivity and try again.

Edit

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);
    }
}
navylover
  • 12,383
  • 5
  • 28
  • 41