从按钮发送的每个请求都会收到相同的响应
Same responses are received for every request sent from button
MainActivity中使用了以下代码:
StringBuilder stringBuilder = new StringBuilder();
Cache cache;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonDiscard = findViewById(R.id.buttonDiscard);
RequestQueue requestQueue;
cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
String url ="https://www.themealdb.com/api/json/v1/1/random.php";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
stringBuilder::append,
error -> System.out.println("Error occurred"));
requestQueue.add(stringRequest);
}
public void handleDiscardClick(View view){
System.out.println(stringBuilder);
}
我面临的问题是,每次单击 url 按钮时,我都会收到相同的字符串响应,该按钮以 json 格式提供随机餐点。我可以获得不同字符串的唯一方法是每次重新启动 android 应用程序,这绝对不好。我尝试在按钮功能内移动网络代码,但我仍然收到相同的响应。如果有人能告诉我如何在每次单击按钮时获得不同的响应,我将不胜感激。 (抱歉,我是 android 网络新手)
我最终修改了整个代码。我在另一个 class 中编写了以下代码并且运行良好:
public class NetworkController {
StringBuilder stringBuilder = new StringBuilder();
private final RequestQueue queue;
public StringBuilder getstringBuilder() {
return stringBuilder;
}
public void setModelFromNetwork() {
final String url = "https://www.themealdb.com/api/json/v1/1/random.php";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
response -> {
try {
stringBuilder.append(response.toString());
} catch (JSONException e) {
System.out.println("JSONException occurred. The error is: ");
e.printStackTrace();
}
}, exception -> System.out.println("Error"));
queue.add(stringRequest);
}
public NetworkController(@NonNull final Activity activity) {
queue = Volley.newRequestQueue(activity.getApplicationContext());
}
}
MainActivity中使用了以下代码:
StringBuilder stringBuilder = new StringBuilder();
Cache cache;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonDiscard = findViewById(R.id.buttonDiscard);
RequestQueue requestQueue;
cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
String url ="https://www.themealdb.com/api/json/v1/1/random.php";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
stringBuilder::append,
error -> System.out.println("Error occurred"));
requestQueue.add(stringRequest);
}
public void handleDiscardClick(View view){
System.out.println(stringBuilder);
}
我面临的问题是,每次单击 url 按钮时,我都会收到相同的字符串响应,该按钮以 json 格式提供随机餐点。我可以获得不同字符串的唯一方法是每次重新启动 android 应用程序,这绝对不好。我尝试在按钮功能内移动网络代码,但我仍然收到相同的响应。如果有人能告诉我如何在每次单击按钮时获得不同的响应,我将不胜感激。 (抱歉,我是 android 网络新手)
我最终修改了整个代码。我在另一个 class 中编写了以下代码并且运行良好:
public class NetworkController {
StringBuilder stringBuilder = new StringBuilder();
private final RequestQueue queue;
public StringBuilder getstringBuilder() {
return stringBuilder;
}
public void setModelFromNetwork() {
final String url = "https://www.themealdb.com/api/json/v1/1/random.php";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
response -> {
try {
stringBuilder.append(response.toString());
} catch (JSONException e) {
System.out.println("JSONException occurred. The error is: ");
e.printStackTrace();
}
}, exception -> System.out.println("Error"));
queue.add(stringRequest);
}
public NetworkController(@NonNull final Activity activity) {
queue = Volley.newRequestQueue(activity.getApplicationContext());
}
}