无法解析符号 queue.add(stringRequest) 在 Volley 中。

huangapple 未分类评论61阅读模式
英文:

Cannot resolve symbol queue.add(stringRequest) in Volley

问题

package com.example.mytest2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

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;

public class MainActivity extends AppCompatActivity {

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

        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "https://www.google.com";

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        textView.setText("Response is: " + response.substring(0,500));
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                textView.setText("That didn't work!");
            }
        });

        queue.add(stringRequest);
    }
}

无法解析的图片请查看此处

英文:

I am new to the android studio and trying to use Volley by repeating the code on the developer web. I have added the permission in the manifest and add 'com.android.volley:volley:1.1.1' in build Gradle. But still got an error in the last coding (queue.add(stringRequest);).

Seems add method got the problem and can not be resolved. Anyone can give a hand?

Thank you!

package com.example.mytest2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

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;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    final TextView textView = (TextView) findViewById(R.id.text1);

    RequestQueue queue = Volley.newRequestQueue(this);
    String url =&quot;https://www.google.com&quot;;

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener&lt;String&gt;() {
                @Override
                public void onResponse(String response) {
                    // Display the first 500 characters of the response string.
                    textView.setText(&quot;Response is: &quot;+ response.substring(0,500));
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            textView.setText(&quot;That didn&#39;t work!&quot;);
        }
    });

    queue.add(stringRequest);
}

Cannot resolve picture see here

error: <identifier> expected
queue.add(stringRequest);
^

error: <identifier> expected
queue.add(stringRequest);
^

答案1

得分: 0

基本网络请求可使用以下代码:

RequestQueue requestQueue;

// 实例化缓存
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB 上限

// 配置网络使用 HttpURLConnection 作为 HTTP 客户端。
Network network = new BasicNetwork(new HurlStack());

// 使用缓存和网络实例化 RequestQueue。
requestQueue = new RequestQueue(cache, network);

// 启动请求队列
requestQueue.start();

String url = "http://www.example.com";

// 构建请求并处理响应。
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // 处理响应数据
    }
},
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // 处理错误
    }
});

// 将请求添加到请求队列。
requestQueue.add(stringRequest);

你可以从Android 开发者官方网站获取详细的指南。


<details>
<summary>英文:</summary>

For basic network requeest use code like below 

RequestQueue requestQueue;

// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap

// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());

// Instantiate the RequestQueue with the cache and network.
requestQueue = new RequestQueue(cache, network);

// Start the queue
requestQueue.start();

String url ="http://www.example.com";

// Formulate the request and handle the response.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Do something with the response
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});

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


You will get proper guideline form [the android devloper official site.](https://developer.android.com/training/volley/requestqueue) 

</details>



# 答案2
**得分**: 0

```java
protected void onCreate(Bundle savedInstanceState) {
     //...
}
英文:

Finally, those coding should be put inside the "onCreate"

protected void onCreate(Bundle savedInstanceState) {
     //...
    }

huangapple
  • 本文由 发表于 2020年7月27日 21:43:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/63116679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定