无法使用Volley库中由newRequestQueue创建的队列

时间:2019-03-14 23:20:55

标签: java android http kotlin android-volley

我正在尝试在Kotlin中创建我的第一个应用程序。我正在使用Volley库

implementation 'com.android.volley:volley:1.1.1'

我正在使用此tutorial,所以我的代码是相同的:

package com.example.raspberrycontrol

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley


class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
}

val textView = findViewById<TextView>(R.id.textfield)

val queue = Volley.newRequestQueue(this)
val url = "http://www.google.com"

val stringRequest = StringRequest(
    Request.Method.GET, url,
    Response.Listener<String> { response ->
        // Display the first 500 characters of the response string.
        textView.text = "Response is: ${response.substring(0, 500)}"
    },
    Response.ErrorListener { textView.text = "That didn't work!" })

queue.add(stringRequest)

fun httpsRequest(view: View){
}

我的问题是

出现错误
queue.add(stringRequest)

当我将鼠标悬停在它上面时,它说它是“期望成员声明”,就好像我之前没做过几行一样:

val queue = Volley.newRequestQueue(this)

我在这里做错了什么或想念什么?

2 个答案:

答案 0 :(得分:0)

您添加的代码必须位于onCreate() {作用域}之内。

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    val textView = findViewById<TextView>(R.id.textfield)

    val queue = Volley.newRequestQueue(this)
    val url = "http://www.google.com"

    val stringRequest = StringRequest(
    Request.Method.GET, url,
    Response.Listener<String> { response ->
        // Display the first 500 characters of the response string.
        textView.text = "Response is: ${response.substring(0, 500)}"
    },
    Response.ErrorListener { textView.text = "That didn't work!" })

    queue.add(stringRequest)

}

答案 1 :(得分:0)

enter image description here

private void getAuthTocken() {


         RequestQueue queue = Volley.newRequestQueue(this);
                StringRequest sr = new StringRequest(Request.Method.POST,"http://www.google.com", new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        oAuth_model = gson.fromJson(response, oAuth_Model.class);
                        if (oAuth_model != null){
                           Log.e("TAG", String.valueOf(oAuth_model));
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("oAuth","Authorize failed");
                    }
                }){
                    @Override
                    protected Map<String,String> getParams(){
                        Map<String,String> params = new HashMap<String, String>();
                        params.put("string","string");   
                        return params;
                    }

                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        Map<String,String> params = new HashMap<String, String>();
                        params.put("Content-Type","application/x-www-form-urlencoded");
                        return params;
                    }
                };
                queue.add(sr);

            }


    Kotlin:

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)

            val textView = findViewById<TextView>(R.id.textfield)

            val queue = Volley.newRequestQueue(this)
            val url = "http://www.google.com"

            val stringReq[![enter image description here][2]][2]uest = StringRequest(
                Request.Method.GET, url,
                Response.Listener<String> { response ->
                    // Display the first 500 characters of the response string.
                    textView.text = "Response is: ${response.substring(0, 500)}"
                },
                Response.ErrorListener { textView.text = "That didn't work!" })

            queue.add(stringRequest)
        }

enter image description here