【问题标题】:What could be wrong with my Android Volley project?我的 Android Volley 项目可能有什么问题?
【发布时间】:2018-09-02 09:06:17
【问题描述】:

大家好,我使用的是 Android Volley,版本 1.1.1。当它是一个 GET 请求时,该项目会迅速运行,但是一旦我添加参数并将其转换为 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);

【问题讨论】:

  • 它仍然给我同样的错误,我开始认为它与上下文有关,但现在我不知道如何应用它。
  • 这取决于您在哪里使用这些代码?这很奇怪,因为我这边的一切看起来都很正常。
  • 您可以在文件顶部发布您的导入语句吗?
  • 导入android.os.Bundle;导入android.app.Activity;导入 android.widget.TextView;导入 java.util.Map;导入 java.util.HashMap;导入 com.android.volley.Request;导入 com.android.volley.Response;导入 com.android.volley.RequestQueue;导入 com.android.volley.VolleyError;导入 com.android.volley.AuthFailureError;导入 com.android.volley.toolbox.Volley;导入 com.android.volley.toolbox.StringRequest;

标签: android android-volley


【解决方案1】:

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

【讨论】:

  • 代码放在 MainActivity 中的 onCreate() 方法上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 2015-09-22
  • 2019-02-13
  • 2019-03-21
  • 1970-01-01
相关资源
最近更新 更多