英文:
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 ="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);
}
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) {
//...
}
专注分享java语言的经验与见解,让所有开发者获益!
评论