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

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

Cannot resolve symbol queue.add(stringRequest) in Volley

问题

  1. package com.example.mytest2;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.widget.TextView;
  5. import com.android.volley.Request;
  6. import com.android.volley.RequestQueue;
  7. import com.android.volley.Response;
  8. import com.android.volley.VolleyError;
  9. import com.android.volley.toolbox.StringRequest;
  10. import com.android.volley.toolbox.Volley;
  11. public class MainActivity extends AppCompatActivity {
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. final TextView textView = (TextView) findViewById(R.id.text1);
  17. RequestQueue queue = Volley.newRequestQueue(this);
  18. String url = "https://www.google.com";
  19. StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
  20. new Response.Listener<String>() {
  21. @Override
  22. public void onResponse(String response) {
  23. // Display the first 500 characters of the response string.
  24. textView.setText("Response is: " + response.substring(0,500));
  25. }
  26. }, new Response.ErrorListener() {
  27. @Override
  28. public void onErrorResponse(VolleyError error) {
  29. textView.setText("That didn't work!");
  30. }
  31. });
  32. queue.add(stringRequest);
  33. }
  34. }

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

英文:

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!

  1. package com.example.mytest2;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.widget.TextView;
  5. import com.android.volley.Request;
  6. import com.android.volley.RequestQueue;
  7. import com.android.volley.Response;
  8. import com.android.volley.VolleyError;
  9. import com.android.volley.toolbox.StringRequest;
  10. import com.android.volley.toolbox.Volley;
  11. public class MainActivity extends AppCompatActivity {
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. }
  17. final TextView textView = (TextView) findViewById(R.id.text1);
  18. RequestQueue queue = Volley.newRequestQueue(this);
  19. String url =&quot;https://www.google.com&quot;;
  20. StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
  21. new Response.Listener&lt;String&gt;() {
  22. @Override
  23. public void onResponse(String response) {
  24. // Display the first 500 characters of the response string.
  25. textView.setText(&quot;Response is: &quot;+ response.substring(0,500));
  26. }
  27. }, new Response.ErrorListener() {
  28. @Override
  29. public void onErrorResponse(VolleyError error) {
  30. textView.setText(&quot;That didn&#39;t work!&quot;);
  31. }
  32. });
  33. queue.add(stringRequest);
  34. }

Cannot resolve picture see here

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

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

答案1

得分: 0

  1. 基本网络请求可使用以下代码:
  2. RequestQueue requestQueue;
  3. // 实例化缓存
  4. Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB 上限
  5. // 配置网络使用 HttpURLConnection 作为 HTTP 客户端。
  6. Network network = new BasicNetwork(new HurlStack());
  7. // 使用缓存和网络实例化 RequestQueue。
  8. requestQueue = new RequestQueue(cache, network);
  9. // 启动请求队列
  10. requestQueue.start();
  11. String url = "http://www.example.com";
  12. // 构建请求并处理响应。
  13. StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
  14. new Response.Listener<String>() {
  15. @Override
  16. public void onResponse(String response) {
  17. // 处理响应数据
  18. }
  19. },
  20. new Response.ErrorListener() {
  21. @Override
  22. public void onErrorResponse(VolleyError error) {
  23. // 处理错误
  24. }
  25. });
  26. // 将请求添加到请求队列。
  27. requestQueue.add(stringRequest);

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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);

  1. You will get proper guideline form [the android devloper official site.](https://developer.android.com/training/volley/requestqueue)
  2. </details>
  3. # 答案2
  4. **得分**: 0
  5. ```java
  6. protected void onCreate(Bundle savedInstanceState) {
  7. //...
  8. }
英文:

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

  1. protected void onCreate(Bundle savedInstanceState) {
  2. //...
  3. }

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:

确定